15 Bash Shell Scripting – 3

Mr. Hardik Joshi

epgp books
  1. Formatting output using echo and tput commands in Shell Scripting echo command

 

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.

 

The following is an example command followed by its output that uses backspace, tab and newline with echo:

$echo -e “A quick brown\b\b\b fox jumps \t\t over the lazy \n dog”

 

Output:

A quick br fox jumps dog over the lazy

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.

 

 

output command

 

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

 

 

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.

 

  1. Debugging Shell scripts and dealing with errors

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.

 

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.

 

There are two popular ways to debug in shell scripting. They are as follows:

  • 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
  • Bracketing part of script using “set” command:Within the script, we can bracket the parts of the script with “set -x” and “set +x” that are to be debugged.The above technqiue helps to debug because it prefixes each command with the +’ character that is within the script, it displays each command before executing it and it can   debug   only   selected   block    of   a    script            which    can   be    as      follows:

set -x        # debugging is turned on…

set +x      # debugging is turned off

 

The following is a sample shell script that uses debugging mode that uses bracketing technique.

 

In Linux and Unix, all programs that run are given three open file streams as listed in table 3:

 

 

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.

 

 

Under such circumstances, the program can be executed as shown below:

$./script_filename.sh 2> error.txt

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:

$cat error.txt

  1. Discarding Output with /dev/null

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.

 

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.

  1. Reading from files

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.

 

Method 1: Piped while-read loop

 

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

 

 

Method 2: Redirected “while-read” loop

 

In the following shell script, the while loop accepts input from the file using the statement done < $FILENAME, each line of the file is read using while loop. This script counts the number of lines of the given file.

 

Method 3: Using file descriptors

 

exec is a shell 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 of 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.

 

 

Method 4: Using combination of head and tail commands

 

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.

 

5.Using arrays in Shell Scripting

 

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.

 

Declaring an Array and Assigning values

 

In bash, array is created automatically when a variable is used in the format as shown below:

array_name[index] = value.

 

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]}.

 

 

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:

 

declare -a array_name=(element1 element2 element3)

While declaring the array and initializing with strings, if the string (elements) has white sace character, we must enclose it with quotes.

To print the whole array, following syntax is used:

echo ${array_name[@]}

To display the length of an array, the following syntax is used:

echo ${#array_name[@]}

To display element at index position  the following syntax is used:

echo ${array_name[2]}

To display the length of an element at index position 2, the following syntax is used:

echo ${#array_name[2]}

 

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.

 

 

Keywords:

array, debugging, file descriptors, stdin, stdout, stderr commands: tput, echo, exec

 

Summary

  • Producing effective output using options of echo and tput command
  • Debugging of shell scripts using set command
  • 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
  • using arrays in shell scripting
you can view video on Bash Shell Scripting – 3

References:

 

[1] Jain, 100 Shell Programs in Unix. Pinnacle Technology, 2009.

[2] Garrels, Bash Guide for Beginners (Second Edition). Fultus Corporation, 2010.

[3] Isrd, Basics Of Os Unix And Shell Programming. Tata McGraw-Hill Education, 2006.

[4] Seebach, Beginning Portable Shell Scripting: From Novice to Professional. Apress, 2008.

[5] Foster-Johnson, J. C.Welch, and M. Anderson, Beginning Shell Scripting. John Wiley & Sons, 2007.

[6] Robbins and N. H. F. Beebe, Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix. O’Reilly Media, Inc., 2005.

[7] Peters, Expert Shell Scripting. Apress, 2009.

[8] G. Venkateshmurthy, Introduction to Unix and Shell Programming. Pearson Education India, 2005.

[9] Fesari, Learning Shell Scripting with Zsh. Packt Publishing Ltd, 2014.

[10] Newham, Learning the bash Shell: Unix Shell Programming. O’Reilly Media, Inc.,2005.

[11] Rosenblatt and A. Robbins, Learning the Korn Shell. O’Reilly Media, Inc., 2002.

[12] E. S. Jr, Linux Command Line. NO STARCH Press, 2012.

[13] Blum and C. Bresnahan, Linux Command Line and Shell Scripting Bible. John Wiley & Sons, 2015.

[14] Blum and C. Bresnahan, Linux Command Line and Shell Scripting Bible. John Wiley & Sons, 2015.

[15] Lakshman, Linux Shell Scripting Cookbook. Packt Publishing Ltd, 2011.

[16] Tushar, Linux Shell Scripting Cookbook. Packt Publishing Ltd, 2013.

[17] O. Burtch, Linux Shell Scripting with Bash. Sams, 2004.

[18] K. Michael, Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus. John Wiley & Sons, 2011.

[19] Johnson, Pro Bash Programming: Scripting the Linux Shell. Apress, 2009.

[20] Veeraraghavan, Sams Teach Yourself Shell Programming in 24 Hours. Sams Publishing, 2002.

[21] Parker, Shell Scripting: Expert Recipes for Linux, Bash and more. John Wiley & Sons, 2011.

[22] F. A. Johnson, Shell Scripting Recipes: A Problem Solution Approach. Dreamtech Press, 2007.

[23] Verma, Unix and Shell Programming. Laxmi Publications, 2006.

[24] A. Forouzan and R. F. Gilberg, UNIX and Shell Programming: A Textbook. Brooks/Cole-Thomson Learning, 2003.

[25] P. Kanetkar, Unix Shell Programming. BPB Publications, 2002.

[26] Sanchez-Clark, Unix Shell Scripting Interview Questions, Answers, and Explanations: Unix Shell Certification Review. Equity Press, 2007.

[27] Taylor, Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems. No Starch Press, 2004.