{"id":107,"date":"2008-05-12T23:32:25","date_gmt":"2008-05-12T18:02:25","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/?p=107"},"modified":"2008-05-12T23:32:25","modified_gmt":"2008-05-12T18:02:25","slug":"hello-script-for-bash","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2008\/05\/hello-script-for-bash\/","title":{"rendered":"Hello Script for bash"},"content":{"rendered":"<p class=\"intro\"><a href=\"http:\/\/www.bin-co.com\/blog\/2008\/03\/learning-a-new-programming-language-the-hello-world-method\/\">Hello Script series<\/a> for Bash. <strong>&#8216;Hello Script&#8217; is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet<\/strong> when working with that language.<\/p>\n<h2>bash<\/h2>\n<p><a href=\"http:\/\/www.gnu.org\/software\/bash\/\">bash<\/a> is the most commonly used shell in Linux. That makes the bash scripting language the most popular shell scripting language. OK, maybe after batch. But then again, bash is much more powerful than batch(DOS scripting language). If want to learn bash, I will recommend this tutorial &#8211; <a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/\">Advanced Bash Scripting<\/a><\/p>\n<p>Officially, <a href=\"http:\/\/lindesk.com\/2008\/05\/shell-script-language-use-perl-not-bash\/\">I hate bash<\/a>. I use perl or other similar high level language to create shell script. I use bash only for the <a href=\"http:\/\/binnyva.blogspot.com\/2007\/03\/using-twitter-part-1-command-line.html\">simplest scripts<\/a>. But even I admit that bash has its uses. So, here is the hello script for bash&#8230;<\/p>\n<h2>Hello Script<\/h2>\n<pre><code class=\"bash\">\n#!\/usr\/sh\n \n# Printing(IO)\necho \"Hello World\"\n\n# Variables, concatenation\nname='Binny'\nyear=2008\necho \"Hello, \" $name \" - welcome to \" $year\n\n#If,else conditions\nif [ $year -gt 2008 ]; then\n\techo \"Welcome to the future - yes, we have flying cars!\"\n\t\nelif [ $year -lt 2008 ]; then\n\techo \"The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!\"\n\t\nelse\n\techo \"Anything wrong with your time machine? You have not gone anywhere, kiddo.\"\nfi\n\n# If you are using anything after this, consider using a high level language.\n# For loop\nfor i in 1 2 3\ndo\n\techo $i \") Hi there!\"\ndone\n\n#Numerical Array, foreach\nrules[0]=\"Do no harm\"\nrules[1]=\"Obey\" \nrules[2]=\"Continue Living\"\n\nfor ((i=0; i &lt; 3; i++))\ndo\n\techo \"Rule\" `expr $i + 1` \":\" ${rules[$i]}\ndone\n\n\n#A While Demo\nkeys=(hello foo lorem)\n\ni=0\nwhile [ $i -lt 3 ]\ndo\n\techo ${keys[$i]}\n\ti=`expr $i + 1`\ndone\n\n# Function, argument, return, call\nhello () {\n\tmyname=$1  #First argument.\n\techo \"Hello\" $myname\n}\nhello \"Binny\"\n\n \n# File IO\n# File reading\ncontents=`cat Hello.sh` #For some reason, I'm losing all the \\n's in the file.\necho \"Hello has `echo $contents|wc -m` chars\" # Or wc -m Hello.sh\n\n# Writing to a file\necho \"Hello World from shell script\" &gt; \/tmp\/hello.txt\n \n# Command Executing\nls\n\n# Regular Expressions\nstring=\"Hello World\"\nevil=`echo $string | grep '^Hell'`\nif [ \"$evil\" != \"\" ]; then\n\techo \"Yup - its evil\"\nfi\necho \"Hello World\" | sed -e 's\/l\/\/g' #Will return Heo Word\n\n# http:\/\/tldp.org\/LDP\/abs\/html\/\n<\/code><\/pre>\n<p>And, by the way, this will work only in Linux &#8211; or if you installed cygwin in your Windows system. Use <code>sh &lt;File_Name&gt;<\/code> to execute the above code.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Hello Script series for Bash. &#8216;Hello Script&#8217; is a file that contains the most commonly used elements of a programming language so that it can <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2008\/05\/hello-script-for-bash\/\" title=\"Hello Script for bash\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,25,28],"tags":[56,137,173,245,247,254],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-linux","category-scripts","category-tutorial","tag-bash","tag-hello","tag-linux","tag-script","tag-scripting","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}