{"id":990,"date":"2014-07-05T15:07:15","date_gmt":"2014-07-05T13:07:15","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=990"},"modified":"2024-12-22T12:14:43","modified_gmt":"2024-12-22T11:14:43","slug":"bash","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=990","title":{"rendered":"bash"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Hash_tables\"><span class=\"toc_number toc_depth_1\">1<\/span> Hash tables<\/a><\/li><li><a href=\"#Split_String\"><span class=\"toc_number toc_depth_1\">2<\/span> Split String<\/a><\/li><li><a href=\"#String_contains\"><span class=\"toc_number toc_depth_1\">3<\/span> String contains<\/a><\/li><li><a href=\"#Replace_string_by_another\"><span class=\"toc_number toc_depth_1\">4<\/span> Replace string by another<\/a><\/li><li><a href=\"#Copy_one_array_to_an_other\"><span class=\"toc_number toc_depth_1\">5<\/span> Copy one array to an other<\/a><\/li><li><a href=\"#Quick_create_file_using_heredoc\"><span class=\"toc_number toc_depth_1\">6<\/span> Quick create file using heredoc<\/a><\/li><li><a href=\"#Loop_though_only_directories\"><span class=\"toc_number toc_depth_1\">7<\/span> Loop though only directories<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Hash_tables\">Hash tables<\/span><\/h1>\n<pre lang=\"bash\">\r\n# First, declare associative array:\r\ndeclare -A myAssociativeArray\r\n\r\n# Then, fill your array:\r\nmyAssociativeArray=( [\"key1\"]=\"value1\" [\"key2\"]=\"value2\")\r\n\r\n# Finally, display:\r\necho \"${myAssociativeArray[\"key1\"]}\"\r\n\r\n# You can iterate elements like this:\r\nfor element in \"${!myAssociativeArray[@])}\";\r\ndo\r\n  echo \"$element <=> ${myAssociativeArray[\"$element\"]}\";\r\ndone\r\n\r\n<\/pre>\n<h1><span id=\"Split_String\">Split String<\/span><\/h1>\n<pre lang=\"bash\">\r\n# split string MYSTRING with delimiter \u201c;\u201d\r\nMYSTRING=\"string1;string2\"\r\n\r\nOIFS=\"$IFS\"\r\nIFS=\";\"\r\n\r\nread -a array <<< \"${MYSTRING}\"\r\n\r\necho $array\r\n\r\ncount=${#array[@]}\r\necho \"count: $count\"\r\n\r\necho \"element 1: ${array[0]}\"\r\necho \"element 2: ${array[1]}\"\r\n<\/pre>\n<h1><span id=\"String_contains\">String contains<\/span><\/h1>\n<pre lang=\"bash\">\r\nstring='My string';\r\n\r\nif [[ $string == *My* ]]\r\nthen\r\n  echo \"It contains!\";\r\nfi\r\n<\/pre>\n<h1><span id=\"Replace_string_by_another\">Replace string by another<\/span><\/h1>\n<pre lang=\"bash\">\r\nMYNEWSTRING=$(echo $MYSTRING| sed 's\/OLDSTRING\/NEWSTRING\/g')\r\n<\/pre>\n<h1><span id=\"Copy_one_array_to_an_other\">Copy one array to an other<\/span><\/h1>\n<pre lang=\"bash\">\r\n# array\r\narray1=(\"elt1\",\"elt2\",\"elt3\")\r\n\r\n# copy array1 content to array2\r\narray2=(\"${array1[@]}\")\r\n\r\n# print array2 values:\r\nfor val in \"${array2 [@]}\" ; do\r\n   echo \"$val\" \r\ndone  \r\n<\/pre>\n<h1><span id=\"Quick_create_file_using_heredoc\">Quick create file using heredoc<\/span><\/h1>\n<pre lang=\"bash\">\r\nmystring=\"hello world\"\r\n\r\ncat > \/tmp\/heredoc.txt << EOF\r\necho \"this should NOT expand 'mystring' var\"\r\necho \\$mystring\r\necho\r\necho \"this should expand 'mystring' var\"\r\necho $mystring\r\nEOF\r\n<\/pre>\n<p>and the result :<\/p>\n<pre lang=\"bash\">\r\n[root@localhost ~]# cat \/tmp\/heredoc.txt\r\necho \"this should NOT expand 'mystring' var\"\r\necho $mystring\r\necho\r\necho \"this should expand 'mystring' var\"\r\necho hello world\r\n<\/pre>\n<h1><span id=\"Loop_though_only_directories\">Loop though only directories<\/span><\/h1>\n<pre lang=\"bash\">\r\nfor d in *\/ ; do\r\n    echo \"$d\"\r\ndone\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Hash tables2 Split String3 String contains4 Replace string by another5 Copy one array to an other6 Quick create file using heredoc7 Loop though only directories Hash tables # First, declare associative array: declare -A myAssociativeArray # Then, fill your array: myAssociativeArray=( [&#8220;key1&#8243;]=&#8221;value1&#8221; [&#8220;key2&#8243;]=&#8221;value2&#8221;) # Finally, display: echo &#8220;${myAssociativeArray[&#8220;key1&#8243;]}&#8221; # You can iterate elements like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1298,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-990","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=990"}],"version-history":[{"count":8,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/990\/revisions"}],"predecessor-version":[{"id":1894,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/990\/revisions\/1894"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1298"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}