{"id":235,"date":"2018-07-13T07:42:31","date_gmt":"2018-07-13T07:42:31","guid":{"rendered":"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=235"},"modified":"2019-05-16T08:24:26","modified_gmt":"2019-05-16T08:24:26","slug":"bash-shell-scripting-3","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/chapter\/bash-shell-scripting-3\/","title":{"rendered":"Bash Shell Scripting &#8211; 3"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/LinDuYGArtM\" 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<ol>\r\n \t<li><b>Formatting\u00a0output using echo and tput commands in Shell Scripting\u00a0<\/b><strong style=\"text-align: initial; font-size: 1em;\">echo command<\/strong><\/li>\r\n<\/ol>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the previous modules, we have used echo in the simplest form. The echo command supports escape sequences to manipulate the output of shell scripts. For instance, we can change the color of text, make it bold or underlined and also insert tab space or newline using echo. Let us go through certain tricks that can be applied while using echo command within shell script. Table 1 lists various options available with echo command and its brief description. While using these options, -e must be used.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-236 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2.png\" alt=\"\" width=\"412\" height=\"250\" \/><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify;\">The following is an example command followed by its output that uses backspace, tab and newline with echo:<\/p>\r\n$echo -e \"A quick brown\\b\\b\\b fox jumps \\t\\t over the lazy \\n dog\"\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">Output:<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">A quick br fox jumps\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">dog\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">over the lazy<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify;\">Special escape sequences can be used with echo command to display text in different colours or to apply formatting to the output text. The following script demonstrates the use of special sequence characters to format the output text. The escape sequences are stored in meaningful variable names. For instance, to make the text underlined, we have used UNDERLINE='\\033[4m'. Here, '\\033[4m' indicates the escape sequence used for underlining the output.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-237 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1.png\" alt=\"\" width=\"703\" height=\"308\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>output command<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Apart from echo, there is a tput command that can control the way the output is displayed on the screen. The tput command is designed to work for different terminal types of Unix and Linux systems. Few options that are available with tput command are listed in table 2<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-238 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1.png\" alt=\"\" width=\"430\" height=\"314\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The following shell script demonstrates the use of tput command with various options. Such options will be helpful when we want to create menu driven scripts that are interactive and which requires the formatting to highlight parts of screen. It is required to use tput sgr0 occasionally to restore the default screen.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-239 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1.png\" alt=\"\" width=\"693\" height=\"379\" \/><\/p>\r\n\r\n<ol start=\"2\">\r\n \t<li><strong> Debugging Shell scripts and dealing with errors<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">At times, while working with scripts and commands, we may face runtime errors. These may be due to errors in the script, such as an incorrect syntax, or missing file or insufficient permission to do an operation. In most of the cases, the errors may be reported with a specific error code or some message, but often just yield incorrect or confusing output. In shell scripting, there are ways and means to identify whether a command has executed successfully or not. The process of identifying errors while executing the shell script is known as debugging.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Debugging helps us to troubleshoot and resolve errors, it is one of the most important task that is usually performed by programmers and system administrators.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">There are two popular ways to debug in shell scripting. They are as follows:<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify;\">Executing bash script in debug mode:We can execute a bash script in debug mode by issuing the bash command as: $bash -x .\/script_filename.sh<\/li>\r\n \t<li style=\"text-align: justify;\">Bracketing part of script using \u201cset\u201d command:Within the script, we can bracket the parts of the script with \u201cset -x\u201d and \u201cset +x\u201d that are to be debugged.The above technqiue helps to debug because it prefixes each command with the <strong style=\"text-align: initial; font-size: 1em;\">\u2018<\/strong><span style=\"text-align: initial; font-size: 1em;\">+\u2019 character that is within the script, it displays each command before executing it and it\u00a0<\/span>can\u00a0\u00a0 debug\u00a0\u00a0 only\u00a0\u00a0 selected\u00a0\u00a0 block\u00a0\u00a0\u00a0 of\u00a0\u00a0 a\u00a0\u00a0\u00a0 script\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 which\u00a0\u00a0\u00a0 can\u00a0\u00a0 be\u00a0\u00a0\u00a0 as\u00a0\u00a0\u00a0\u00a0\u00a0 follows:<\/li>\r\n<\/ul>\r\nset -x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # debugging is turned on...\r\n\r\nset +x\u00a0\u00a0\u00a0\u00a0\u00a0 # debugging is turned off\r\n\r\n&nbsp;\r\n\r\nThe following is a sample shell script that uses debugging mode that uses bracketing technique.\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-240 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5.png\" alt=\"\" width=\"694\" height=\"196\" \/><\/p>\r\n&nbsp;\r\n\r\nIn Linux and Unix, all programs that run are given three open file streams as listed in table 3:\r\n\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/6.png\" alt=\"\" width=\"682\" height=\"172\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Using redirection, we can save the stdout and stderr output streams or files for later analysis after a program or command is executed. Let us take an example of script that may run into errors. The following script may enter into a runtime error if the file with .java extension is not present in the current directory. Let us presume that the script generates error.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/7.png\" alt=\"\" width=\"695\" height=\"174\" \/><\/p>\r\n&nbsp;\r\n\r\nUnder such circumstances, the program can be executed as shown below:\r\n\r\n$.\/script_filename.sh 2&gt; error.txt\r\n\r\nIn the presence of any error, the error message will be stored in the file error.txt that can be displayed afterwards using the following command:\r\n\r\n$cat error.txt\r\n<ol start=\"3\">\r\n \t<li><strong> Discarding Output with <\/strong><strong>\/dev\/null<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">Certain commands like find or grep will produce huge amount of output, such outputs can overwhelm the console. Such commands may create problems (by producing output that is not required) while executing scripts, if they are used within the scripts. To avoid this, we can redirect the unwanted output to a special file (a device node) called \/dev\/null. This pseudofile is also called the bit bucket or black hole.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The following script is an example where we discard unwanted output to the \/dev\/null file while the program is executing. In the following script, user is asked to enter the name of friend(user-id) whom he wants to check whether the user has logged in or not. The script can run in background till the user wants to keep on checking whether the friend has logged-in or not in that time frame. In this script, the grep command may produce unnecessary output after each interval of one minute. So, to hide this output, we redirect it into the \/dev\/null file.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1.png\" alt=\"\" width=\"695\" height=\"349\" \/><\/p>\r\n\r\n<ol start=\"4\">\r\n \t<li><strong> Reading from files<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify;\">At times, it is required to process files from the shell scripts. Online transaction systems or billing systems usually process sequence of records from files. Small shell scripts can be helpful in processing voluminous amount of data and such scripts can be created within minutes. Let us explore few ways of reading files line by line from shell scripts. Although there are many different methods to read files, we will explore few of them which are relatively simple.<\/p>\r\n&nbsp;\r\n\r\n<strong>Method 1: Piped while-read loop<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the following shell script, the while loop accepts output of the command cat $FILENAME and processes each line, the program calculates number of lines of the file<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2.png\" alt=\"\" width=\"708\" height=\"202\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Method 2: Redirected \"while-read\" loop<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the following shell script, the while loop accepts input from the file using the statement done &lt; $FILENAME, each line of the file is read using while loop. This script counts the number of lines of the given file.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"alignnone size-full wp-image-245\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1.png\" alt=\"\" width=\"704\" height=\"203\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Method <\/strong><strong>3<\/strong><strong>: <\/strong><strong>Using file descriptors<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">exec is a <a href=\"https:\/\/www.computerhope.com\/jargon\/s\/shell.htm\">shell <\/a>command that replaces the current shell process with the command specified after exec. The first exec command redirects stdin to file descriptor 3. The use\u00a0of second exec command redirects the $FILENAME into stdin, which is indicated by the file descriptor 0. When the while loop executes, the loop takes input from the file and reads into the LINE variable. When the while loop exits, to restore the file descriptors, the exec restores it back to the original file descriptor 0.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1.png\" alt=\"\" width=\"690\" height=\"302\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Method 4: Using combination of head and tail commands<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Use of head and tail commands can be combined to point to a specific line from a given file. These commands when used within loop can help in reading entire file as shown in the following script.<\/p>\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/11.png\" alt=\"\" width=\"692\" height=\"235\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>5.Using arrays in Shell Scripting<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">An array is a variable containing multiple values that may be of same type or of different type. In Linux, there is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. An array index starts with zero. Let us explore how to use arrays within shell scripts.<\/p>\r\n&nbsp;\r\n\r\n<strong>Declaring an Array and Assigning values<\/strong>\r\n\r\n&nbsp;\r\n\r\nIn bash, array is created automatically when a variable is used in the format as shown below:\r\n\r\narray_name[index] = value.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">In the above format, the name can be any variable name, index values must be greater than or equal to zero, values that are stored in an array usually takes string format. To access an element of an array, the syntax uses curly brackets like ${name[index]}.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-248 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/12.png\" alt=\"\" width=\"699\" height=\"227\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Instead of initializing each element of an array separately, we can declare and initialize an array by specifying the list of elements (separated by white space) within curly braces. The syntax of the same is as follows:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">declare -a array_name=(element1 element2 element3)<\/p>\r\n<p style=\"text-align: justify;\">While declaring the array and initializing with strings, if the string (elements) has white sace character, we must enclose it with quotes.<\/p>\r\n<p style=\"text-align: justify;\">To print the whole array, following syntax is used:<\/p>\r\n<p style=\"text-align: justify;\">echo ${array_name[@]}<\/p>\r\n<p style=\"text-align: justify;\">To display the length of an array, the following syntax is used:<\/p>\r\n<p style=\"text-align: justify;\">echo ${#array_name[@]}<\/p>\r\n<p style=\"text-align: justify;\">To display element at index position\u00a0 the following syntax is used:<\/p>\r\n<p style=\"text-align: justify;\">echo ${array_name[2]}<\/p>\r\n<p style=\"text-align: justify;\">To display the length of an element at index position 2, the following syntax is used:<\/p>\r\n<p style=\"text-align: justify;\">echo ${#array_name[2]}<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The following shell script illustrates the use of array, it initializes the array with 4 elements, further we display the entire array, length of the array, display the element at position 3 and to display the length of element at position 3 of that array.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center;\"><img class=\"size-full wp-image-249 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/13.png\" alt=\"\" width=\"692\" height=\"303\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Keywords:<\/strong>\r\n\r\narray, debugging, file descriptors, stdin, stdout, stderr commands: tput, echo, exec\r\n\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Producing effective output using options of echo and tput command<\/li>\r\n \t<li>Debugging of shell scripts using set command<\/li>\r\n \t<li style=\"text-align: justify;\">Reading files line by line using different methods like redirection of file descriptors, combination of head and tail commands and inputting file to while loop<\/li>\r\n \t<li>using arrays in shell scripting<\/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 - 3<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/LinDuYGArtM\" 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\r\n\r\n<strong>References:<\/strong>\r\n\r\n&nbsp;\r\n\r\n[1] Jain, <em>100 Shell Programs in Unix<\/em>. Pinnacle Technology, 2009.\r\n\r\n[2] Garrels, <em>Bash Guide for Beginners (Second Edition)<\/em>. Fultus Corporation, 2010.\r\n\r\n[3] Isrd, <em>Basics Of Os Unix And Shell Programming<\/em>. Tata McGraw-Hill Education, 2006.\r\n\r\n[4] Seebach, <em>Beginning Portable Shell Scripting: From Novice to Professional<\/em>. Apress, 2008.\r\n\r\n[5] Foster-Johnson, J. C.Welch, and M. Anderson, <em>Beginning Shell Scripting<\/em>. John Wiley &amp; Sons, 2007.\r\n\r\n[6] 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] Peters, <em>Expert Shell Scripting<\/em>. Apress, 2009.\r\n\r\n[8] G. Venkateshmurthy, <em>Introduction to Unix and Shell Programming<\/em>. Pearson Education India, 2005.\r\n\r\n[9] Fesari, <em>Learning Shell Scripting with Zsh<\/em>. Packt Publishing Ltd, 2014.\r\n\r\n[10] Newham, <em>Learning the bash Shell: Unix Shell Programming<\/em>. O\u2019Reilly Media, Inc.,2005.\r\n\r\n[11] Rosenblatt and A. Robbins, <em>Learning the Korn Shell<\/em>. O\u2019Reilly Media, Inc., 2002.\r\n\r\n[12] E. S. Jr, <em>Linux Command Line<\/em>. NO STARCH Press, 2012.\r\n\r\n[13] Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.\r\n\r\n[14] Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.\r\n\r\n[15] Lakshman, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2011.\r\n\r\n[16] Tushar, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2013.\r\n\r\n[17] O. Burtch, <em>Linux Shell Scripting with Bash<\/em>. Sams, 2004.\r\n\r\n[18] 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] Johnson, <em>Pro Bash Programming: Scripting the Linux Shell<\/em>. Apress, 2009.\r\n\r\n[20] Veeraraghavan, <em>Sams Teach Yourself Shell Programming in 24 Hours<\/em>. Sams Publishing, 2002.\r\n\r\n[21] Parker, <em>Shell Scripting: Expert Recipes for Linux, Bash and more<\/em>. John Wiley &amp; Sons, 2011.\r\n\r\n[22] F. A. Johnson, <em>Shell Scripting Recipes: A Problem Solution Approach<\/em>. Dreamtech Press, 2007.\r\n\r\n[23] Verma, <em>Unix and Shell Programming<\/em>. Laxmi Publications, 2006.\r\n\r\n[24] A. Forouzan and R. F. Gilberg, <em>UNIX and Shell Programming: A Textbook<\/em>. Brooks\/Cole-Thomson Learning, 2003.\r\n\r\n[25] P. Kanetkar, <em>Unix Shell Programming<\/em>. BPB Publications, 2002.\r\n\r\n[26] 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] Taylor, <em>Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems<\/em>. No Starch Press, 2004.","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/LinDuYGArtM\" 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<ol>\n<li><b>Formatting\u00a0output using echo and tput commands in Shell Scripting\u00a0<\/b><strong style=\"text-align: initial; font-size: 1em;\">echo command<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the previous modules, we have used echo in the simplest form. The echo command supports escape sequences to manipulate the output of shell scripts. For instance, we can change the color of text, make it bold or underlined and also insert tab space or newline using echo. Let us go through certain tricks that can be applied while using echo command within shell script. Table 1 lists various options available with echo command and its brief description. While using these options, -e must be used.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-236 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2.png\" alt=\"\" width=\"412\" height=\"250\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2.png 412w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2-300x182.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2-225x137.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/1-2-350x212.png 350w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/p>\n<div>\n<p style=\"text-align: justify;\">The following is an example command followed by its output that uses backspace, tab and newline with echo:<\/p>\n<p>$echo -e &#8220;A quick brown\\b\\b\\b fox jumps \\t\\t over the lazy \\n dog&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">Output:<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">A quick br fox jumps\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">dog\u00a0<\/span><span style=\"text-align: initial; font-size: 1em;\">over the lazy<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify;\">Special escape sequences can be used with echo command to display text in different colours or to apply formatting to the output text. The following script demonstrates the use of special sequence characters to format the output text. The escape sequences are stored in meaningful variable names. For instance, to make the text underlined, we have used UNDERLINE=&#8217;\\033[4m&#8217;. Here, &#8216;\\033[4m&#8217; indicates the escape sequence used for underlining the output.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-237 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1.png\" alt=\"\" width=\"703\" height=\"308\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1.png 703w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1-225x99.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/2-1-350x153.png 350w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>output command<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Apart from echo, there is a tput command that can control the way the output is displayed on the screen. The tput command is designed to work for different terminal types of Unix and Linux systems. Few options that are available with tput command are listed in table 2<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-238 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1.png\" alt=\"\" width=\"430\" height=\"314\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1.png 430w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1-300x219.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1-225x164.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/3-1-350x256.png 350w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The following shell script demonstrates the use of tput command with various options. Such options will be helpful when we want to create menu driven scripts that are interactive and which requires the formatting to highlight parts of screen. It is required to use tput sgr0 occasionally to restore the default screen.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-239 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1.png\" alt=\"\" width=\"693\" height=\"379\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1.png 693w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1-300x164.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1-225x123.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/4-1-350x191.png 350w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/p>\n<ol start=\"2\">\n<li><strong> Debugging Shell scripts and dealing with errors<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">At times, while working with scripts and commands, we may face runtime errors. These may be due to errors in the script, such as an incorrect syntax, or missing file or insufficient permission to do an operation. In most of the cases, the errors may be reported with a specific error code or some message, but often just yield incorrect or confusing output. In shell scripting, there are ways and means to identify whether a command has executed successfully or not. The process of identifying errors while executing the shell script is known as debugging.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Debugging helps us to troubleshoot and resolve errors, it is one of the most important task that is usually performed by programmers and system administrators.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">There are two popular ways to debug in shell scripting. They are as follows:<\/p>\n<ul>\n<li style=\"text-align: justify;\">Executing bash script in debug mode:We can execute a bash script in debug mode by issuing the bash command as: $bash -x .\/script_filename.sh<\/li>\n<li style=\"text-align: justify;\">Bracketing part of script using \u201cset\u201d command:Within the script, we can bracket the parts of the script with \u201cset -x\u201d and \u201cset +x\u201d that are to be debugged.The above technqiue helps to debug because it prefixes each command with the <strong style=\"text-align: initial; font-size: 1em;\">\u2018<\/strong><span style=\"text-align: initial; font-size: 1em;\">+\u2019 character that is within the script, it displays each command before executing it and it\u00a0<\/span>can\u00a0\u00a0 debug\u00a0\u00a0 only\u00a0\u00a0 selected\u00a0\u00a0 block\u00a0\u00a0\u00a0 of\u00a0\u00a0 a\u00a0\u00a0\u00a0 script\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 which\u00a0\u00a0\u00a0 can\u00a0\u00a0 be\u00a0\u00a0\u00a0 as\u00a0\u00a0\u00a0\u00a0\u00a0 follows:<\/li>\n<\/ul>\n<p>set -x\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # debugging is turned on&#8230;<\/p>\n<p>set +x\u00a0\u00a0\u00a0\u00a0\u00a0 # debugging is turned off<\/p>\n<p>&nbsp;<\/p>\n<p>The following is a sample shell script that uses debugging mode that uses bracketing technique.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-240 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/5.png\" alt=\"\" width=\"694\" height=\"196\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5.png 694w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-300x85.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-65x18.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-225x64.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/5-350x99.png 350w\" sizes=\"auto, (max-width: 694px) 100vw, 694px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In Linux and Unix, all programs that run are given three open file streams as listed in table 3:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-241 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/6.png\" alt=\"\" width=\"682\" height=\"172\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6.png 682w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-300x76.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-65x16.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-225x57.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/6-350x88.png 350w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Using redirection, we can save the stdout and stderr output streams or files for later analysis after a program or command is executed. Let us take an example of script that may run into errors. The following script may enter into a runtime error if the file with .java extension is not present in the current directory. Let us presume that the script generates error.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-242 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/7.png\" alt=\"\" width=\"695\" height=\"174\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7.png 695w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-300x75.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-65x16.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-225x56.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/7-350x88.png 350w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Under such circumstances, the program can be executed as shown below:<\/p>\n<p>$.\/script_filename.sh 2&gt; error.txt<\/p>\n<p>In the presence of any error, the error message will be stored in the file error.txt that can be displayed afterwards using the following command:<\/p>\n<p>$cat error.txt<\/p>\n<ol start=\"3\">\n<li><strong> Discarding Output with <\/strong><strong>\/dev\/null<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Certain commands like find or grep will produce huge amount of output, such outputs can overwhelm the console. Such commands may create problems (by producing output that is not required) while executing scripts, if they are used within the scripts. To avoid this, we can redirect the unwanted output to a special file (a device node) called \/dev\/null. This pseudofile is also called the bit bucket or black hole.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The following script is an example where we discard unwanted output to the \/dev\/null file while the program is executing. In the following script, user is asked to enter the name of friend(user-id) whom he wants to check whether the user has logged in or not. The script can run in background till the user wants to keep on checking whether the friend has logged-in or not in that time frame. In this script, the grep command may produce unnecessary output after each interval of one minute. So, to hide this output, we redirect it into the \/dev\/null file.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-243 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1.png\" alt=\"\" width=\"695\" height=\"349\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1.png 695w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1-300x151.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1-225x113.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-1-350x176.png 350w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/p>\n<ol start=\"4\">\n<li><strong> Reading from files<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify;\">At times, it is required to process files from the shell scripts. Online transaction systems or billing systems usually process sequence of records from files. Small shell scripts can be helpful in processing voluminous amount of data and such scripts can be created within minutes. Let us explore few ways of reading files line by line from shell scripts. Although there are many different methods to read files, we will explore few of them which are relatively simple.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method 1: Piped while-read loop<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the following shell script, the while loop accepts output of the command cat $FILENAME and processes each line, the program calculates number of lines of the file<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-244 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2.png\" alt=\"\" width=\"708\" height=\"202\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2.png 708w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2-300x86.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2-225x64.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/8-2-350x100.png 350w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method 2: Redirected &#8220;while-read&#8221; loop<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the following shell script, the while loop accepts input from the file using the statement done &lt; $FILENAME, each line of the file is read using while loop. This script counts the number of lines of the given file.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-245\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1.png\" alt=\"\" width=\"704\" height=\"203\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1.png 704w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1-300x87.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1-225x65.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/9-1-350x101.png 350w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method <\/strong><strong>3<\/strong><strong>: <\/strong><strong>Using file descriptors<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">exec is a <a href=\"https:\/\/www.computerhope.com\/jargon\/s\/shell.htm\">shell <\/a>command that replaces the current shell process with the command specified after exec. The first exec command redirects stdin to file descriptor 3. The use\u00a0of second exec command redirects the $FILENAME into stdin, which is indicated by the file descriptor 0. When the while loop executes, the loop takes input from the file and reads into the LINE variable. When the while loop exits, to restore the file descriptors, the exec restores it back to the original file descriptor 0.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-246 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1.png\" alt=\"\" width=\"690\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1.png 690w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1-225x98.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/10-1-350x153.png 350w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Method 4: Using combination of head and tail commands<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Use of head and tail commands can be combined to point to a specific line from a given file. These commands when used within loop can help in reading entire file as shown in the following script.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-247 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/11.png\" alt=\"\" width=\"692\" height=\"235\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/11.png 692w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/11-300x102.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/11-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/11-225x76.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/11-350x119.png 350w\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>5.Using arrays in Shell Scripting<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">An array is a variable containing multiple values that may be of same type or of different type. In Linux, there is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. An array index starts with zero. Let us explore how to use arrays within shell scripts.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Declaring an Array and Assigning values<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>In bash, array is created automatically when a variable is used in the format as shown below:<\/p>\n<p>array_name[index] = value.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">In the above format, the name can be any variable name, index values must be greater than or equal to zero, values that are stored in an array usually takes string format. To access an element of an array, the syntax uses curly brackets like ${name[index]}.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-248 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/12.png\" alt=\"\" width=\"699\" height=\"227\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/12.png 699w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/12-300x97.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/12-65x21.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/12-225x73.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/12-350x114.png 350w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Instead of initializing each element of an array separately, we can declare and initialize an array by specifying the list of elements (separated by white space) within curly braces. The syntax of the same is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">declare -a array_name=(element1 element2 element3)<\/p>\n<p style=\"text-align: justify;\">While declaring the array and initializing with strings, if the string (elements) has white sace character, we must enclose it with quotes.<\/p>\n<p style=\"text-align: justify;\">To print the whole array, following syntax is used:<\/p>\n<p style=\"text-align: justify;\">echo ${array_name[@]}<\/p>\n<p style=\"text-align: justify;\">To display the length of an array, the following syntax is used:<\/p>\n<p style=\"text-align: justify;\">echo ${#array_name[@]}<\/p>\n<p style=\"text-align: justify;\">To display element at index position\u00a0 the following syntax is used:<\/p>\n<p style=\"text-align: justify;\">echo ${array_name[2]}<\/p>\n<p style=\"text-align: justify;\">To display the length of an element at index position 2, the following syntax is used:<\/p>\n<p style=\"text-align: justify;\">echo ${#array_name[2]}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The following shell script illustrates the use of array, it initializes the array with 4 elements, further we display the entire array, length of the array, display the element at position 3 and to display the length of element at position 3 of that array.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-249 aligncenter\" src=\"http:\/\/itp7.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/30\/2018\/07\/13.png\" alt=\"\" width=\"692\" height=\"303\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/13.png 692w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/13-300x131.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/13-65x28.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/13-225x99.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-content\/uploads\/sites\/30\/2018\/07\/13-350x153.png 350w\" sizes=\"auto, (max-width: 692px) 100vw, 692px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Keywords:<\/strong><\/p>\n<p>array, debugging, file descriptors, stdin, stdout, stderr commands: tput, echo, exec<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Producing effective output using options of echo and tput command<\/li>\n<li>Debugging of shell scripts using set command<\/li>\n<li style=\"text-align: justify;\">Reading files line by line using different methods like redirection of file descriptors, combination of head and tail commands and inputting file to while loop<\/li>\n<li>using arrays in shell scripting<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on  Bash Shell Scripting &#8211; 3<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/LinDuYGArtM\" 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<p><strong>References:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>[1] Jain, <em>100 Shell Programs in Unix<\/em>. Pinnacle Technology, 2009.<\/p>\n<p>[2] Garrels, <em>Bash Guide for Beginners (Second Edition)<\/em>. Fultus Corporation, 2010.<\/p>\n<p>[3] Isrd, <em>Basics Of Os Unix And Shell Programming<\/em>. Tata McGraw-Hill Education, 2006.<\/p>\n<p>[4] Seebach, <em>Beginning Portable Shell Scripting: From Novice to Professional<\/em>. Apress, 2008.<\/p>\n<p>[5] Foster-Johnson, J. C.Welch, and M. Anderson, <em>Beginning Shell Scripting<\/em>. John Wiley &amp; Sons, 2007.<\/p>\n<p>[6] 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] Peters, <em>Expert Shell Scripting<\/em>. Apress, 2009.<\/p>\n<p>[8] G. Venkateshmurthy, <em>Introduction to Unix and Shell Programming<\/em>. Pearson Education India, 2005.<\/p>\n<p>[9] Fesari, <em>Learning Shell Scripting with Zsh<\/em>. Packt Publishing Ltd, 2014.<\/p>\n<p>[10] Newham, <em>Learning the bash Shell: Unix Shell Programming<\/em>. O\u2019Reilly Media, Inc.,2005.<\/p>\n<p>[11] Rosenblatt and A. Robbins, <em>Learning the Korn Shell<\/em>. O\u2019Reilly Media, Inc., 2002.<\/p>\n<p>[12] E. S. Jr, <em>Linux Command Line<\/em>. NO STARCH Press, 2012.<\/p>\n<p>[13] Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.<\/p>\n<p>[14] Blum and C. Bresnahan, <em>Linux Command Line and Shell Scripting Bible<\/em>. John Wiley &amp; Sons, 2015.<\/p>\n<p>[15] Lakshman, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2011.<\/p>\n<p>[16] Tushar, <em>Linux Shell Scripting Cookbook<\/em>. Packt Publishing Ltd, 2013.<\/p>\n<p>[17] O. Burtch, <em>Linux Shell Scripting with Bash<\/em>. Sams, 2004.<\/p>\n<p>[18] 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] Johnson, <em>Pro Bash Programming: Scripting the Linux Shell<\/em>. Apress, 2009.<\/p>\n<p>[20] Veeraraghavan, <em>Sams Teach Yourself Shell Programming in 24 Hours<\/em>. Sams Publishing, 2002.<\/p>\n<p>[21] Parker, <em>Shell Scripting: Expert Recipes for Linux, Bash and more<\/em>. John Wiley &amp; Sons, 2011.<\/p>\n<p>[22] F. A. Johnson, <em>Shell Scripting Recipes: A Problem Solution Approach<\/em>. Dreamtech Press, 2007.<\/p>\n<p>[23] Verma, <em>Unix and Shell Programming<\/em>. Laxmi Publications, 2006.<\/p>\n<p>[24] A. Forouzan and R. F. Gilberg, <em>UNIX and Shell Programming: A Textbook<\/em>. Brooks\/Cole-Thomson Learning, 2003.<\/p>\n<p>[25] P. Kanetkar, <em>Unix Shell Programming<\/em>. BPB Publications, 2002.<\/p>\n<p>[26] 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] Taylor, <em>Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems<\/em>. No Starch Press, 2004.<\/p>\n","protected":false},"author":4,"menu_order":15,"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-235","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\/235","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":6,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/235\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapters\/235\/revisions\/693"}],"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\/235\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/pressbooks\/v2\/chapter-type?post=235"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/contributor?post=235"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp7\/wp-json\/wp\/v2\/license?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}