{"id":710,"date":"2022-02-21T15:04:56","date_gmt":"2022-02-21T20:04:56","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=710"},"modified":"2022-02-21T15:04:58","modified_gmt":"2022-02-21T20:04:58","slug":"bash-shell-scripting","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/bash-shell-scripting\/","title":{"rendered":"bash shell scripting"},"content":{"rendered":"\n<p>A shell script is a code (script) written for the shell or command line interpreter of an operating system such as Unix. It allows users to automate tasks which would be cumbersome to execute manually. For example renaming 2000 files. You could do it by 2000 mv commands or write up a 3 line shell script to do it for you.<\/p>\n\n\n\n<p>A shell script can be written in any shell scripting language installed on the system. The most widely used shell is the Bash Shell. Others include csh, ksh, tcsh, etc. Here we would only be discussing bash shell.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"getting-started\">Getting Started<\/h3>\n\n\n\n<p><strong>Which shell I am running?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo $SHELL<\/code><\/pre>\n\n\n\n<p>This command would print the name of the shell you are running.<\/p>\n\n\n\n<p><strong>How do I switch to Bash Shell?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash<\/code><\/pre>\n\n\n\n<p><strong>Which shells are installed on my system?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/etc\/shells<\/code><\/pre>\n\n\n\n<p><strong>First Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clear; echo \"This months calendar\"; cal;<\/code><\/pre>\n\n\n\n<p>A shell can script can be run on commandline from a file. You can use any text editor which doesn&#8217;t insert formatting into text to write a shell script. vi and emacs are good editors. Save the following code in file called test.sh:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clear\necho \"This year's calendar\"\ncal -y<\/code><\/pre>\n\n\n\n<p>By convention, shell scripts are saved with .sh extension. Change permissions of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod 755 test.sh<\/code><\/pre>\n\n\n\n<p>You can use any one of the following commands to execute your bash script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash test.sh\nsh test.sh\n.\/test.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bash-variables\">Bash Variables<\/h3>\n\n\n\n<p>Bash uses two kinds of variables<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>System Variables:<\/strong>&nbsp;Variables pertaining to the Unix system<\/li><li><strong>User Defined Variables:<\/strong>&nbsp;Variables created by users for scripting<\/li><\/ul>\n\n\n\n<p>To see a list of system variables type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set<\/code><\/pre>\n\n\n\n<p>This would give you a list of system variables. DO NOT change values of any of these variable unless you know what you are doing. Unlike Microsoft windows, Linux allows you more control over you system assuming that you know what you are doing. So, if the root granted you a rather unrestrictive access, you can create many problems for yourself.<\/p>\n\n\n\n<p>To create a user defined variable &#8216;number&#8217;, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number=10<\/code><\/pre>\n\n\n\n<p>You can use alphanumeric characters and underscore to for variable names. Variable names are case-sensitive. You many not use spaces in assignment. Following are not allowed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>number =10\nnumber= 10\nnumber = 10<\/code><\/pre>\n\n\n\n<p>To assign NULL values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>budget=NULL\nbudget=NULL<\/code><\/pre>\n\n\n\n<p>To print variables<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo $number<\/code><\/pre>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>year=\"427 BC\"\nname=Plato\ncity=Athens\necho \"$name was born in $city around $year\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"renaming-files-in-directory\">Renaming Files in Directory<\/h3>\n\n\n\n<p>Suppose you have 2000 photos in a directory and you need to rename them. The files are named as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IMG_1232.JPG\nSP234324.JPG\nwebdown.JPG\n...<\/code><\/pre>\n\n\n\n<p>We need to rename them as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>salzburg_1.jpg\nsalzburg_2.jpg\n...<\/code><\/pre>\n\n\n\n<p>Here is how:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>j=1\ns=salzburg_\nfor f in *.JPG; do\n  mv $f $s$j.jpg\n  j=(($j+1))\n  echo renaming $f to $s$j.jpg\ndone<\/code><\/pre>\n\n\n\n<p>j in is the counter. Line 5 increments j. s is a string. Line 4 uses the mv command renaming each file to the desired combination of text and number. Line 6 prints a message on the command line. Note that in line 3, we only loop through files ending in .JPG.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"substring\">Substring<\/h3>\n\n\n\n<p>The following code show how to get a substring:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\na='Freundschaftsbezeugung'\necho $a\necho ${a:0:6} \necho ${a:6:7}\nb=${a:13:9}\necho $b<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Freundschaftsbezeugung\nFreund\nschafts\nbezeugung<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A shell script is a code (script) written for the shell or command line interpreter of an operating system such as Unix. It allows users to automate tasks which would be cumbersome to execute manually. For example renaming 2000 files. You could do it by 2000 mv commands or write up a 3 line shell [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,34],"tags":[94,35],"class_list":["post-710","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","tag-bash","tag-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=710"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/710\/revisions\/711"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}