{"id":179,"date":"2010-04-06T19:19:54","date_gmt":"2010-04-06T18:19:54","guid":{"rendered":"http:\/\/www.nax.cz\/2010\/04\/06\/bash-tips-tricks\/"},"modified":"2010-04-06T19:19:54","modified_gmt":"2010-04-06T18:19:54","slug":"bash-tips-tricks","status":"publish","type":"post","link":"https:\/\/nax.cz\/?p=179","title":{"rendered":"bash tips &#038; tricks"},"content":{"rendered":"<p>Recently I am <a href=\"http:\/\/www.google.ie\">in need<\/a> to repeat bash scripting and may be to learn something new. There are some nice features which surprised me, because I have never saw them in action, I haven&#8217;t know them.<\/p>\n<p>Just for record, I use <a href=\"http:\/\/www.amazon.com\/Unix-Shells-Example-Ellie-Quigley\/dp\/0130212229\">Unix Shell by Example by Eliie Quigley<\/a>.<\/p>\n<p><strong>variables<\/strong><\/p>\n<p><i>variable=${variable:-word}<\/i> If variable is set and is non-null, substitute its value; otherwise, substitute word. This can be very easily set just unset variables. It is equivalent of:<\/p>\n<pre>\nif [ \".$variable\" = \".\" ] ; then \n   $variable = \"word\";\nfi\n<\/pre>\n<p><em>${variable:?word}<\/em> If variable is set and is non-null, substitute its value; otherwise, print word and exit from the shell. If word is omitted, the message parameter null or not set is printed.<\/p>\n<pre>\nif [ \".$variable\" = \".\" ] ; then \n   echo \"word\";\n   exit\nfi\n<\/pre>\n<p><em>${variable:offset:length}<\/em> Something as a substring() function in other programing languages. You can omit <em>length<\/em> parameter.<\/p>\n<p><strong>read &#038; arrays<\/strong><\/p>\n<p>You can give your user command line editing functions (let say with vi editor commands) while using read command:<\/p>\n<p><em>read -e variable<\/em><\/p>\n<p>Do you know that <em>read<\/em> command can be used also to read elements to an array?<\/p>\n<p><em>read -a arrayname<\/em><\/p>\n<p>You can address fields in array like this: <em>${arrayname[2]}<\/em>. To declare an array you can use keyword <em>declare<\/em> with same parameter (-a):<\/p>\n<p><em>declare -a fibonaci=(1 1 2 3 5 8 13)<\/em><\/p>\n<p>or just declare it with parenthesis:<\/p>\n<p><em>fibonaci=(1 1 2 3 5 8 13)<\/em><\/p>\n<p>BTW, you do not need to declare in right order, or you can even omit some slot:<\/p>\n<pre>\nfibonaciwtf=(1 [6]=13 2 [1]=1)\nfor element in `seq 0 6`; do \n   echo $element=${fibonaciwtf[$element]}\ndone\n\n0=1\n1=1\n2=\n3=\n4=\n5=\n6=13\n<\/pre>\n<p><strong>parsing parameters<\/strong><\/p>\n<pre>\nwhile getopts xy options; do\n   case $options in\n        x) echo \"you entered \u00e2\u0080\u0093x as an option\"\n            ;;\n        y) echo \"you entered \u00e2\u0080\u0093y as an option\"\n            ;;\n   esac\ndone\n<\/pre>\n<p>Another great source is <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/\">Advanced Bash-Scripting Guide<\/a> from tldp.org<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I am in need to repeat bash scripting and may be to learn something new. There are some nice features which surprised me, because I have never saw them in action, I haven&#8217;t know them. Just for record, I use Unix Shell by Example by Eliie Quigley. variables variable=${variable:-word} If variable is set and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35,19],"tags":[48,76],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-linux","category-unix","tag-bash","tag-scripting"],"_links":{"self":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":0,"href":"https:\/\/nax.cz\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"wp:attachment":[{"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nax.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}