References: [1] S. Jain, 100 Shell Programs in Unix. Pinnacle Technology, 2009. [2] M. 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] P. Seebach, Beginning Portable Shell Scripting: From Novice to Professional. Apress, 2008. [5] E. Foster-Johnson, J. C.Welch, and M. Anderson, Beginning Shell Scripting. John Wiley & Sons, 2007. [6] A. Robbins and N. H. F. Beebe, Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix. O Reilly Media, Inc., 2005. [7] R. Peters, Expert Shell Scripting. Apress, 2009. [8] M. G. Venkateshmurthy, Introduction to Unix and Shell Programming. Pearson Education India, 2005. [9] G. Festari, Learning Shell Scripting with Zsh. Packt Publishing Ltd, 2014. [10] C. Newham, Learning the bash Shell: Unix Shell Programming. O Reilly Media, Inc., 2005. [11] B. Rosenblatt and A. Robbins, Learning the Korn Shell. O Reilly Media, Inc., 2002. [12] W. E. S. Jr, Linux Command Line. NO STARCH Press, 2012. [13] R. Blum and C. Bresnahan, Linux Command Line and Shell Scripting Bible. John Wiley & Sons, 2015. [14] R. Blum and C. Bresnahan, Linux Command Line and Shell Scripting Bible. John Wiley & Sons, 2015. [15] S. Lakshman, Linux Shell Scripting Cookbook. Packt Publishing Ltd, 2011. [16] S. Tushar, Linux Shell Scripting Cookbook. Packt Publishing Ltd, 2013. [17] K. O. Burtch, Linux Shell Scripting with Bash. Sams, 2004. [18] R. K. Michael, Mastering Unix Shell Scripting: Bash, Bourne, and Korn Shell Scripting for Programmers, System Administrators, and UNIX Gurus. John Wiley & Sons, 2011. [19] C. Johnson, Pro Bash Programming: Scripting the Linux Shell. Apress, 2009. [20] S. Veeraraghavan, Sams Teach Yourself Shell Programming in 24 Hours. Sams Publishing, 2002. [21] S. Parker, Shell Scripting: Expert Recipes for Linux, Bash and more. John Wiley & Sons, 2011. [22] C. F. A. Johnson, Shell Scripting Recipes: A Problem Solution Approach. Dreamtech Press, 2007. [23] A. Verma, Unix and Shell Programming. Laxmi Publications, 2006. [24] B. A. Forouzan and R. F. Gilberg, UNIX and Shell Programming: A Textbook. Brooks/Cole-Thomson Learning, 2003. [25] Y. P. Kanetkar, Unix Shell Programming. BPB Publications, 2002. [26] T. Sanchez-Clark, Unix Shell Scripting Interview Questions, Answers, and Explanations: Unix Shell Certification Review. Equity Press, 2007. [27] D. Taylor, Wicked Cool Shell Scripts: 101 Scripts for Linux, Mac OS X, and Unix Systems. No Starch Press, 2004.
Echo Command echo command is used to display text or value of variable. echo [options] [string, variables...] Options -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: \b backspace \c suppress trailing new line \n new line \t horizontal tab \\ backslash \r carriage return For e.g. $ echo -e "An apple a day \n keeps doctor \t\taway" User Defined Variables The user defined variables (UDV) can be defined and assigned values using the following syntax : variable_name=value Here, value is assigned to the variable with variable_name. The following are few valid syntax : no=10 my_age=45 vehicle=car vehicle= vehicle= readonly no=10 #null value #null value #variable no cannot be modified Few invalid syntax : no =10 no = 10
no= 10 10=no Splitting Long Commands Over Multiple Lines Long commands that cannot be accommodated in a single line can be split across multiple lines. For instance a command to copy file from /var/ftp/pub/userdata/applications/file.txt from server1.mylinux.com to the directory /var/ftp/pub/userdata/applications/ directory of server2.mylinux.com will be like: scp xyz@server1.mylinux.com: /var/ftp/pub/userdata/applications/file.txt xyz@server2.mylinux.com:/var/ftp/pub/userdata/applications/ The above command can be split within the script using concatenation operator (\). So the large command spanning several lines can be made more readable. The concatenation operator applied to the above command looks like: scp xyz@server1.mylinux.com:\ /var/ftp/pub/userdata/applications/file.txt\ xyz@server2.mylinux.com:\ /var/ftp/pub/userdata/applications/ Putting Multiple Commands on a Single Line Multiple commands can be grouped together in a single line using the semicolon (;) operator. The following commands will all execute even if one of the preceding command fails: $ cat file1 ; cat file2 ; cat file3 However, if we want to abort subsequent commands if any one command fails, we can use the and (&&) operator as follows: $ cat file1 && cat file2 && cat file3 We can use the or ( ) operator which ensures that if the first command fails, the second one will never be executed. $ cat file1 cat file2 cat file3
Creating custom commands Shell scripts can be converted to user defined commands or custom commands. Suppose we have create a shell program that displays date in a given format along with the count of users. Let us assume the file is mydate. We can create our own command mydate by making this script executable and placing it in one of the directories pointed by the PATH variable. For instance, we can place the mydate file in the /home/richard/bin directory so that the shell program becomes a command. Few Sample Scripts Script-1 #Script to reverse the name echo "Enter your name : " read nm echo "Reverse of your name is : " echo "$nm" rev Script-2 #Script to append content of first file into second file echo "First file : $1" echo "Second file : $2" cat $1 >> $2 Script-3 #Script to display line count of a file echo "File name : $1" cnt=`wc -l $1` echo "Count : $cnt" Script-4 #Using functions within shell scripts getdata() { echo "Enter your name : " read name echo "Enter your age : " read age } showdata() { echo "Your name: $name" echo "Your age: $age"
} getdata showdata Script-5 #store in file : demo.sh #execute as $demo.sh a b c d e f g