{"id":196,"date":"2018-07-13T06:55:35","date_gmt":"2018-07-13T06:55:35","guid":{"rendered":"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=196"},"modified":"2019-05-16T07:28:37","modified_gmt":"2019-05-16T07:28:37","slug":"bash-shell-scripting-1","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/chapter\/bash-shell-scripting-1\/","title":{"rendered":"Bash Shell Scripting &#8211; 1"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/VwElbfiqVJU\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1. Motivation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Suppose we want to backup the work of few selected users at regular time intervals, we may need a sophisticated backup program that takes care of the requirement. Alternatively, we can write small scripts that can perform similar task. In Linux and UNIX, we can write scripts to automate tasks depending on the need. Such kind of programming is known as \u201cshell programming\u201d or \u201cshell scripting\u201d. Let us consider another example from academia. Every year a college enrolls 120 students, it is the job of system administrator to create 120 users in the server, create their profiles and assign separate passwords for each user. This mammoth task may require one full day, however, with the help of shell scripting it can be achieved in a matter of few minutes. The only requirement being, system administrator must be good in writing shell scripts. The following are few benefits of shell scripting:<\/p>\r\n\r\n<\/div>\r\na) Automate various tasks\r\n\r\nb) Provide a controlled user interface\r\n\r\nc) Reduce rate of errors\r\n\r\nd) Create new\/custom commands\r\n\r\ne) Share procedures among different users\r\n\r\nf) Combine repetitive tasks\/sequences by combining various commands.\r\n\r\n&nbsp;\r\n\r\nIn this module, we will learn how to write simple shell scripts. In subsequent modules we will deal with more features of shell scripting that can be used to automate various tasks.\r\n<ol start=\"2\">\r\n \t<li><strong> Introduction to Shell<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">As we have discussed in earlier modules, a shell is a command line interpreter which provides the user interface for terminal. Usually, shell interacts with the kernel on behalf of users. Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file. For instance, when we issue a command to create a file or directory, the shell will forward the request to Linux kernel to carry out the desired operation. As the kernel understands system calls, it will be difficult for a user to directly interact with kernel that is why, shell acts as an interpreter to interpret user\u2019s commands.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W1.png\" alt=\"\" width=\"367\" height=\"325\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: center;\"><strong>Figure: Role of Shell in executing User\u2019s commands<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Linux provides wide variety of shells. Depending on the type of work, a user has freedom to use a particular shell. Some widely used shells are bash, csh, ksh, sh,\u00a0<span style=\"text-align: initial; font-size: 1em;\">and tcsh. Each shell has its own syntax and has different features. For instance, those who are comfortable with \u2018C\u2019 programming syntax may prefer using C shell (csh). However, most of the users use <\/span><strong style=\"text-align: initial; font-size: 1em;\">bash<\/strong><span style=\"text-align: initial; font-size: 1em;\"> shell. We will focus on learning bash shell as it is widely used, apart from it, lot of books, good documentation and sample scripts are available online. A brief summary of various shells available with Linux is listed in following table:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W2.png\" alt=\"\" width=\"676\" height=\"293\" \/><\/p>\r\n&nbsp;\r\n\r\nLinux provides various shells. List of shells available in the system can be known by following command:\r\n\r\n<strong>$cat \/etc\/shells<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe default shell of command line interface can be known by following command:\r\n\r\n&nbsp;\r\n\r\n<strong>$echo $SHELL<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Usually shells are interactive, the shell accepts commands from user (via keyboard) and executes them. But in case if the user has to execute multiple commands in a sequence, then he\/she can store this sequence of command in some text file and let the shell execute this text file. Shell script is series of commands along with the programming language constructs like if statements, loop constructs etc. written in plain text file. Shell scripting is somewhat similar to batch programming of MS-DOS.<\/p>\r\n\r\n<ol start=\"3\">\r\n \t<li><strong> Creating first shell script<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">We will learn how to create bash scripts in this module. As discussed earlier, each shell has its own syntax. As such, there is no need to learn scripting for different shells. In this course, we will focus on bash shell scripting. Let us create a simple\u00a0script that displays two line messages on the screen. We can type the script in any editor like gedit, kwrite, vim, emacs etc., however, it is advisable to use either vim or emacs when the scripts become large. Create a file \u201csample.sh\u201d and type the following two lines:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-199 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W3.png\" alt=\"\" width=\"646\" height=\"50\" \/><\/p>\r\n&nbsp;\r\n\r\nThe output of above script is:\r\n\r\nToday is:05\/22\/15\r\n\r\nTo execute the above script, we can use either of the following alternatives:\r\n\r\na) Using bash Command : <strong>$bash sample.sh<\/strong>\r\n\r\nb) Make the sample.sh file executable using chmod command\r\n\r\n&nbsp;\r\n\r\n(chmod +x sample.sh) and then execute from the current directory indicated by dot (.) :\r\n\r\n<strong>$.\/sample.sh<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As we have discussed earlier, shell is an interpreter. Usually a shell program is a series of commands that are interpreted by the shell. That is why, shell programs are never compiled. As the html programs run in browser without compiling, similarly shell scripts are interpreted by the shell without any need of compilation. It must also be noted that the file extension \u201c.sh\u201d is not required. We can create a file named \u201csample\u201d and make it executable. However, for the sake of distinguishing the file as a shell program from other text files, it is a convention to use the extension of \u201c.sh\u201d to shell scripts.<\/p>\r\n\r\n<ol start=\"4\">\r\n \t<li><strong> Creating Interactive Scripts<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">Let us create another script that has more statements in the program. The following script is interactive that asks to enter the name and displays the same on the screen.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-200 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W4.png\" alt=\"\" width=\"666\" height=\"102\" \/><\/p>\r\n\r\n<div>\r\n\r\n\u00a0 \u00a0 The output of above script is:\r\n\r\nEnter your name:\r\n\r\nRichard Davis\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">You entered: Richard Davis<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify;\">In the above script, the hash-tag or number-sign (#) is used to start comments. Comments can be placed anywhere in the script. It is recommended to put the first line that indicates the path of the shell for which the script is written. The above script asks to enter the name; the text entered by user will be stored in a variable \u201cname\u201d using the read command. The last statement of the script displays the name entered by the user. To display the content of a variable, we must use \u2018$\u2019 sign prefixed with the variable name. Three important points that must be noted are:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">a) A variable name consists of alphabets, digits or underscore. The first character must be either an alphabet or an underscore.<\/p>\r\n<p style=\"text-align: justify;\">b) In shell scripting there is no need to mention type of variable like int, float, etc. In fact, all variables are treated as strings.<\/p>\r\n<p style=\"text-align: justify;\">c) To display the content of any variable, the variable name is preceded by $ sign.<\/p>\r\n<p style=\"text-align: justify;\">d) Variable names are case-sensitive.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Let us see how variables can be assigned values. We will modify the above script to make it non-interactive. The variables will be assigned values within the script. In the following script, we have used two variables, name and age. It is clear that both the variables are assigned some value. Although age is assigned the value 45, shell script will treat it like string. Curious learners may investigate by adding two lines (age=$age+1 and echo $age) to the below mentioned script:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W5.png\" alt=\"\" width=\"659\" height=\"105\" \/><\/p>\r\n&nbsp;\r\n\r\nThe output of above script is:\r\n\r\nName: Richard Davis\r\n\r\nAge: 45\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The following script is another example with a better formatted output. It also demonstrates combining multiple commands in a single file. In the following script, we have used $USER variable to display the user who executes the script, it displays the current date using echo statement. The \u2018-e\u2019 option with echo will display date in the same line, similarly the count of online users is displayed in the\u00a0same line of the message. In the later sections, we will explore how to store the count of users in a variable<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W6.png\" alt=\"\" width=\"651\" height=\"126\" \/><\/p>\r\n&nbsp;\r\n\r\nOutput of the above script:\r\n\r\nHello Richard\r\n\r\nToday is: 05\/22\/15\r\n\r\nNumber of online user: 20\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Note: If the shell is unable to understand a word as a variable, it will interpret it as a linux command which may lead to an error.<\/p>\r\n&nbsp;\r\n\r\n<strong>Exporting Variables<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">By default, the variables being used within a shell script has its scope upto the execution of that script. These variables cannot be accessed outside the script. Once the script terminates, content of these variables are lost. For instance, after the termination of the script that displays the name of a user, if we try to display the value of variable name, it may not show any value. All the variables declared in the shell script have their life-time till the script executes. Moreover, the variable declared in one shell cannot be used in another shell. The export command allows us to make the variable global so that it can be accessed in child processes of the existing shell. Let us experiment with the variables across different shells. By typing the following sequence of commands on shell prompt, we will be able appreciate the use of export command.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-203 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W7.png\" alt=\"\" width=\"650\" height=\"291\" \/><\/p>\r\n<p style=\"text-align: justify;\">In the above case, variable 'a' was a local variable. Once we start another shell (\/bin\/bash), the contents of variable 'a' disappeared. To make the contents of variable 'a' visible in the new shell, we need to export the variable. The syntax of exporting a variable can be:<\/p>\r\nexport variable_name=value\r\n\r\nOR\r\n\r\nvariable_name=value\r\n\r\nexport variable_name\r\n\r\nSo, we export the variable 'a' as follows:\r\n\r\n$export a\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Exported variables are copied into child processes. The values of exported variables can be modified by child process. Let us try to export the variable 'a' in above sequence. Let us experiment with the above sequence of execution after exporting the variables.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-204 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W8.png\" alt=\"\" width=\"662\" height=\"315\" \/><\/p>\r\nFrom the above execution, it might be clear that exporting a variable provides the copy of values to the child process.\r\n<ol start=\"5\">\r\n \t<li><strong> Functions<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">A function is a block of code that implements a set of operations. Usually, functions are helpful in executing the procedure multiple times. Functions are also called procedures or sub-routines. Using functions in shell scripts require two steps:<\/p>\r\n&nbsp;\r\n\r\na) Declaring a function\r\n\r\nb) Calling a function\r\n\r\n&nbsp;\r\n\r\nThe syntax of declaring a function is:\r\n\r\n&nbsp;\r\n\r\nfunction_name() {\r\n\r\ncommands\u2026.\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nFunctions are invoked by writing the name of function within shell script\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Let us create a function that displays fancy line made up of asterisks (*). First we have to declare the function with some name, let us assume the name of our user defined function is \u201cMyline\u201d. Within the shell script, we invoke the function in between the execution of commands. A script demonstrating the use of functions is given below:<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-205 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W9.png\" alt=\"\" width=\"653\" height=\"200\" \/><\/p>\r\n&nbsp;\r\n\r\nOutput of the above script is:\r\n\r\n&nbsp;\r\n\r\n************************************* Today is :Fri May 22 19:33:28 EDT 2015 ************************************* No. of online users: 20 *************************************\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">As seen in the above output, the function displays fancy lines wherever the function \u201cMyline\u201d was invoked. It must be noted that the function is called multiple times to perform the same procedure.<\/p>\r\n&nbsp;\r\n\r\n<strong>Passing Parameters to Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We can define a function which would accept parameters; these parameters are represented by $1, $2 and so on. The parameters passed to any function must follow the function call. Following is an example where we pass two parameters, \u201cRichard\u201d and \u201cDavis\u201d and then we capture and print them through the function.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"alignnone size-full wp-image-206\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W10.png\" alt=\"\" width=\"657\" height=\"146\" \/><\/p>\r\n&nbsp;\r\n\r\nOutput of the above script is:\r\n\r\nHello Richard Davis\r\n\r\nReturning values from functions\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We can return values from functions. The \u201creturn\u201d keyword is used within function body with some value that is to be returned. In the following script, we have returned the value 12 from function Hello. This returned value is captured by $? that follows the function call.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-207 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W11.png\" alt=\"\" width=\"663\" height=\"213\" \/><\/p>\r\n&nbsp;\r\n\r\nOutput of the above script is:\r\n\r\nHello Richard Davis\r\n\r\nReturn value is 12.\r\n\r\n&nbsp;\r\n\r\n<strong>Function calls within functions<\/strong>\r\n<p style=\"text-align: justify;\">In shell scripting, functions can call other functions or can call themselves from within the function body. In the following script, the \u201cfirst\u201d function is invoking the \u201csecond\u201d function.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-208 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w12.png\" alt=\"\" width=\"673\" height=\"254\" \/><\/p>\r\n&nbsp;\r\n\r\nOutput of the above script:\r\n\r\nThis is the first function...\r\n\r\nThis is now the second function...\r\n\r\n&nbsp;\r\n\r\nFunctions are an important feature of any programming language, they are generally used for validity checks like whether a file exists or not or to perform set of operations pertaining to any task.\r\n\r\n&nbsp;\r\n\r\n<strong>Built-In Shell Commands<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Shell scripts are used to execute sequence of commands and other types of statements like loop constructs, if statements etc. The commands that are executed by shell script can be divided into three categories:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">a) Built-In bash commands: Bash shell provides a set of commands that are executed by the shell itself. Various commands like cd, pwd, echo, read, let etc. are bash commands. A complete list of bash commands can be viewed by the command $man bash<\/p>\r\n<p style=\"text-align: justify;\">b) Compiled Applications: Various applications like rm, ls, df, vi etc. are present in the form of binary executable files. The shell script can invoke such type of compiled applications.<\/p>\r\n<p style=\"text-align: justify;\">c) Other scripts: A shell script can invoke other shell scripts provided there are adequate access privileges. Such kind of shell scripts behave as a custom command.<\/p>\r\n&nbsp;\r\n\r\n<strong>Command Substitution<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We may come across situations where there is a need of substituting the result of one command as a portion of another command. Suppose we want to display the statement in the following form: \u201cToday is \u2026day\u201d, here we need to execute the date command (date +%A) first and the output of this command will become portion of echo command.<\/p>\r\n&nbsp;\r\n\r\nCommand substitution can be done in two ways:\r\n\r\n&nbsp;\r\n\r\na) By enclosing the inner command with backticks\/grave accent ( ` )\r\n\r\nb) By enclosing the inner command in $( )\r\n\r\nFollowing are examples of command substitution:\r\n\r\n&nbsp;\r\n\r\n$echo \u201cToday is `date +%A` \u201d\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the above example, the date command is enclosed with backticks. Another way of using command substitution to get the same output is:<\/p>\r\n$echo \u201cToday is $(date +%A)\u201d\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Let us explore a shell script that uses command substitution. In the following script, we are storing the number of online users in a variable \u201ccnt\u201d. The value of variable cnt is displayed on screen.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1.png\" alt=\"\" width=\"649\" height=\"85\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">We must take care that no blank spaces must be left before or after the \u2018=\u2019 sign.<\/p>\r\n&nbsp;\r\n\r\n<strong>Script Parameters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Script parameters are often known as positional parameters or command line arguments. Using this facility, we can pass parameter values to a script. For instance, a script may require accepting the file names on command prompt as shown below:<\/p>\r\n&nbsp;\r\n\r\n$bash myscript.sh &lt;file1&gt; &lt;file2&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the above case, &lt;file1&gt; and &lt;file2&gt; are passed as command line arguments. These arguments can be accessed from within the script. Such kind of arguments are represented with a $ sign and a number. Following are some of the positional parameters used in shell programming:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-210 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1.png\" alt=\"\" width=\"660\" height=\"451\" \/><\/p>\r\n&nbsp;\r\n\r\nExecution and Output of the above script\r\n\r\nTo execute the script, type the bash command as given below:\r\n\r\n&nbsp;\r\n\r\n<strong>$ bash parameters.sh one two three four<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>The output will be as follows:<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe name of this program is: parameters.sh\r\n\r\nThe first argument passed from the command line is: one\r\n\r\nThe second argument passed from the command line is: two\r\n\r\nThe third argument passed from the command line is: three\r\n\r\nAll of the arguments passed from the command line are : one two three four\r\n\r\nAll arguments in another way : one two three four\r\n\r\nTotal number of arguments : 4\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">At the most 9 values can be displayed starting from $1 upto $9. A curious learner may experiment by passing 12-15 parameters and see what happens by displaying upto 15 variables using echo $15 command. Shell scripting provides functionality to use all the parameters passed. We can use the shift command to shift parameters by some specific number and display the later ones. By shifting the parameters we can display the values of script parameters from $10 and onward. To display the content of tenth parameter, we have to start from $1 after performing the shift operation. The syntax of shift command is:<\/p>\r\n&nbsp;\r\n\r\n$shift [number of parameters to shift]\r\n\r\n&nbsp;\r\n\r\n<strong>Features of Bourne Again Shell (BASH)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Bash being the GNU version of the standard bourne shell found in UNIX, it provides various facilities to help the programmers. It also incorporates popular features of csh, tcsh and ksh. Bash is the standard Linux shell, that gets loaded by default when most of the user accounts are created.<\/p>\r\n&nbsp;\r\n\r\nProgrammers and system administrators can harness the following techniques within shell scripts:\r\n\r\n&nbsp;\r\n\r\na) Support for Wildcard characters (meta characters)\r\n\r\nb) Support for I\/O redirection, Piping\r\n\r\nc) Support for Standard Error Redirection\r\n\r\nd) Command Grouping\r\n\r\ne) Command Separation\r\n\r\nf) Running processes in background\r\n\r\ng) Conditional execution using &amp;&amp; and || operators\r\n\r\nh) Support for command line arguments (positional parameters)\r\n\r\ni) Access to Environment variables\r\n\r\nj) Access to previous commands\r\n\r\nk) Command Substitution\r\n\r\nl) Command name abbreviation (aliasing)\r\n\r\n&nbsp;\r\n\r\n<strong>Keywords<\/strong>\r\n\r\n&nbsp;\r\n\r\nbash, csh, ksh, sh, tcsh, command substitution, positional parameters\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n\r\nLet us summarize the key concepts covered in this module\r\n<ul>\r\n \t<li>Shell is a command interpreter.<\/li>\r\n \t<li style=\"text-align: justify;\">Shell scripting includes series of commands and programming constructs to perform particular tasks.<\/li>\r\n \t<li style=\"text-align: justify;\">Variables can be used in shell scripts and can be made global using export command. However, variables are treated as strings.<\/li>\r\n \t<li style=\"text-align: justify;\">Shell scripting supports use of functions, command line arguments (script parameters) and command substitution.<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Bash Shell Scripting - 1<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/VwElbfiqVJU\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n\r\n[1]\u00a0 \u00a0S. Jain, <em>100 Shell Programs in Unix<\/em>. Pinnacle Technology, 2009.\r\n\r\n[2]\u00a0 M. Garrels, <em>Bash Guide for Beginners (Second Edition)<\/em>. Fultus Corporation, 2010.\r\n\r\n[3]\u00a0 Isrd, <em>Basics Of Os Unix And Shell Programming<\/em>. Tata McGraw-Hill Education, 2006.\r\n\r\n[4]\u00a0 P. Seebach, <em>Beginning Portable Shell Scripting: From Novice to Professional<\/em>. Apress, 2008.\r\n\r\n[5]\u00a0 E. Foster-Johnson, J. C.Welch, and M. Anderson, <em>Beginning Shell Scripting<\/em>. John Wiley &amp; Sons, 2007.\r\n\r\n[6]\u00a0 A. Robbins and N. H. F. Beebe, <em>Classic Shell Scripting: Hidden Commands that Unlock the<\/em> <em>Power of Unix<\/em>. O\u2019Reilly Media, Inc., 2005.\r\n\r\n[7]\u00a0 R. Peters, <em>Expert Shell Scripting<\/em>. Apress, 2009.\r\n\r\n[8]\u00a0 M. G. Venkateshmurthy, <em>Introduction to Unix and Shell Programming<\/em>. Pearson Education India, 2005.\r\n\r\n[9]\u00a0 G. Festari, <em>Learning Shell Scripting with Zsh<\/em>. Packt Publishing Ltd, 2014.\r\n\r\n[10]\u00a0 C. Newham, <em>Learning the bash Shell: Unix Shell Programming<\/em>. O\u2019Reilly Media, Inc.,\r\n\r\n2005.\r\n\r\n[11]\u00a0 B. Rosenblatt and A. Robbins, <em>Learning the Korn Shell<\/em>. O\u2019Reilly Media, Inc., 2002.\r\n\r\n[12] W. E. S. Jr, <em>Linux Command Line<\/em>. NO STARCH Press, 2012.\r\n\r\n[13] R. Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.\r\n\r\n[14] R. Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.\r\n\r\n[15] S. Lakshman, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2011.\r\n\r\n[16] S. Tushar, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2013.\r\n\r\n[17] K. O. Burtch, <em>Linux Shell Scripting with Bash<\/em>. Sams, 2004.\r\n\r\n[18] R. K. Michael, <em>Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for<\/em> <em>Programmers, System Administrators, and UNIX Gurus<\/em>. John Wiley &amp; Sons, 2011.\r\n\r\n[19] C. Johnson, <em>Pro Bash Programming: Scripting the Linux Shell<\/em>. Apress, 2009.\r\n\r\n[20] S. Veeraraghavan, <em>Sams Teach Yourself Shell Programming in 24 Hours<\/em>. Sams Publishing, 2002.\r\n\r\n[21] S. Parker, <em>Shell Scripting: Expert Recipes for Linux, Bash and more<\/em>. John Wiley &amp; Sons, 2011.\r\n\r\n[22] C. F. A. Johnson, <em>Shell Scripting Recipes: A Problem Solution Approach<\/em>. Dreamtech Press, 2007.\r\n\r\n[23] A. Verma, <em>Unix and Shell Programming<\/em>. Laxmi Publications, 2006.\r\n\r\n[24] B. A. Forouzan and R. F. Gilberg, <em>UNIX and Shell Programming: A Textbook<\/em>. Brooks\/Cole-Thomson Learning, 2003.\r\n\r\n[25] Y. P. Kanetkar, <em>Unix Shell Programming<\/em>. BPB Publications, 2002.\r\n\r\n[26] T. Sanchez-Clark, <em>Unix Shell Scripting Interview Questions, Answers, and Explanations:<\/em> <em>Unix Shell Certification Review<\/em>. Equity Press, 2007.\r\n\r\n[27] D. Taylor, <em>Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems<\/em>. No Starch Press, 2004.\r\n\r\n<\/div>\r\n<\/div>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/VwElbfiqVJU\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1. Motivation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Suppose we want to backup the work of few selected users at regular time intervals, we may need a sophisticated backup program that takes care of the requirement. Alternatively, we can write small scripts that can perform similar task. In Linux and UNIX, we can write scripts to automate tasks depending on the need. Such kind of programming is known as \u201cshell programming\u201d or \u201cshell scripting\u201d. Let us consider another example from academia. Every year a college enrolls 120 students, it is the job of system administrator to create 120 users in the server, create their profiles and assign separate passwords for each user. This mammoth task may require one full day, however, with the help of shell scripting it can be achieved in a matter of few minutes. The only requirement being, system administrator must be good in writing shell scripts. The following are few benefits of shell scripting:<\/p>\n<\/div>\n<p>a) Automate various tasks<\/p>\n<p>b) Provide a controlled user interface<\/p>\n<p>c) Reduce rate of errors<\/p>\n<p>d) Create new\/custom commands<\/p>\n<p>e) Share procedures among different users<\/p>\n<p>f) Combine repetitive tasks\/sequences by combining various commands.<\/p>\n<p>&nbsp;<\/p>\n<p>In this module, we will learn how to write simple shell scripts. In subsequent modules we will deal with more features of shell scripting that can be used to automate various tasks.<\/p>\n<ol start=\"2\">\n<li><strong> Introduction to Shell<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">As we have discussed in earlier modules, a shell is a command line interpreter which provides the user interface for terminal. Usually, shell interacts with the kernel on behalf of users. Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file. For instance, when we issue a command to create a file or directory, the shell will forward the request to Linux kernel to carry out the desired operation. As the kernel understands system calls, it will be difficult for a user to directly interact with kernel that is why, shell acts as an interpreter to interpret user\u2019s commands.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-197 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W1.png\" alt=\"\" width=\"367\" height=\"325\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W1.png 367w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W1-300x266.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W1-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W1-225x199.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W1-350x310.png 350w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/p>\n<div>\n<p style=\"text-align: center;\"><strong>Figure: Role of Shell in executing User\u2019s commands<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Linux provides wide variety of shells. Depending on the type of work, a user has freedom to use a particular shell. Some widely used shells are bash, csh, ksh, sh,\u00a0<span style=\"text-align: initial; font-size: 1em;\">and tcsh. Each shell has its own syntax and has different features. For instance, those who are comfortable with \u2018C\u2019 programming syntax may prefer using C shell (csh). However, most of the users use <\/span><strong style=\"text-align: initial; font-size: 1em;\">bash<\/strong><span style=\"text-align: initial; font-size: 1em;\"> shell. We will focus on learning bash shell as it is widely used, apart from it, lot of books, good documentation and sample scripts are available online. A brief summary of various shells available with Linux is listed in following table:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-198 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W2.png\" alt=\"\" width=\"676\" height=\"293\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W2.png 676w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W2-300x130.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W2-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W2-225x98.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W2-350x152.png 350w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Linux provides various shells. List of shells available in the system can be known by following command:<\/p>\n<p><strong>$cat \/etc\/shells<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The default shell of command line interface can be known by following command:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>$echo $SHELL<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Usually shells are interactive, the shell accepts commands from user (via keyboard) and executes them. But in case if the user has to execute multiple commands in a sequence, then he\/she can store this sequence of command in some text file and let the shell execute this text file. Shell script is series of commands along with the programming language constructs like if statements, loop constructs etc. written in plain text file. Shell scripting is somewhat similar to batch programming of MS-DOS.<\/p>\n<ol start=\"3\">\n<li><strong> Creating first shell script<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">We will learn how to create bash scripts in this module. As discussed earlier, each shell has its own syntax. As such, there is no need to learn scripting for different shells. In this course, we will focus on bash shell scripting. Let us create a simple\u00a0script that displays two line messages on the screen. We can type the script in any editor like gedit, kwrite, vim, emacs etc., however, it is advisable to use either vim or emacs when the scripts become large. Create a file \u201csample.sh\u201d and type the following two lines:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-199 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W3.png\" alt=\"\" width=\"646\" height=\"50\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W3.png 646w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W3-300x23.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W3-65x5.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W3-225x17.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W3-350x27.png 350w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The output of above script is:<\/p>\n<p>Today is:05\/22\/15<\/p>\n<p>To execute the above script, we can use either of the following alternatives:<\/p>\n<p>a) Using bash Command : <strong>$bash sample.sh<\/strong><\/p>\n<p>b) Make the sample.sh file executable using chmod command<\/p>\n<p>&nbsp;<\/p>\n<p>(chmod +x sample.sh) and then execute from the current directory indicated by dot (.) :<\/p>\n<p><strong>$.\/sample.sh<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As we have discussed earlier, shell is an interpreter. Usually a shell program is a series of commands that are interpreted by the shell. That is why, shell programs are never compiled. As the html programs run in browser without compiling, similarly shell scripts are interpreted by the shell without any need of compilation. It must also be noted that the file extension \u201c.sh\u201d is not required. We can create a file named \u201csample\u201d and make it executable. However, for the sake of distinguishing the file as a shell program from other text files, it is a convention to use the extension of \u201c.sh\u201d to shell scripts.<\/p>\n<ol start=\"4\">\n<li><strong> Creating Interactive Scripts<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Let us create another script that has more statements in the program. The following script is interactive that asks to enter the name and displays the same on the screen.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-200 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W4.png\" alt=\"\" width=\"666\" height=\"102\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W4.png 666w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W4-300x46.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W4-65x10.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W4-225x34.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W4-350x54.png 350w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/p>\n<div>\n<p>\u00a0 \u00a0 The output of above script is:<\/p>\n<p>Enter your name:<\/p>\n<p>Richard Davis<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">You entered: Richard Davis<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify;\">In the above script, the hash-tag or number-sign (#) is used to start comments. Comments can be placed anywhere in the script. It is recommended to put the first line that indicates the path of the shell for which the script is written. The above script asks to enter the name; the text entered by user will be stored in a variable \u201cname\u201d using the read command. The last statement of the script displays the name entered by the user. To display the content of a variable, we must use \u2018$\u2019 sign prefixed with the variable name. Three important points that must be noted are:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">a) A variable name consists of alphabets, digits or underscore. The first character must be either an alphabet or an underscore.<\/p>\n<p style=\"text-align: justify;\">b) In shell scripting there is no need to mention type of variable like int, float, etc. In fact, all variables are treated as strings.<\/p>\n<p style=\"text-align: justify;\">c) To display the content of any variable, the variable name is preceded by $ sign.<\/p>\n<p style=\"text-align: justify;\">d) Variable names are case-sensitive.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Let us see how variables can be assigned values. We will modify the above script to make it non-interactive. The variables will be assigned values within the script. In the following script, we have used two variables, name and age. It is clear that both the variables are assigned some value. Although age is assigned the value 45, shell script will treat it like string. Curious learners may investigate by adding two lines (age=$age+1 and echo $age) to the below mentioned script:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-201 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W5.png\" alt=\"\" width=\"659\" height=\"105\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W5.png 659w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W5-300x48.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W5-65x10.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W5-225x36.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W5-350x56.png 350w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The output of above script is:<\/p>\n<p>Name: Richard Davis<\/p>\n<p>Age: 45<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The following script is another example with a better formatted output. It also demonstrates combining multiple commands in a single file. In the following script, we have used $USER variable to display the user who executes the script, it displays the current date using echo statement. The \u2018-e\u2019 option with echo will display date in the same line, similarly the count of online users is displayed in the\u00a0same line of the message. In the later sections, we will explore how to store the count of users in a variable<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W6.png\" alt=\"\" width=\"651\" height=\"126\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W6.png 651w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W6-300x58.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W6-65x13.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W6-225x44.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W6-350x68.png 350w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Output of the above script:<\/p>\n<p>Hello Richard<\/p>\n<p>Today is: 05\/22\/15<\/p>\n<p>Number of online user: 20<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Note: If the shell is unable to understand a word as a variable, it will interpret it as a linux command which may lead to an error.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Exporting Variables<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">By default, the variables being used within a shell script has its scope upto the execution of that script. These variables cannot be accessed outside the script. Once the script terminates, content of these variables are lost. For instance, after the termination of the script that displays the name of a user, if we try to display the value of variable name, it may not show any value. All the variables declared in the shell script have their life-time till the script executes. Moreover, the variable declared in one shell cannot be used in another shell. The export command allows us to make the variable global so that it can be accessed in child processes of the existing shell. Let us experiment with the variables across different shells. By typing the following sequence of commands on shell prompt, we will be able appreciate the use of export command.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-203 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W7.png\" alt=\"\" width=\"650\" height=\"291\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W7.png 650w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W7-300x134.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W7-65x29.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W7-225x101.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W7-350x157.png 350w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p style=\"text-align: justify;\">In the above case, variable &#8216;a&#8217; was a local variable. Once we start another shell (\/bin\/bash), the contents of variable &#8216;a&#8217; disappeared. To make the contents of variable &#8216;a&#8217; visible in the new shell, we need to export the variable. The syntax of exporting a variable can be:<\/p>\n<p>export variable_name=value<\/p>\n<p>OR<\/p>\n<p>variable_name=value<\/p>\n<p>export variable_name<\/p>\n<p>So, we export the variable &#8216;a&#8217; as follows:<\/p>\n<p>$export a<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Exported variables are copied into child processes. The values of exported variables can be modified by child process. Let us try to export the variable &#8216;a&#8217; in above sequence. Let us experiment with the above sequence of execution after exporting the variables.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-204 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W8.png\" alt=\"\" width=\"662\" height=\"315\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W8.png 662w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W8-300x143.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W8-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W8-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W8-350x167.png 350w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/p>\n<p>From the above execution, it might be clear that exporting a variable provides the copy of values to the child process.<\/p>\n<ol start=\"5\">\n<li><strong> Functions<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">A function is a block of code that implements a set of operations. Usually, functions are helpful in executing the procedure multiple times. Functions are also called procedures or sub-routines. Using functions in shell scripts require two steps:<\/p>\n<p>&nbsp;<\/p>\n<p>a) Declaring a function<\/p>\n<p>b) Calling a function<\/p>\n<p>&nbsp;<\/p>\n<p>The syntax of declaring a function is:<\/p>\n<p>&nbsp;<\/p>\n<p>function_name() {<\/p>\n<p>commands\u2026.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Functions are invoked by writing the name of function within shell script<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Let us create a function that displays fancy line made up of asterisks (*). First we have to declare the function with some name, let us assume the name of our user defined function is \u201cMyline\u201d. Within the shell script, we invoke the function in between the execution of commands. A script demonstrating the use of functions is given below:<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-205 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W9.png\" alt=\"\" width=\"653\" height=\"200\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W9.png 653w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W9-300x92.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W9-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W9-225x69.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W9-350x107.png 350w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Output of the above script is:<\/p>\n<p>&nbsp;<\/p>\n<p>************************************* Today is :Fri May 22 19:33:28 EDT 2015 ************************************* No. of online users: 20 *************************************<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">As seen in the above output, the function displays fancy lines wherever the function \u201cMyline\u201d was invoked. It must be noted that the function is called multiple times to perform the same procedure.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Passing Parameters to Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We can define a function which would accept parameters; these parameters are represented by $1, $2 and so on. The parameters passed to any function must follow the function call. Following is an example where we pass two parameters, \u201cRichard\u201d and \u201cDavis\u201d and then we capture and print them through the function.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-206\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W10.png\" alt=\"\" width=\"657\" height=\"146\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W10.png 657w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W10-300x67.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W10-65x14.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W10-225x50.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W10-350x78.png 350w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Output of the above script is:<\/p>\n<p>Hello Richard Davis<\/p>\n<p>Returning values from functions<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We can return values from functions. The \u201creturn\u201d keyword is used within function body with some value that is to be returned. In the following script, we have returned the value 12 from function Hello. This returned value is captured by $? that follows the function call.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-207 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/W11.png\" alt=\"\" width=\"663\" height=\"213\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W11.png 663w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W11-300x96.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W11-65x21.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W11-225x72.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/W11-350x112.png 350w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Output of the above script is:<\/p>\n<p>Hello Richard Davis<\/p>\n<p>Return value is 12.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Function calls within functions<\/strong><\/p>\n<p style=\"text-align: justify;\">In shell scripting, functions can call other functions or can call themselves from within the function body. In the following script, the \u201cfirst\u201d function is invoking the \u201csecond\u201d function.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-208 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w12.png\" alt=\"\" width=\"673\" height=\"254\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12.png 673w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-300x113.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-225x85.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-350x132.png 350w\" sizes=\"auto, (max-width: 673px) 100vw, 673px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Output of the above script:<\/p>\n<p>This is the first function&#8230;<\/p>\n<p>This is now the second function&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>Functions are an important feature of any programming language, they are generally used for validity checks like whether a file exists or not or to perform set of operations pertaining to any task.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Built-In Shell Commands<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Shell scripts are used to execute sequence of commands and other types of statements like loop constructs, if statements etc. The commands that are executed by shell script can be divided into three categories:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">a) Built-In bash commands: Bash shell provides a set of commands that are executed by the shell itself. Various commands like cd, pwd, echo, read, let etc. are bash commands. A complete list of bash commands can be viewed by the command $man bash<\/p>\n<p style=\"text-align: justify;\">b) Compiled Applications: Various applications like rm, ls, df, vi etc. are present in the form of binary executable files. The shell script can invoke such type of compiled applications.<\/p>\n<p style=\"text-align: justify;\">c) Other scripts: A shell script can invoke other shell scripts provided there are adequate access privileges. Such kind of shell scripts behave as a custom command.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Command Substitution<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We may come across situations where there is a need of substituting the result of one command as a portion of another command. Suppose we want to display the statement in the following form: \u201cToday is \u2026day\u201d, here we need to execute the date command (date +%A) first and the output of this command will become portion of echo command.<\/p>\n<p>&nbsp;<\/p>\n<p>Command substitution can be done in two ways:<\/p>\n<p>&nbsp;<\/p>\n<p>a) By enclosing the inner command with backticks\/grave accent ( ` )<\/p>\n<p>b) By enclosing the inner command in $( )<\/p>\n<p>Following are examples of command substitution:<\/p>\n<p>&nbsp;<\/p>\n<p>$echo \u201cToday is `date +%A` \u201d<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the above example, the date command is enclosed with backticks. Another way of using command substitution to get the same output is:<\/p>\n<p>$echo \u201cToday is $(date +%A)\u201d<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Let us explore a shell script that uses command substitution. In the following script, we are storing the number of online users in a variable \u201ccnt\u201d. The value of variable cnt is displayed on screen.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1.png\" alt=\"\" width=\"649\" height=\"85\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1.png 649w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1-300x39.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1-65x9.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1-225x29.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w12-1-350x46.png 350w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">We must take care that no blank spaces must be left before or after the \u2018=\u2019 sign.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Script Parameters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Script parameters are often known as positional parameters or command line arguments. Using this facility, we can pass parameter values to a script. For instance, a script may require accepting the file names on command prompt as shown below:<\/p>\n<p>&nbsp;<\/p>\n<p>$bash myscript.sh &lt;file1&gt; &lt;file2&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the above case, &lt;file1&gt; and &lt;file2&gt; are passed as command line arguments. These arguments can be accessed from within the script. Such kind of arguments are represented with a $ sign and a number. Following are some of the positional parameters used in shell programming:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-210 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1.png\" alt=\"\" width=\"660\" height=\"451\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1.png 660w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1-300x205.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1-225x154.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/w13-1-350x239.png 350w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Execution and Output of the above script<\/p>\n<p>To execute the script, type the bash command as given below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>$ bash parameters.sh one two three four<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>The output will be as follows:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The name of this program is: parameters.sh<\/p>\n<p>The first argument passed from the command line is: one<\/p>\n<p>The second argument passed from the command line is: two<\/p>\n<p>The third argument passed from the command line is: three<\/p>\n<p>All of the arguments passed from the command line are : one two three four<\/p>\n<p>All arguments in another way : one two three four<\/p>\n<p>Total number of arguments : 4<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">At the most 9 values can be displayed starting from $1 upto $9. A curious learner may experiment by passing 12-15 parameters and see what happens by displaying upto 15 variables using echo $15 command. Shell scripting provides functionality to use all the parameters passed. We can use the shift command to shift parameters by some specific number and display the later ones. By shifting the parameters we can display the values of script parameters from $10 and onward. To display the content of tenth parameter, we have to start from $1 after performing the shift operation. The syntax of shift command is:<\/p>\n<p>&nbsp;<\/p>\n<p>$shift [number of parameters to shift]<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Features of Bourne Again Shell (BASH)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Bash being the GNU version of the standard bourne shell found in UNIX, it provides various facilities to help the programmers. It also incorporates popular features of csh, tcsh and ksh. Bash is the standard Linux shell, that gets loaded by default when most of the user accounts are created.<\/p>\n<p>&nbsp;<\/p>\n<p>Programmers and system administrators can harness the following techniques within shell scripts:<\/p>\n<p>&nbsp;<\/p>\n<p>a) Support for Wildcard characters (meta characters)<\/p>\n<p>b) Support for I\/O redirection, Piping<\/p>\n<p>c) Support for Standard Error Redirection<\/p>\n<p>d) Command Grouping<\/p>\n<p>e) Command Separation<\/p>\n<p>f) Running processes in background<\/p>\n<p>g) Conditional execution using &amp;&amp; and || operators<\/p>\n<p>h) Support for command line arguments (positional parameters)<\/p>\n<p>i) Access to Environment variables<\/p>\n<p>j) Access to previous commands<\/p>\n<p>k) Command Substitution<\/p>\n<p>l) Command name abbreviation (aliasing)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Keywords<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>bash, csh, ksh, sh, tcsh, command substitution, positional parameters<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Let us summarize the key concepts covered in this module<\/p>\n<ul>\n<li>Shell is a command interpreter.<\/li>\n<li style=\"text-align: justify;\">Shell scripting includes series of commands and programming constructs to perform particular tasks.<\/li>\n<li style=\"text-align: justify;\">Variables can be used in shell scripts and can be made global using export command. However, variables are treated as strings.<\/li>\n<li style=\"text-align: justify;\">Shell scripting supports use of functions, command line arguments (script parameters) and command substitution.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Bash Shell Scripting &#8211; 1<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/VwElbfiqVJU\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>[1]\u00a0 \u00a0S. Jain, <em>100 Shell Programs in Unix<\/em>. Pinnacle Technology, 2009.<\/p>\n<p>[2]\u00a0 M. Garrels, <em>Bash Guide for Beginners (Second Edition)<\/em>. Fultus Corporation, 2010.<\/p>\n<p>[3]\u00a0 Isrd, <em>Basics Of Os Unix And Shell Programming<\/em>. Tata McGraw-Hill Education, 2006.<\/p>\n<p>[4]\u00a0 P. Seebach, <em>Beginning Portable Shell Scripting: From Novice to Professional<\/em>. Apress, 2008.<\/p>\n<p>[5]\u00a0 E. Foster-Johnson, J. C.Welch, and M. Anderson, <em>Beginning Shell Scripting<\/em>. John Wiley &amp; Sons, 2007.<\/p>\n<p>[6]\u00a0 A. Robbins and N. H. F. Beebe, <em>Classic Shell Scripting: Hidden Commands that Unlock the<\/em> <em>Power of Unix<\/em>. O\u2019Reilly Media, Inc., 2005.<\/p>\n<p>[7]\u00a0 R. Peters, <em>Expert Shell Scripting<\/em>. Apress, 2009.<\/p>\n<p>[8]\u00a0 M. G. Venkateshmurthy, <em>Introduction to Unix and Shell Programming<\/em>. Pearson Education India, 2005.<\/p>\n<p>[9]\u00a0 G. Festari, <em>Learning Shell Scripting with Zsh<\/em>. Packt Publishing Ltd, 2014.<\/p>\n<p>[10]\u00a0 C. Newham, <em>Learning the bash Shell: Unix Shell Programming<\/em>. O\u2019Reilly Media, Inc.,<\/p>\n<p>2005.<\/p>\n<p>[11]\u00a0 B. Rosenblatt and A. Robbins, <em>Learning the Korn Shell<\/em>. O\u2019Reilly Media, Inc., 2002.<\/p>\n<p>[12] W. E. S. Jr, <em>Linux Command Line<\/em>. NO STARCH Press, 2012.<\/p>\n<p>[13] R. Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.<\/p>\n<p>[14] R. Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.<\/p>\n<p>[15] S. Lakshman, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2011.<\/p>\n<p>[16] S. Tushar, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2013.<\/p>\n<p>[17] K. O. Burtch, <em>Linux Shell Scripting with Bash<\/em>. Sams, 2004.<\/p>\n<p>[18] R. K. Michael, <em>Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for<\/em> <em>Programmers, System Administrators, and UNIX Gurus<\/em>. John Wiley &amp; Sons, 2011.<\/p>\n<p>[19] C. Johnson, <em>Pro Bash Programming: Scripting the Linux Shell<\/em>. Apress, 2009.<\/p>\n<p>[20] S. Veeraraghavan, <em>Sams Teach Yourself Shell Programming in 24 Hours<\/em>. Sams Publishing, 2002.<\/p>\n<p>[21] S. Parker, <em>Shell Scripting: Expert Recipes for Linux, Bash and more<\/em>. John Wiley &amp; Sons, 2011.<\/p>\n<p>[22] C. F. A. Johnson, <em>Shell Scripting Recipes: A Problem Solution Approach<\/em>. Dreamtech Press, 2007.<\/p>\n<p>[23] A. Verma, <em>Unix and Shell Programming<\/em>. Laxmi Publications, 2006.<\/p>\n<p>[24] B. A. Forouzan and R. F. Gilberg, <em>UNIX and Shell Programming: A Textbook<\/em>. Brooks\/Cole-Thomson Learning, 2003.<\/p>\n<p>[25] Y. P. Kanetkar, <em>Unix Shell Programming<\/em>. BPB Publications, 2002.<\/p>\n<p>[26] T. Sanchez-Clark, <em>Unix Shell Scripting Interview Questions, Answers, and Explanations:<\/em> <em>Unix Shell Certification Review<\/em>. Equity Press, 2007.<\/p>\n<p>[27] D. Taylor, <em>Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems<\/em>. No Starch Press, 2004.<\/p>\n<\/div>\n<\/div>\n","protected":false},"author":4,"menu_order":13,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-hardik-joshi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-196","chapter","type-chapter","status-publish","hentry","contributor-mr-hardik-joshi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/196\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/196\/revisions\/689"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/196\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapter-type?post=196"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/contributor?post=196"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/license?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}