{"id":182943,"date":"2021-02-19T17:00:22","date_gmt":"2021-02-19T14:00:22","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/"},"modified":"2021-02-19T17:00:22","modified_gmt":"2021-02-19T14:00:22","slug":"bash-automation-scripting-basics-part-1-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/","title":{"rendered":"#Bash Automation &#038; Scripting Basics (Part 1) \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a279d7812416\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a279d7812416\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/#What_Is_Bash_automation\" >What Is Bash automation?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/#Shebang\" >Shebang!<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/#Input_Parameters\" >Input Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/bash-automation-scripting-basics-part-1-cloudsavvy-it\/#Wrapping_up\" >Wrapping up<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Bash Automation &amp; <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">Script<\/a>ing Basics (Part 1) \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<figure style=\"width: 700px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-9748 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6a265fd5b3845c325bc5e843a1ff97bc\/p\/uploads\/2021\/02\/cc604deb.png\" alt=\"\" width=\"700\" height=\"307\" data-crediturl=\"https:\/\/www.shutterstock.com\/image-illustration\/robot-typing-on-computer-keyboard-automation-400106332\" data-credittext=\"Shutterstock\/Mopic\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-illustration\/robot-typing-on-computer-keyboard-automation-400106332\">Shutterstock\/Mopic<\/a><\/span><\/figcaption><\/figure>\n<p>Automation of a repetitive task tends to bring joy to the Bash developer: instead of typing endless commands again and again, a script simply does the work repetitively. Bash is ideal for such automation. This <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/watch-movies-tv-seriess\/\" data-internallinksmanager029f6b8e52c=\"8\" title=\"Watch Movies &amp; TV Series\" target=\"_blank\" rel=\"noopener\">series<\/a> will get you started\u2026<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_Is_Bash_automation\"><\/span>What Is <i>Bash automation<\/i>?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Bash shell is a powerful Linux shell which allows in-depth automation of repetitive tasks. Not only is the Bash Linux shell a prime choice for DevOps, database and test engineers alike, every day users can benefit from slowly learned, ever-increasing Bash skills. Bash is also a scripting and coding language that grows on you. I have been actively coding in Bash since 2012 and have used it much longer then that.<\/p>\n<p>Bash also lends itself to many different application domains and use cases. For example, you can easily use for Big Data handling, and surprisingly it seems to lend itself extremely well to this task due to the myriad of text processing tools available within it, or available as easy to install packages. It is also very suited for backup and database scheduling and maintenance, or handling large file storage solutions, automating web servers and much more.<\/p>\n<p>One thing I have found is that whenever the next problem presents itself, a little research in a search engine, or the various Stackoverflow websites, will quickly yield not only a solution to the problem, but an opportunity to grow and learn. This is a very similar experience to a person learning the editor <code>vi<\/code> where the same holds; whenever a problem presents itself, the solution is nearby.<\/p>\n<p>This mini series consisting of three parts of which is the first, and in it we will look at Bash automation and scripting basics.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Shebang\"><\/span>Shebang!<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>You may be wondering what sort of a title that is. And you would be right, unless you were talking to a seasoned Linux developer. They would smile at best. That is because the two first letters of a well-written Bash script would always be, well, Shebang!<\/p>\n<p>There are two characters, namely <code>#!<\/code> that can be placed at the top of a script, which indicate to the shell what interpreter can, should, and will be used to process the script at hand. It happens to be that this <i>symbol<\/i> as a whole is called <i>Shebang<\/i> as a whole.<\/p>\n<p>Thus, to expand on our previous statement: the first line of a well-written Bash script should start with <code>#!\/bin\/bash<\/code> to indicate to the shell (whatever shell is being used, it could be for example <code>bash<\/code> or <code>sh<\/code> or <code>zsh<\/code>) that we want the binary <code>\/bin\/bash<\/code> (our Bash shell) to execute this code.<\/p>\n<p>Let\u2019s put this into practice, by defining a small script <code>test.sh<\/code> as follows:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho 'Hello CloudSavvyIT Readers!'&#13;\n<\/pre>\n<p>You can create this script by using your favorite text editor, which preferably would be an editor which uses only monospace fonts like <i>vi<\/i>, <i>vim<\/i>, <i>nano<\/i> or some plain text based editor in your Linux Desktop, and preferably avoiding things like a word processor like OpenOffice Writer etc. as they may write extra unwanted binary data or characters to our script. In other words; we need to use plain text.<\/p>\n<p>Once this script is defined, we make it executable by executing <code>chmod +x test.sh<\/code> at the command line. Then we can start the script simply by calling it\u2019s name prefixed with <code>.\/<\/code>: <code>.\/test.sh<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9677\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/0f0534deef2e025a2678972087878025\/p\/uploads\/2021\/02\/8fedaa68.png\" alt=\"Our first bash script using shebang and echo\" width=\"362\" height=\"160\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Input_Parameters\"><\/span>Input Parameters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As soon as you start writing scripts, you will likely find that you want to pass input parameters to the script. One way of doing so is to use the simple positional parameters which are available in Bash by default. Let\u2019s have a look at an example script <code>test2.sh<\/code>, which we define as follows:<\/p>\n<pre>#!\/bin\/bash&#13;\n&#13;\necho \"${1}\"&#13;\n<\/pre>\n<p>Here we have used the positional parameter <code>${1}<\/code>. This variable will reflect the first word (separated by space by default) which was passed to our script, unless quotation is used. Let\u2019s see how this works:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9678\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/c2946636fa992ec4e8bce0a04d24bc8e\/p\/uploads\/2021\/02\/27c47967.png\" alt=\"Using positional parameters in our second bash script\" width=\"392\" height=\"228\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>After making our script executable again with <code>chmod +x test2.sh<\/code> we execute the same and pass a single word <code>hello<\/code> as the first positional parameter (<code>${1}<\/code>). The result is that <code>hello<\/code> is echoed back to us.<\/p>\n<p>This is because when the script was started, the variable <code>${1}<\/code> (or <code>$1<\/code> though I recommend to always put quotes around variable names) was set to the value of the first positional parameter; the first word or quoted string after the script name.<\/p>\n<p>Next we passed <code>hello world<\/code>, however this echoed only <code>hello<\/code> back to us. The reason is simply; the second word (as separated by default by a space) is seen as the second positional parameter and is thus initialized as <code>${2}<\/code> in the script, not <code>${1}<\/code>.<\/p>\n<p>We circumvented this behavior by putting quotes around the input when we passed <code>'Hello CloudSavvyIT Readers!'<\/code>. Single or double quotes would both have worked, though their operation differs, more on this in the next part of this series. The result is that our first positional parameter <code>${1}<\/code> is set to the full text <code>Hello CloudSavvyIT Readers!<\/code>, and thus the same is echoed back to us.<\/p>\n<p>If you would like to learn more about variables, our Bash Functions and Local Variables and Exporting Variables in Bash: the Why and How articles may be of interest too.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Wrapping_up\"><\/span>Wrapping up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this article, we reviewed the first set of Bash automation and scripting basics. We learned what Bash automation and a Shebang is, and how to start passing input variables to our scripts. In the next part of this series we will look at variable quoting and more! <strong>Stay tuned!<\/strong>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/9676\/bash-automation-scripting-basics-part-1\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Bash Automation &amp; Scripting Basics (Part 1) \u2013 CloudSavvy IT&#8221; Shutterstock\/Mopic Automation of a repetitive task tends to bring joy to the Bash developer: instead of typing endless commands again and again, a script simply does the work repetitively. Bash is ideal for such automation. This series will get you started\u2026 What Is Bash automation?&#8230;<\/p>\n","protected":false},"author":1,"featured_media":182944,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6a265fd5b3845c325bc5e843a1ff97bc\/p\/uploads\/2021\/02\/cc604deb.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-182943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/182943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=182943"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/182943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/182944"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=182943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=182943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=182943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}