{"id":1115,"date":"2018-10-21T17:22:59","date_gmt":"2018-10-21T17:22:59","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1115"},"modified":"2018-10-23T00:33:14","modified_gmt":"2018-10-23T00:33:14","slug":"bash-scripting-introduction-linuxadmin-io","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/21\/bash-scripting-introduction-linuxadmin-io\/","title":{"rendered":"Bash Scripting Introduction &#8211; LinuxAdmin.io"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/07\/bash_scripting_introduction.png\" alt=\"Bash Scripting Introduction\" width=\"667\" height=\"200\" \/><\/p>\n<p>Essentially bash scripting is just linking commands together. You can perform many tasks consistently using predefined scripts allowing them to become repeatable and standardized. If you are looking for more information you can check out the <a href=\"https:\/\/linux.die.net\/man\/1\/bash\">bash man page<\/a>.<\/p>\n<p>Each script should start with a \u201cshebang\u201d telling which environment to interpret the script, bash scripts start with<\/p>\n<p>#!\/bin\/bash<\/p>\n<p>In CentOS\/RHEL 7 , \/bin\/sh is symlinked to \/bin\/bash<\/p>\n<p>To make a script execute, you can either call it via the interpreter<\/p>\n<p>\/bin\/bash myscript.sh<\/p>\n<p>Or change the permissions to make it executable<\/p>\n<p>chmod ug+x myscript.sh<\/p>\n<p>and then execute it directly<\/p>\n<p>.\/myscript.sh<\/p>\n<h2>Bash Test Comparison Operators<\/h2>\n<p>Test comparison operators are use to compare two pieces of information.<\/p>\n<h3>String Tests:<\/h3>\n<p>Are used to compare to strings together<\/p>\n<p>$x = $y <em>\u2013 if $x is equal to $y, this will be true<\/em><br \/>\n$x != $y <em>\u2013 if $x is not equal to $y, this will be true<\/em><\/p>\n<h3>Integer Tests:<\/h3>\n<p>Integer tests are used to compare to integers together<\/p>\n<p>$x -eq $y<em> \u2013 if $x is equal to $y this will be true.<\/em><br \/>\n$x -ne $y <em>\u2013 if $x is not equal to $y, this will be true.<\/em><br \/>\n$x -ge $y<em> \u2013 if $x is greater than or equal to $y, this will be true.<\/em><br \/>\n$x -gt $y \u2013<em> if $x is greater than $y, this will be true.<\/em><br \/>\n$x -le $y<em> -if $x is less than or equal to $y, this will be true.<\/em><br \/>\n$x -lt $y <em>\u2013 if $x is less then $y, this will be true.<\/em><\/p>\n<h3>Bash Test Operators<\/h3>\n<p>Test operators are used to test if a condition is true.<\/p>\n<p>-d<em> FILE \u2013 True if file is a directory.<\/em><br \/>\n-e <em>FILE \u2013 True if file exists.<\/em><br \/>\n-f <em>FILE \u2013 True if exists and is a file.<\/em><br \/>\n-r <em>FILE \u2013 True if file exists and is readable.<\/em><br \/>\n-w <em>FILE \u2013 True if file exists and is granted write permissions.<\/em><br \/>\n-x <em>FILE \u2013 True if file exists and is granted execute permissions.<\/em><\/p>\n<h2>Bash If Statements<\/h2>\n<p>To utilize the above test operators, you will need to use \u2018if\u2019 statements<\/p>\n<p>To use a comparison:<\/p>\n<p>if [ $x -eq $y ]; then<br \/>\necho &#8220;x equals y&#8221;;<br \/>\nelse<br \/>\necho &#8220;x does not equal y&#8221;;<br \/>\nfi<\/p>\n<p>To use a test operator:<\/p>\n<p>if [ -d \/home ]; then<br \/>\necho &#8220;\/home exists&#8221;;<br \/>\nelse<br \/>\necho &#8220;\/home does not exist&#8221;;<br \/>\nfi<\/p>\n<h2>Bash Loops<\/h2>\n<h3>For Loops<\/h3>\n<p>Loops can be utilized to do the same task multiple times<\/p>\n<p>for i in ; do<br \/>\necho &#8220;I have a $&#8221;;<br \/>\ndone<\/p>\n<p>The output will be<\/p>\n<p>I have a apple<br \/>\nI have a orange<br \/>\nI have a watermelon<\/p>\n<h3>While Loops<\/h3>\n<p>These are done, while something is true perform the following task<\/p>\n<p>i=0<br \/>\nwhile [ $i -le 5 ]<br \/>\ndo<br \/>\necho &#8220;i is currently at $&#8221;;<br \/>\n((i++))<br \/>\ndone<\/p>\n<p>The output will be:<\/p>\n<p>i is currently at 0<br \/>\ni is currently at 1<br \/>\ni is currently at 2<br \/>\ni is currently at 3<br \/>\ni is currently at 4<br \/>\ni is currently at 5<\/p>\n<h2>Bash Script Arguments<\/h2>\n<p>You can use arguments to pass stored data into a script and store it as a variable. The first argument is stored as $1, the second argument is stored as $2 and so on.<\/p>\n<p>if [ $1 -eq $2 ]; then<br \/>\necho &#8220;The arguments are equal&#8221;;<br \/>\nelse<br \/>\necho &#8220;The arguments are not equal&#8221;<br \/>\nfi<\/p>\n<p>The output would be<\/p>\n<p>.\/compare_arguments 3 5<br \/>\nThe arguments are not equal<\/p>\n<p>.\/compare_arguments 4 4<br \/>\nThe arguments are equal<\/p>\n<p>You can also require a certain number of arguments be entered by using $#<\/p>\n<p>if [ $# -ne 2 ]; then<br \/>\necho &#8220;the number of required arguments is 2&#8221;;<br \/>\nexit;<br \/>\nfi<\/p>\n<p>Which indicates if the number of required arguments is not 2, then exit.<\/p>\n<p>Jul 16, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/bash-scripting-introduction\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Essentially bash scripting is just linking commands together. You can perform many tasks consistently using predefined scripts allowing them to become repeatable and standardized. If you are looking for more information you can check out the bash man page. Each script should start with a \u201cshebang\u201d telling which environment to interpret the script, bash scripts &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/21\/bash-scripting-introduction-linuxadmin-io\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Bash Scripting Introduction &#8211; LinuxAdmin.io&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1115","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1115"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1115\/revisions"}],"predecessor-version":[{"id":1326,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1115\/revisions\/1326"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}