Introduction To Shell Script
Introduction To Shell Script
This tutorial is designed for beginners who wish to learn the basics of shell scripting/programming plus introduction to power tools such as awk, sed, etc. It is not help or manual for the shell; while reading this tutorial you can find manual quite useful (type man bash at $ prompt to see manual pages). Manual contains all necessary information you need, but it won't have that much examples, which makes idea more clear. For this reason, this tutorial contains examples rather than all the features of shell.
Chapter Organization
Chapter 1 to 4 shows most of the useful and important shell scripting concepts. Chapter 5 introduction to tools & utilities which can be used while programming the Linux shell smartly. Chapter 6 and 7 is all about expression and expression mostly used by tools such as sed and awk. Chapter 8 is loaded with tons of shell scripting examples divided into different categories. Chapter 9 gives more resources information which can be used while learning the shell scripting like information on Linux file system, common Linux command reference and other resources. Chapter 1 introduces to basic concepts such as what is Linux, where Linux can used and continue enplaning the shell, shell script and kernel etc. Chapter 2 shows how to write the shell script and execute them. It explains many basic concepts which requires to write shell script.
Chapter 3 is all about making decision in shell scripting as well as loops in shell. It explains what expression are, how shell understands the condition/decisions. It also shows you nesting concept for if and for loop statement and debugging of shell script. Chapter 4 introduces the many advanced shell scripting concepts such as function, user interface, File Descriptors, signal handling, Multiple command line arguments etc. Chapter 5 introduces to powerful utility programs which can be used variety of purpose while programming the shell scripting. Chapter 6 and 7 gives more information on patterns, filters, expressions, and off course sed and awk is covered in depth. Chapter 8 contains lot of example of shell scripting divided into various category such as logic development, system administration etc. Note that indicate advanced shell scripting concepts, you can skip this if are really new to Linux or Programming, though this is not RECOMMENDED by me. Also not that currently this tutorial is also translated into some other foreign language(s); if you are interested to read it in other language the the English then please visit http://www.nixcraft.com/uniqlinuxfeatures/lsst/. I hope you get as much pleasure reading this tutorial, as I had writing it. After reading this tutorial if you are able to write your own powerful shell scripts, then I think the purpose of writing this tutorial is served and finally if you do get time after reading this tutorial drop me an e-mail message about your comment/suggestion and off course bugs (errors) of this tutorial.
Caldera Linux: http://www.calderasystems.com/ Debian GNU/Linux: http://www.debian.org/ Slackware Linux: http://www.slackware.com/ Note: If you are in India then you can get Linux Distribution from the Leading Computer magazine such as PC Quest (Even PCQuest has got its own Linux flavor) or if you are in Pune, India please visit the our sponsor web site to obtained the Red Hat Linux or any other official Linux distribution. Note that you can also obtained your Linux distribution with Linux books which you purchase from Local book store.
Linux offers all of the above characteristics plus its Open Source and Free OS. So Linux can be used as: (1) On stand alone workstation/PC for word processing, graphics, software development, internet, e-mail, chatting, small personal database management system etc. (2) In network environment as: (A) File and Print or Application Server Share the data, Connect the expensive device like printer and share it, e-mail within the LAN/intranet etc are some of the application.
(B) Linux sever cab be connected to Internet, So that PC's on intranet can share the internet/e-mail etc. You can put your web sever that run your web site or transmit the information on the internet.
Personal Work Web Server Software Development Workstation Workgroup Server In Data Center for various server activities such as FTP, Telnet, SSH, Web, Mail, Proxy, Proxy Cache Appliance etc
See the LESSBS project for more information on Linux Essential Services (as mentioned above) and how to implement them in easy manner for you or your organization.
I/O management Process management Device management File management Memory management
CSH (C SHell)
Freeware shell. Bill Joy University of The C shell's syntax California (For BSD) and usage are very similar to the C programming language. David Korn AT & T Bell Labs -See the man page. -TCSH is an enhanced Type $ man tcsh but completely compatible version of the Berkeley UNIX C shell (CSH).
Tip: To find all available shells in your system type following command: $ cat /etc/shells Note that each shell does the same job, but each understand a different command syntax and provides different built-in functions. In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's not as powerful as our Linux Shells are! Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux Os what users want. If we are giving commands from keyboard it is called command line interface ( Usually in-front of $ prompt, This prompt is depend upon your shell and Environment that you set or by your System Administrator, therefore you may get different prompt ). Tip: To find your current shell type following command $ echo $SHELL
commands) , the you can store this sequence of command to text file and tell the shell to execute this text file instead of entering the commands. This is know as shell script. Shell script defined as: "Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file."
Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.
In this part of tutorial you are introduce to shell programming, how to write script, execute them etc. We will getting started with writing small shell script, that will print "Knowledge is Power" on screen. Before starting with this you should know
How to use text editor such as vi, see the common vi command for more information. Basic command navigation
Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5). (3) Execute your script as syntax: bash your-script-name sh your-script-name ./your-script-name Examples:
$ bash bar $ sh bar $ ./bar
NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows Syntax: . command-name Example:
$ . foo
Now you are ready to write first shell script that will print "Knowledge is Power" on screen. See the common vi command list , if you are new to vi.
$ vi first # # My first shell script # clear echo "Knowledge is Power"
After saving the above script, you can run the script as follows:
$ ./first
This will not run script since we have not set execute permission for our script first; to do this type command
$ chmod 755 first $ ./first
First screen will be clear, then Knowledge is Power is printed on screen. Script Command(s) $ vi first # # My first shell script # clear Meaning Start vi editor # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text clear the screen To print message or value of variables on screen, we use echo command, general form of echo command is as follows syntax: echo "Message"
How Shell Locates the file (My own bin directory to execute script) Tip: For shell script file try to give file extension such as .sh, which can be easily identified by you as shell script. Exercise: 1)Write following shell script, save it, execute it and note down the it's output.
$ vi ginfo # #
10
# Script to print user information who currently login , current date & time # clear echo "Hello $USER" echo "Today is \c ";date echo "Number of user login : \c" ; who | wc -l echo "Calendar" cal exit 0
Future Point: At the end why statement exit 0 is used? See exit status for more information.
Variables in Shell
To process our data/information, data must be kept in computers RAM memory. RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable (Its a named storage location that may take different values, but only one at a time). In Linux (Shell), there are two types of variable: (1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. (2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters. You can see system variables by giving command like $ set, some of the important System variables are: System Variable BASH=/bin/bash BASH_VERSION=1.14.7(1) COLUMNS=80 HOME=/home/vivek LINES=25 LOGNAME=students OSTYPE=Linux PATH=/usr/bin:/sbin:/bin:/usr/sbin PS1=[\u@\h \W]\$ PWD=/home/students/Common Meaning Our shell name Our shell version name No. of columns for our screen Our home directory No. of columns for our screen students Our logging name Our Os type Our path settings Our prompt settings Our current working directory 11
SHELL=/bin/bash USERNAME=vivek
NOTE that Some of the above settings can be different in your PC/Linux environment. You can print any of the above variables contains as follows:
$ echo $USERNAME $ echo $HOME
Exercise: 1) If you want to print your home directory location then you give command: a)$ echo $HOME OR (b)$ echo HOME Which of the above command is correct & why? Click here for answer. Caution: Do not modify System variable this can some time create problems.
Rules for Naming variable name (Both UDV and System Variable)
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g. Valid shell variable are as
12
follows HOME SYSTEM_VERSION vech no (2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In following variable declaration there will be no error
$ no=10
But there will be problem for any of the following variable declaration:
$ no =10 $ no= 10 $ no = 10
(3) Variables are case-sensitive, just like filename in Linux. For e.g.
$ $ $ $ no=10 No=11 NO=20 nO=2
Above all are different variable name, so to print value 20 we have to use $ echo $NO and not any of the following $ echo $no # will print 10 but not 20 $ echo $No# will print 11 but not 20 $ echo $nO# will print 2 but not 20 (4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g. $ vech= $ vech="" Try to print it's value by issuing following command
$ echo $vech
Nothing will be shown because variable has no value i.e. NULL variable. (5) Do not use ?,* etc, to name your variable names.
13
$ echo $vech
It will print 'Bus',To print contains of variable 'n' type command as follows
$ echo $n
Caution: Do not try $ echo vech, as it will print vech instead its value 'Bus' and $ echo n, as it will print n instead its value '10', You must use $ followed by variable name. Exercise Q.1.How to Define variable x with value 10 and print it on screen. Q.2.How to Define variable xn with value Rani and print it on screen Q.3.How to print sum of two numbers, let's say 6 and 3? Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y) Q.5.Modify above and store division of x and y to variable called z Q.6.Point out error if any in following script
$ vi variscript # # # Script to test MY knowledge about variables! # myname=Vivek myos = TroubleOS myno=5 echo "My name is $myname" echo "My os is $myos" echo "My number is myno, can you see this number"
echo Command
Use echo command to display text or value of variable. echo [options] [string, variables...] Displays text or variables value on screen. Options -n Do not output the trailing new line. -e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell) \b backspace \c suppress trailing new line \n new line \r carriage return \t horizontal tab \\ backslash For e.g. $ echo -e "An apple a day keeps away \a\t\tdoctor\n"
14
How to display colorful text on screen with bold or blink effects, how to print text on any row, column on screen, click here for more!
Shell Arithmetic
Use to perform arithmetic operations. Syntax: expr op1 math-operator op2 Examples:
$ $ $ $ $ $ expr expr expr expr expr echo 1 + 3 2 - 1 10 / 2 20 % 3 10 \* 3 `expr 6 + 3`
Note: expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2. expr 10 \* 3 - Multiplication use \* and not * since its wild card. For the last statement not the following points (1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key. (2) Second, expr is also end with ` i.e. . back quote. (3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum (4) Here if you use double quote or single quote, it will NOT work For e.g. $ echo "expr 6 + 3" # It will print expr 6 + 3 $ echo 'expr 6 + 3' # It will print expr 6 + 3 See Parameter substitution - To save your time.
15
Meaning "Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $). 'Single quotes' - Enclosed in single quotes remains unchanged. `Back quote` - To execute command
Example: $ echo "Today is date" Can't print message with today's date. $ echo "Today is `date`". It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back quote?
Exit Status
By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not. (1) If return value is zero (0), command is successful. (2) If return value is nonzero, command is not successful or some sort of error executing command/shell script. This value is know as Exit Status. But how to find out exit status of command or shell script? Simple, to determine this exit Status you can use $? special variable of shell. For e.g. (This example assumes that unknow1file doest not exist on your hard drive) $ rm unknow1file It will show error as follows rm: cannot remove `unkowm1file': No such file or directory and after that if you give command $ echo $? it will print nonzero value to indicate error. Now give command $ ls $ echo $? It will print 0 to indicate command is successful. Exercise Try the following commands and note down the exit status:
16
$ expr 1 + 3 $ echo $? $ echo Welcome $ echo $? $ wildwest canwork? $ echo $? $ date $ echo $? $ echon $? $ echo $?
$? useful variable, want to know more such Linux variables click here to explore them!
Run it as follows:
17
Meaning $ ls * $ ls a*
Examples will show all files will show all files whose first name is starting with letter 'a' will show all files having extension .c will show all files having extension .c but file name must begin with 'ut'. will show all files whose names are 1 character long will show all files whose names are 3 character long and file name begin with fo will show all files beginning with letters a,b,c
[...]
$ ls [abc]*
Note: [..-..] A pair of characters separated by a minus sign denotes a range. Example: $ ls /bin/[a-c]* Will show all files name beginning with letter a,b or c like /bin/arch /bin/awk /bin/bsh /bin/chmod /bin/cp /bin/ash /bin/basename /bin/cat /bin/chown /bin/cpio /bin/ash.static /bin/bash /bin/chgrp /bin/consolechars /bin/csh But $ ls /bin/[!a-o] $ ls /bin/[^a-o] If the first character following the [ is a ! or a ^ ,then any character not enclosed is matched i.e. do not show us file name that beginning with a,b,c,e...o, like /bin/ps /bin/pwd /bin/rvi /bin/rview /bin/sleep /bin/touch /bin/sort /bin/true /bin/view /bin/wcomp 18
/bin/red /bin/sayHello /bin/stty /bin/umount /bin/xconf /bin/remadmin /bin/sed /bin/su /bin/uname /bin/ypdomainname /bin/rm /bin/setserial /bin/sync /bin/userconf /bin/zcat /bin/rmdir /bin/sfxload /bin/tar /bin/usleep /bin/rpm /bin/sh /bin/tcsh /bin/vi
Answer:
19
Actual Argument foo y and y.bak y.bak and y.okay -10 and myf raj -r, -n, and myf
NOTE: $# holds number of arguments specified on command line. And $* or $@ refer to all arguments passed to script.
20
Shell Script name i.e. myshell First command line argument passed to myshell i.e. foo Second command line argument passed to myshell i.e. bar In shell if we wish to refer this command line argument we refer above as follows myshell it is $0 foo it is $1 bar it is $2 Here $# (built in shell variable ) will be 2 (Since foo and bar only two Arguments), Please note at a time such 9 arguments can be used from $1..$9, You can also refer all of them by using $* (which expand to `$1,$2...$9`). Note that $1..$9 i.e command line arguments to shell script is know as "positional parameters". Exercise Try to write following for commands Shell Script Name ($0), No. of Arguments (i.e. $#), And actual argument (i.e. $1,$2 etc)
$ $ $ $ $ $ $ sum 11 20 math 4 - 7 d bp -5 myf +20 Ls * cal findBS 4 8 24 BIG
Answer
21
cal findBS
0 4
24
BIG
Following script is used to print command ling argument and will show you how to access them:
$ vi demo #!/bin/sh # # Script that demos, command line args # echo "Total number of command line argument are $#" echo "$0 is script name" echo "$1 is first argument" echo "$2 is second argument" echo "All of them are :- $* or $@"
Run it as follows Set execute permission as follows: $ chmod 755 demo Run it & test it as follows: $ ./demo Hello World If test successful, copy script to your own bin directory (Install script for private use) $ cp demo ~/bin Check whether it is working or not (?) $ demo $ demo Hello World NOTE: After this, for any script you have to used above command, in sequence, I am not going to show you all of the above command(s) for rest of Tutorial. Also note that you can't assigne the new value to command line arguments i.e positional parameters. So following all statements in shell script are invalid: $1 = 5 $2 = "My Name"
22
For e.g. $ ls command gives output to screen; to send output to file of ls command give command $ ls > filename It means put output of ls command to filename. There are three main redirection symbols >,>>,< (1) > Redirector Symbol Syntax: Linux-command > filename To output Linux-commands result (output of command or shell script) to file. Note that if file already exist, it will be overwritten else new file is created. For e.g. To send output of ls command give $ ls > myfiles Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning. (2) >> Redirector Symbol Syntax: Linux-command >> filename To output Linux-commands result (output of command or shell script) to END of file. Note that if file exist , it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created. For e.g. To send output of date command to already exist file give command $ date >> myfiles (3) < Redirector Symbol Syntax: Linux-command < filename To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give $ cat < myfiles Click here to learn more about I/O Redirection You can also use above redirectors simultaneously as follows Create text file sname as follows $cat > sname vivek ashish zebra babu Press CTRL + D to save.
23
Now issue following command. $ sort < sname > sorted_names $ cat sorted_names ashish babu vivek zebra In above example sort ($ sort < sname > sorted_names) command takes input from sname file and output of sort command (i.e. sorted names) is redirected to sorted_names file. Try one more example to clear your idea: $ tr "[a-z]" "[A-Z]" < sname > cap_names $ cat cap_names VIVEK ASHISH ZEBRA BABU tr command is used to translate all lower case characters to upper-case letters. It take input from sname file, and tr's output is redirected to cap_names file. Future Point : Try following command and find out most important point: $ sort > new_sorted_names < sname $ cat new_sorted_names
Pipes
A pipe is a way to connect the output of one program to the input of another program without any temporary file.
Pipe Defined as: "A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line."
24
$ ls -l | wc -l
Meaning or Use of Pipes Output of ls command is given as input to more command So that output is printed one screen full page at a time. Output of who command is given as input to sort command So that it will print sorted list of users Same as above except output of sort is send to (redirected) user_list file Output of who command is given as input to wc command So that it will number of user who logon to system Output of ls command is given as input to wc command So that it will print number of files in current directory. Output of who command is given as input to grep command So that it will print if particular user name if he is logon or nothing is printed (To see particular user is logon or not)
Filter
If a Linux command accepts its input from the standard input and produces its output on standard output is know as a filter. A filter performs some kind of process on the input and gives output. For e.g.. Suppose you have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' you would like to print contains from line number 20 to line number 30 and store this result to file called 'hlist' then give command: $ tail +20 < hotel.txt | head -n30 >hlist Here head command is filter which takes its input from tail command (tail command start selecting from line number 20 of given file i.e. hotel.txt) and passes this lines as input to head, whose output is redirected to 'hlist' file. Consider one more following example $ sort < sname | uniq > u_sname
25
Here uniq is filter which takes its input from sort command and passes this lines as input to uniq; Then uniqs output is redirected to "u_sname" file.
What is Processes
Process is kind of program or task carried out by your PC. For e.g. $ ls -lR ls command or a request to list files in a directory and all subdirectory in your current directory - It is a process. Process defined as: "A process is program (command given by user) to perform specific Job. In Linux when you start process, it gives a number to process (called PID or process-id), PID starts from 0 to 65535."
26
To stop processes by name i.e. to kill process To get information about all running process To stop all process except your shell For background processing (With &, use to put particular command and program in background) To display the owner of the processes along with the processes
linux-command &
$ ls / -R | wc -l &
ps aux
$ ps aux
For e.g. you want to see To see if a particular process is whether Apache web running or not. For this purpose ps ax | grep process-U-want- server process is running you have to use ps command in or not then give command to see combination with the grep command $ ps ax | grep httpd To see currently running $ top processes and other information top like memory and CPU usage See the output of top command. Note that to exit from top with real time updates. command press q. To display a tree of processes pstree $ pstree
* To run some of this command you need to be root or equivalnt user. NOTE that you can only kill process which are created by yourself. A Administrator can almost kill 95-98% process. But some process can not be killed, such as VDU Process. Exercise: You are working on your Linux workstation (might be learning LSST or some other work like sending mails, typing letter), while doing this work you have started to play MP3 files on your workstation. Regarding this situation, answer the following question: 1) Is it example of Multitasking? 2) How you will you find out the both running process (MP3 Playing & Letter typing)? 3) "Currently only two Process are running in your Linux/PC environment", Is it True or False?, And how you will verify this? 4) You don't want to listen music (MP3 Files) but want to continue with other work on PC, you will take any of the following action: 1. Turn off Speakers
27
2. Turn off Computer / Shutdown Linux Os 3. Kill the MP3 playing process 4. None of the above
Introduction - 3
Making decision is important part in ONCE life as well as in computers logical driven program. In fact logic is not LOGIC until you use decision making. This chapter introduces to the bashs structured language constricts such as:
Is there any difference making decision in Real life and with Computers? Well real life decision are quit complicated to all of us and computers even don't have that much power to understand our real life decisions. What computer know is 0 (zero) and 1 that is Yes or No. To make this idea clear, lets play some game (WOW!) with bc - Linux calculator program. $ bc After this command bc is started and waiting for your commands, i.e. give it some calculation as follows type 5 + 2 as: 5+2 7 7 is response of bc i.e. addition of 5 + 2 you can even try 5-2 5/2 See what happened if you type 5 > 2 as follows 5>2 1 1 (One?) is response of bc, How? bc compare 5 with 2 as, Is 5 is greater then 2, (If I ask same question to you, your answer will be YES), bc gives this 'YES' answer by showing 1 value. Now try 5<2 0 0 (Zero) indicates the false i.e. Is 5 is less than 2?, Your answer will be no which is indicated by bc by showing 0 (Zero). Remember in bc, relational expression always returns true (1) or false (0 - zero). Try following in bc to clear your Idea and not down bc's response 5 > 12 5 == 10 5 != 2 5 == 5 12 < 2
28
BC's Response 0 0 1 1 1
It means when ever there is any type of comparison in Linux Shell It gives only two answer one is YES and NO is other. In Linux Shell Value Zero Value (0) NON-ZERO Value Meaning Yes/True No/False Example 0 -1, 32, 55 anything but not zero
Remember both bc and Linux Shell uses different ways to show True/False values Value True/Yes False/No 1 0 Shown in bc as Shown in Linux Shell as 0 Non - zero value
if condition
if condition which is used for decision making in shell script, If given condition is true then command1 is executed. Syntax:
if condition then command1 if condition is true or if exit status of condition is 0 (zero) ... ... fi
Condition is defined as: "Condition is nothing but comparison between two values." For compression you can use test or [ expr ] statements or even exist status can be also used. 29
Expreession is defined as: "An expression is nothing but combination of values, relational operator (such as >,<, <> etc) and mathematical operators (such as +, -, / etc )." Following are all examples of expression: 5>2 3+6 3 * 65 a<b c>5 c > 5 + 30 -1 Type following commands (assumes you have file called foo) $ cat foo $ echo $? The cat command return zero(0) i.e. exit status, on successful, this can be used, in if condition as follows, Write shell script as
$ cat > showfile #!/bin/sh # #Script to print file # if cat $1 then echo -e "\n\nFile $1, found and successfully echoed" fi
Run above script as: $ chmod 755 showfile $./showfile foo Shell script name is showfile ($0) and foo is argument (which is $1).Then shell compare it as follows: if cat $1 which is expanded to if cat foo. Detailed explanation if cat command finds foo file and if its successfully shown on screen, it means our cat command is successful and its exist status is 0 (indicates success), So our if condition is also true and hence statement echo -e "\n\nFile $1, found and successfully echoed" is proceed by shell. Now if cat command is not successful then it returns non-zero value (indicates some sort of failure) and this statement echo -e "\n\nFile $1, found and successfully echoed" is skipped by our shell. Exercise Write shell script as follows:
cat > trmif
30
# # Script to test rm command and exist status # if rm $1 then echo "$1 file deleted" fi
Press Ctrl + d to save $ chmod 755 trmif Answer the following question in referance to above script: (A) foo file exists on your disk and you give command, $ ./trmfi foo what will be output? (B) If bar file not present on your disk and you give command, $ ./trmfi bar what will be output? (C) And if you type $ ./trmfi What will be output?
Run it as follows $ chmod 755 ispostive $ ispostive 5 5 number is positive $ispostive -45 Nothing is printed $ispostive ./ispostive: test: -gt: unary operator expected 31
Detailed explanation The line, if test $1 -gt 0 , test to see if first command line argument($1) is greater than 0. If it is true(0) then test will return 0 and output will printed as 5 number is positive but for -45 argument there is no output because our condition is not true(0) (no -45 is not greater than 0) hence echo statement is skipped. And for last statement we have not supplied any argument hence error ./ispostive: test: -gt: unary operator expected, is generated by shell , to avoid such error we can test whether command line argument is supplied or not. test or [ expr ] works with 1.Integer ( Number without decimal point) 2.File types 3.Character strings For Mathematics, use following operator in Shell Script Mathematical Operator in Shell Script Normal Arithmetical/ Mathematical Statements
Meaning
But in Shell For test For [ expr ] statement with statement with if command if command if test 5 -eq 6 if [ 5 -eq 6 ] if test 5 -ne 6 if test 5 -lt 6 if test 5 -le 6 if test 5 -gt 6 if test 5 -ge 6 if [ 5 -ne 6 ] if [ 5 -lt 6 ] if [ 5 -le 6 ] if [ 5 -gt 6 ] if [ 5 -ge 6 ]
is equal to is not equal to is less than is less than or equal to is greater than is greater than or equal to
NOTE: == is equal, != is not equal. For string Comparisons use Operator string1 = string2 string1 != string2 Meaning string1 is equal to string2 string1 is NOT equal to string2
32
string1 is NOT NULL or not defined string1 is NOT NULL and does exist string1 is NULL and does exist Shell also test for file and directory types
Meaning Non empty file Is File exist or normal file and not a directory Is Directory exist and not a file Is writeable file Is read-only file Is file is executable Logical Operators
Logical operators are used to combine two or more condition at a time Operator ! expression expression1 -a expression2 expression1 -o expression2 Meaning Logical NOT Logical AND Logical OR
if...else...fi
If given condition is true then command1 is executed otherwise command2 is executed. Syntax:
if condition then condition is zero (true - 0) execute all commands up to else statement else if condition is not true then execute all commands up to fi fi
33
$ vi isnump_n #!/bin/sh # # Script to see whether argument is positive or negative # if [ $# -eq 0 ] then echo "$0 : You must give/supply one integers" exit 1 fi
if test $1 -gt 0 then echo "$1 number is positive" else echo "$1 number is negative" fi
Try it as follows: $ chmod 755 isnump_n $ isnump_n 5 5 number is positive $ isnump_n -45 -45 number is negative $ isnump_n ./ispos_n : You must give/supply one integers $ isnump_n 0 0 number is negative Detailed explanation First script checks whether command line argument is given or not, if not given then it print error message as "./ispos_n : You must give/supply one integers". if statement checks whether number of argument ($#) passed to script is not equal (-eq) to 0, if we passed any argument to script then this if statement is false and if no command line argument is given then this if statement is true. The echo command i.e. echo "$0 : You must give/supply one integers" | | | | 1 2 1 will print Name of script 2 will print this error message And finally statement exit 1 causes normal program termination with exit status 1 (nonzero means script is not successfully run).
34
The last sample run $ isnump_n 0 , gives output as "0 number is negative", because given argument is not > 0, hence condition is false and it's taken as negative number. To avoid this replace second if statement with if test $1 -ge 0.
Nested if-else-fi
You can write the entire if-else construct within either the body of the if statement of the body of an else statement. This is called the nesting of ifs.
$ vi nestedif.sh osch=0 echo echo echo read "1. Unix (Sun Os)" "2. Linux (Red Hat)" -n "Select your os choice [1 or 2]? " osch
if [ $osch -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else #### nested if i.e. if within if ###### if [ $osch -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS." fi fi
Run the above shell script as follows: $ chmod +x nestedif.sh $ ./nestedif.sh 1. Unix (Sun Os) 2. Linux (Red Hat) Select you os choice [1 or 2]? 1 You Pick up Unix (Sun Os) $ ./nestedif.sh 1. Unix (Sun Os) 2. Linux (Red Hat) Select you os choice [1 or 2]? 2 You Pick up Linux (Red Hat) $ ./nestedif.sh 1. Unix (Sun Os) 2. Linux (Red Hat) Select you os choice [1 or 2]? 3 What you don't like Unix/Linux OS. 35
Note that Second if-else constuct is nested in the first else statement. If the condition in the first if statement is false the the condition in the second if statement is checked. If it is false as well the final else statement is executed. You can use the nested ifs as follows also: Syntax:
if condition then if condition then ..... .. do this else .... .. do this fi else ... ..... do this fi
Multilevel if-then-else
Syntax:
condition is zero (true - 0) execute all commands up to elif statement elif condition1 then condition1 is zero (true - 0) execute all commands up to elif statement elif condition2 then condition2 is zero (true - 0) execute all commands up to elif statement else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) execute all commands up to fi fi if condition then
36
$ cat > elf # #!/bin/sh # Script to test if..elif...else # if [ $1 -gt 0 ]; then echo "$1 is positive" elif [ $1 -lt 0 ] then echo "$1 is negative" elif [ $1 -eq 0 ] then echo "$1 is zero" else echo "Opps! $1 is not number, give number" fi
Try above script as follows: $ chmod 755 elf $ ./elf 1 $ ./elf -2 $ ./elf 0 $ ./elf a Here o/p for last sample run: ./elf: [: -gt: unary operator expected ./elf: [: -lt: unary operator expected ./elf: [: -eq: unary operator expected Opps! a is not number, give number Above program gives error for last run, here integer comparison is expected therefore error like "./elf: [: -gt: unary operator expected" occurs, but still our program notify this error to user by providing message "Opps! a is not number, give number".
Note that in each and every loop, (a) First, the variable used in loop condition must be initialized, then execution of the loop begins.
37
(b) A test (condition) is made at the beginning of each iteration. (c) The body of loop ends with a statement that modifies the value of the test (condition) variable.
for Loop
Syntax:
for { variable name } in { list } do execute one for each item in the list until the not finished (And repeat all statement between do done
Run it above script as follows: $ chmod +x testfor $ ./testfor The for loop first creates i variable and assigned a number to i from the list of number from 1 to 5, The shell execute echo statement for each assignment of i. (This is usually know as iteration) This process will continue until all the items in the list were not finished, because of this it will repeat 5 echo statements. To make you idea more clear try following script:
$ cat > mtable #!/bin/sh # #Script to test for loop # # if [ $# -eq 0 ] then echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo "Use to print multiplication table for given number" exit 1 fi n=$1 for i in 1 2 3 4 5 6 7 8 9 10 do echo "$n * $i = `expr $i \* $n`"
38
done
Save above script and run it as: $ chmod 755 mtable $ ./mtable 7 $ ./mtable For first run, above script print multiplication table of given number where i = 1,2 ... 10 is multiply by given n (here command line argument 7) in order to produce multiplication table as 7*1=7 7 * 2 = 14 ... .. 7 * 10 = 70 And for second test run, it will print message Error - Number missing form command line argument Syntax : ./mtable number Use to print multiplication table for given number This happened because we have not supplied given number for which we want multiplication table, Hence script is showing Error message, Syntax and usage of our script. This is good idea if our program takes some argument, let the user know what is use of the script and how to used the script. Note that to terminate our script we used 'exit 1' command which takes 1 as argument (1 indicates error and therefore script is terminated) Even you can use following syntax: Syntax:
for (( expr1; expr2; expr3 )) do ..... ... repeat all statements between do and done until expr2 is TRUE Done
In above syntax BEFORE the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop. All the statements between do and done is executed repeatedly UNTIL the value of expr2 is TRUE. AFTER each iteration of the loop, expr3 is evaluated. This is usually use to increment a loop counter.
39
$ cat > for2 for (( i = 0 ; i <= 5; i++ do echo "Welcome $i times" done
))
Run the above script as follows: $ chmod +x for2 $ ./for2 Welcome 0 times Welcome 1 times Welcome 2 times Welcome 3 times Welcome 4 times Welcome 5 times In above example, first expression (i = 0), is used to set the value variable i to zero. Second expression is condition i.e. all statements between do and done executed as long as expression 2 (i.e continue as long as the value of variable i is less than or equel to 5) is TRUE. Last expression i++ increments the value of i by 1 i.e. it's equivalent to i = i + 1 statement.
for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ### do echo -n "$i " done echo "" #### print the new line ### done
Run the above script as follows: $ chmod +x nestedfor.sh $ ./nestefor.sh 11111 22222 33333
40
44444 55555 Here, for each value of i the inner loop is cycled through 5 times, with the varible j taking values from 1 to 5. The inner for loop terminates when the value of j exceeds 5, and the outer loop terminets when the value of i exceeds 5. Following script is quite intresting, it prints the chess board on screen.
$ vi chessboard for (( i = 1; i <= 9; i++ )) ### Outer for loop ### do for (( j = 1 ; j <= 9; j++ )) ### Inner for loop ### do tot=`expr $i + $j` tmp=`expr $tot % 2` if [ $tmp -eq 0 ]; then echo -e -n "\033[47m " else echo -e -n "\033[40m " fi done echo -e -n "\033[40m" #### set back background colour to black echo "" #### print the new line ### done
Run the above script as follows: $ chmod +x chessboard $ ./chessboard On my terminal above script produec the output as follows:
41
Above shell script cab be explained as follows: Command(s)/Statements for (( i = 1; i <= 9; i++ )) do for (( j = 1 ; j <= 9; j++ )) do tot=`expr $i + $j` tmp=`expr $tot % 2` if [ $tmp -eq 0 ]; then echo -e -n "\033[47m " else echo -e -n "\033[40m " fi done echo -e -n "\033[40m" echo "" done Explanation Begin the outer loop which runs 9 times., and the outer loop terminets when the value of i exceeds 9 Begins the inner loop, for each value of i the inner loop is cycled through 9 times, with the varible j taking values from 1 to 9. The inner for loop terminates when the value of j exceeds 9. See for even and odd number positions using these statements. If even number posiotion print the white colour block (using echo -e -n "\033[47m " statement); otherwise for odd postion print the black colour box (using echo -e -n "\033[40m " statement). This statements are responsible to print entier chess board on screen with alternet colours. End of inner loop Make sure its black background as we always have on our terminals. Print the blank line End of outer loop and shell scripts get terminted by printing the chess board.
42
while loop
Syntax:
while [ condition ] do command1 command2 command3 .. .... done
Loop is executed as long as given condition is true. For e.g.. Above for loop program (shown in last section of for loop) can be written using while loop as:
$cat > nt1 #!/bin/sh # #Script to test while statement # # if [ $# -eq 0 ] then echo "Error - Number missing form command line argument" echo "Syntax : $0 number" echo " Use to print multiplication table for given number" exit 1 fi n=$1 i=1 while [ $i -le 10 ] do echo "$n * $i = `expr $i \* $n`" i=`expr $i + 1` done
Save it and try as $ chmod 755 nt1 $./nt1 7 Above loop can be explained as follows:
Set the value of command line argument to variable n. (Here it's set to 7 ) Set variable i to 1 This is our loop condition, here if value of i is less than 10 then, shell execute all
43
do
i=`expr $i + 1`
done
statements between do and done Start loop Print multiplication table as 7*1=7 7 * 2 = 14 .... 7 * 10 = 70, Here each time value of variable n is multiply be i. Increment i by 1 and store result to i. ( i.e. i=i+1) Caution: If you ignore (remove) this statement than our loop become infinite loop because value of variable i always remain less than 10 and program will only output 7*1=7 ... ... E (infinite times) Loop stops here if i is not less than 10 i.e. condition of loop is not true. Hence loop is terminated.
esac
44
The $variable-name is compared against the patterns until a match is found. The shell then executes all the statements up to the two semicolons that are next to each other. The default is *) and its executed if no match is found. For e.g. write script as follows:
$ # # # # # cat > car if no vehicle name is given i.e. -z $1 is defined and it is NULL if no command line arg
if [ -z $1 ] then rental="*** Unknown vehicle ***" elif [ -n $1 ] then # otherwise make first arg as rental rental=$1 fi case $rental in "car") echo "For $rental Rs.20 per k/m";; "van") echo "For $rental Rs.10 per k/m";; "jeep") echo "For $rental Rs.5 per k/m";; "bicycle") echo "For $rental 20 paisa per k/m";; *) echo "Sorry, I can not gat a $rental for you";; esac
Save it by pressing CTRL+D and run it as follows: $ chmod +x car $ car van $ car car $ car Maruti-800 First script will check, that if $1(first command line argument) is given or not, if NOT given set value of rental variable to "*** Unknown vehicle ***",if command line arg is supplied/given set value of rental variable to given value (command line arg). The $rental is compared against the patterns until a match is found. For first test run its match with van and it will show output "For van Rs.10 per k/m." For second test run it print, "For car Rs.20 per k/m". And for last run, there is no match for Maruti-800, hence default i.e. *) is executed and it prints, "Sorry, I can not gat a Maruti-800 for you". Note that esac is always required to indicate end of case statement.
Syntax: sh option { shell-script-name } OR bash option { shell-script-name } Option can be -v Print shell input lines as they are read. -x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments. Example: $ cat > dsh1.sh # # Script to show debug of shell # tot=`expr $1 + $2` echo $tot Press ctrl + d to save, and run it as $ chmod 755 dsh1.sh $ ./dsh1.sh 4 5 9 $ sh -x dsh1.sh 4 5 # # Script to show debug of shell # tot=`expr $1 + $2` expr $1 + $2 ++ expr 4 + 5 + tot=9 echo $tot + echo 9 9 See the above output, -x shows the exact values of variables (or statements are shown on screen with values). $ sh -v dsh1.sh 4 5 Use -v option to debug complex shell script.
Introduction - 4
After learning basis of shell scripting, its time to learn more advance features of shell scripting/command such as: 46
Functions User interface Conditional execution File Descriptors traps Multiple command line args handling etc
47
NOTE:-Empty line printed $ vech=Car $ echo $vech Car $ exit $ echo $vech Bus Command Meaning $ vech=Bus Create new local variable 'vech' with Bus as value in first shell $ echo $vech Print the contains of variable vech Now load second shell in memory (Which ignores all old shell's $ /bin/bash variable) $ echo $vech Print the contains of variable vech Create new local variable 'vech' with Car as value in second $ vech=Car shell $ echo $vech Print the contains of variable vech $ exit Exit from second shell return to first shell Print the contains of variable vech (Now you can see first shells $ echo $vech variable and its value) Global shell defined as: "You can copy old shell's variable to new shell (i.e. first shells variable to seconds shell), such variable is know as Global Shell variable." To set global varible you have to use export command. Syntax: export variable1, variable2,.....variableN Examples: $ vech=Bus $ echo $vech Bus $ export vech $ /bin/bash $ echo $vech Bus $ exit $ echo $vech Bus Command Meaning $ vech=Bus Create new local variable 'vech' with Bus as value in first shell 48
$ echo $vech $ export vech $ /bin/bash $ echo $vech $ exit $ echo $vech
Print the contains of variable vech Export first shells variable to second shell i.e. global varible Now load second shell in memory (Old shell's variable is accessed from second shell, if they are exported ) Print the contains of variable vech Exit from second shell return to first shell Print the contains of variable vech
49
$ cat > myf This is my file ^D (press CTRL + D to save file) Above command send output of cat command to myf file $ cal Above command prints calendar on screen, but if you wish to store this calendar to file then give command $ cal > mycal The cal command send output to mycal file. This is called output redirection. $ sort 10 -20 11 2 ^D -20 2 10 11 sort command takes input from keyboard and then sorts the number and prints (send) output to screen itself. If you wish to take input from file (for sort command) give command as follows: $ cat > nos 10 -20 11 2 ^D $ sort < nos -20 2 10 11 First you created the file nos using cat command, then nos file given as input to sort command which prints sorted numbers. This is called input redirection. In Linux (And in C programming Language) your keyboard, screen etc are all treated as files. Following are name of such files Standard File Descriptors Use Example File number as Standard stdin 0 Keyboard input as Standard stdout 1 Screen output stderr 2 as Standard Screen
50
error By default in Linux every program has three files associated with it, (when we start our program these three files are automatically opened by your shell). The use of first two files (i.e. stdin and stdout) , are already seen by us. The last file stderr (numbered as 2) is used by our program to print error on screen. You can redirect the output from a file descriptor directly to file with following syntax Syntax: file-descriptor-number>filename Examples: (Assemums the file bad_file_name111 does not exists) $ rm bad_file_name111 rm: cannot remove `bad_file_name111': No such file or directory Above command gives error as output, since you don't have file. Now if we try to redirect this error-output to file, it can not be send (redirect) to file, try as follows: $ rm bad_file_name111 > er Still it prints output on stderr as rm: cannot remove `bad_file_name111': No such file or directory, And if you see er file as $ cat er , this file is empty, since output is send to error device and you can not redirect it to copy this error-output to your file 'er'. To overcome this problem you have to use following command: $ rm bad_file_name111 2>er Note that no space are allowed between 2 and >, The 2>er directs the standard error output to file. 2 number is default number (file descriptors number) of stderr file. To clear your idea onsider another example by writing shell script as follows:
$ cat > demoscr if [ $# -ne 2 ] then echo "Error : Number are not supplied" echo "Usage : $0 number1 number2" exit 1 fi ans=`expr $1 + $2` echo "Sum is $ans"
Run it as follows: $ chmod 755 demoscr $ ./demoscr Error : Number are not supplied Usage : ./demoscr number1 number2 $ ./demoscr > er1 $ ./demoscr 5 7 Sum is 12 For first sample run , our script prints error message indicating that you have not given two number.
51
For second sample run, you have redirect output of script to file er1, since it's error we have to show it to user, It means we have to print our error message on stderr not on stdout. To overcome this problem replace above echo statements as follows echo "Error : Number are not supplied" 1>&2 echo "Usage : $0 number1 number2" 1>&2 Now if you run it as follows: $ ./demoscr > er1 Error : Number are not supplied Usage : ./demoscr number1 number2 It will print error message on stderr and not on stdout. The 1>&2 at the end of echo statement, directs the standard output (stdout) to standard error (stderr) device. Syntax: from>&destination
Functions
Humans are intelligent animals. They work together to perform all of life's task, in fact most of us depend upon each other. For e.g. you rely on milkman to supply milk, or teacher to learn new technology (if computer teacher). What all this mean is you can't perform all of life's task alone. You need somebody to help you to solve specific task/problem. The above logic also applies to computer program (shell script). When program gets complex we need to use divide and conquer technique. It means whenever programs gets complicated, we divide it into small chunks/entities which is know as function. Function is series of instruction/commands. Function performs particular activity in shell i.e. it had specific work to do or simply say task. To define function use following syntax: Syntax:
function-name ( ) { command1 command2 ..... ... commandN return }
Where function-name is name of you function, that executes series of commands. A return statement will terminate the function. Example: Type SayHello() at $ prompt as follows $ SayHello() { echo "Hello $LOGNAME, Have nice computing" 52
return } To execute this SayHello() function just type it name as follows: $ SayHello Hello vivek, Have nice computing. This way you can call function. Note that after restarting your computer you will loss this SayHello() function, since its created for current session only. To overcome this problem and to add you own function to automat some of the day today life task, add your function to /etc/bashrc file. To add function to this file you must logon as root. Following is the sample /etc/bashrc file with today() function , which is used to print formatted date. First logon as root or if you already logon with your name (your login is not root), and want to move to root account, then you can type following command , when asked for password type root (administrators) password $ su -l password: Open file /etc/bashrc using vi and goto the end of file (by pressing shift+G) and type the today() function:
# vi /etc/bashrc # At the end of file add following in /etc/bashrc file # # today() to print formatted date # # To run this function type today at the $ prompt # Added by Vivek to show function in Linux # today() { echo This is a `date +"%A %d in %B of %Y (%r)"` return }
Save the file and exit it, after all this modification your file may look like as follows (type command cat /etc/bashrc) # cat /etc/bashrc # /etc/bashrc # System wide functions and aliases # Environment stuff goes in /etc/profile # For some unknown reason bash refuses to inherit # PS1 in some circumstances that I can't figure out. # Putting PS1 here ensures that it gets loaded every time. PS1="[\u@\h \W]\\$ "
53
# # today() to print formatted date # # To run this function type today at the $ prompt # Added by Vivek to show function in Linux today() { echo This is a `date +"%A %d in %B of %Y (%r)"` return } To run function first completely logout by typing exit at the $ prompt (Or press CTRL + D, Note you may have to type exit (CTRL +D) twice if you login to root account by using su command) ,then login and type $ today , this way today() is available to all user in your system, If you want to add particular function to particular user then open .bashrc file in users home directory as follows:
# vi .bashrc OR # mcedit .bashrc At the end of file add following in .bashrc file SayBuy() { echo "Buy $LOGNAME ! Life never be the same, until you login again!" echo "Press a key to logout. . ." read return }
Save the file and exit it, after all this modification your file may look like as follows (type command cat .bashrc) # cat .bashrc # .bashrc # # User specific aliases and functions # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi SayBuy() { echo "Buy $LOGNAME ! Life never be the same, until you login again!" echo "Press a key to logout. . ." read
54
return } To run function first logout by typing exit at the $ prompt (Or press CTRL + D ) ,then logon and type $ SayBuy , this way SayBuy() is available to only in your login and not to all user in system, Use .bashrc file in your home directory to add User specific aliases and functions only. Tip: If you want to show some message or want to perform some action when you logout, Open file .bash_logout in your home directory and add your stuff here For e.g. When ever I logout, I want to show message Buy! Then open your .bash_logout file using text editor such as vi and add statement: echo "Buy $LOGNAME, Press a key. . ." read Save and exit from the file. Then to test this logout from your system by pressing CTRL + D (or type exit) immediately you will see message "Buy xxxxx, Press a key. . .", after pressing key you will be logout and login prompt will be shown to you. :-)
Saves lot of time. Avoids rewriting of same code again and again Program is easier to write. Program maintains is very easy.
55
$ cat > userinte # # Script to demo echo and read command for user interaction # echo "Your good name please :" read na echo "Your age please :" read age neyr=`expr $age + 1` echo "Hello $na, next year you will be $neyr yrs old."
Save it and run as $ chmod 755 userinte $ ./userinte Your good name please : Vivek Your age please : 25 Hello Vivek, next year you will be 26 yrs old. Even you can create menus to interact with user, first show menu option, then ask user to choose menu item, and take appropriate action according to selected menu item, this technique is show in following script:
$ cat > menuui # # Script to create simple menus and take action according to that selected # menu item # while : do clear echo "-------------------------------------" echo " Main Menu " echo "-------------------------------------" echo "[1] Show Todays date/time" echo "[2] Show files in current directory" echo "[3] Show calendar" echo "[4] Start editor to write letters" echo "[5] Exit/Stop" echo "=======================" echo -n "Enter your menu choice [1-5]: " read yourch case $yourch in 1) echo "Today is `date` , press a key. . ." ; read ;; 2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;; 3) cal ; echo "Press a key. . ." ; read ;; 4) vi ;; 5) exit 0 ;; *) echo "Opps!!! Please select choice 1,2,3,4, or 5";
56
Above all statement explained in following table: Statement Explanation Start infinite loop, this loop will only break if you select 5 ( i.e. Exit/Stop menu item) as your menu choice Start loop Clear the screen, each and every time
while :
do clear echo "-------------------------------------" echo " Main Menu " echo "-------------------------------------" echo "[1] Show Todays date/time" echo "[2] Show files in current directory" echo "[3] Show calendar" echo "[4] Start editor to write letters" echo "[5] Exit/Stop" echo "=======================" echo -n "Enter your menu choice [1-5]: " read yourch case $yourch in 1) echo "Today is `date` , press a key. . ." ; read ;; 2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;; 3) cal ; echo "Press a key. . ." ; read ;; 4) vi ;; 5) exit 0 ;; *) echo "Opps!!! Please select choice 1,2,3,4, or 5"; echo "Press a key. . ." ; read ;; esac done
Ask user to enter menu item number Read menu item number from user
Take appropriate action according to selected menu item, If menu item is not between 1 - 5, then show error and ask user to input number between 1-5 again
User interface usually includes, menus, different type of boxes like info box, message box, Input box etc. In Linux shell (i.e. bash) there is no built-in facility available to create 57
such user interface, But there is one utility supplied with Red Hat Linux version 6.0 called dialog, which is used to create different type of boxes like info box, message box, menu box, Input box etc. Next section shows you how to use dialog utility.
Save the shell script and run it as: $ chmod +x dia1 $ ./dia1
58
After executing this dialog statement you will see box on screen with titled as "Welcome to Linux Dialog Utility" and message "This is dialog....Press any key. . ." inside this box. The title of box is specified by --title option and infobox with --infobox "Message" with this option. Here 7 and 50 are height-of-box and width-of-box respectively. "Linux Shell Script Tutorial" is the backtitle of dialog show on upper left side of screen and below that line is drawn. Use dialog utility to Display dialog boxes from shell scripts. Syntax:
dialog --title {title} --backtitle {backtitle} {Box options} where Box options can be any one of following --yesno {text} {height} {width} --msgbox {text} {height} {width} --infobox {text} {height} {width} --inputbox {text} {height} {width} [{init}] --textbox {file} {height} {width} --menu {text} {height} {width} {menu} {height} {tag1} item1}...
59
60
file" 7 60 sel=$? case $sel in 0) echo "User select to delete file";; 1) echo "User select not to delete file";; 255) echo "Canceled by user by pressing [ESC] key";; esac Save the script and run it as: $ chmod +x dia3 $ ./dia3
Above script creates yesno type dialog box, which is used to ask some questions to the user , and answer to those question either yes or no. After asking question how do we know, whether user has press yes or no button ? The answer is exit status, if user press yes button exit status will be zero, if user press no button exit status will be one and if user press Escape key to cancel dialog box exit status will be one 255. That is what we have tested in our above shell script as Statement sel=$? Meaning Get exit status of dialog utility
61
case $sel in Now take action according to exit 0) echo "You select to delete file";; status of dialog utility, if exit status 1) echo "You select not to delete file";; is 0 , delete file, if exit status is 1 do 255) echo "Canceled by you by pressing [Escape] not delete file and if exit status is key";; 255, means Escape key is pressed. esac
62
Inputbox is used to take input from user, In this example we are taking Name of user as input. But where we are going to store inputted name, the answer is to redirect inputted name to file via statement 2>/tmp/input.$$ at the end of dialog command, which means send screen output to file called /tmp/input.$$, letter we can retrieve this inputted name and store to variable as follows na=`cat /tmp/input.$$`. For input box's exit status refer the following table: Exit Status for Input box 0 1 255 Meaning Command is successful Cancel button is pressed by user Escape key is pressed by user
63
Calendar Editor and action for each menu-item is follows : MENUITEM Date/time Calendar Editor ACTION Show current date/time Show calendar Start vi Editor
$ cat > smenu # #How to create small menu using dialog # dialog --backtitle "Linux Shell Script Tutorial " --title "Main\ Menu" --menu "Move using [UP] [DOWN],[Enter] to\ Select" 15 50 3\ Date/time "Shows Date and Time"\ Calendar "To see calendar "\ Editor "To start vi editor " 2>/tmp/menuitem.$$ menuitem=`cat /tmp/menuitem.$$` opt=$? case $menuitem in Date/time) date;; Calendar) cal;; Editor) vi;; esac
64
--menu option is used of dialog utility to create menus, menu option take --menu options "Move using [UP] [DOWN],[Enter] to Select" 15 50 3 Meaning This is text show before menu
Height of box Width of box Height of menu First menu item called as tag1 (i.e. Date/time) Date/time "Shows Date and Time" and description for menu item called as item1 (i.e. "Shows Date and Time") First menu item called as tag2 (i.e. Calendar) and description for menu item called as item2 (i.e. Calendar "To see calendar " "To see calendar") First menu item called as tag3 (i.e. Editor) and Editor "To start vi editor " description for menu item called as item3 (i.e."To start vi editor") Send selected menu item (tag) to this temporary 2>/tmp/menuitem.$$ file
65
After creating menus, user selects menu-item by pressing the ENTER key, selected choice is redirected to temporary file, Next this menu-item is retrieved from temporary file and following case statement compare the menu-item and takes appropriate step according to selected menu item. As you see, dialog utility allows more powerful user interaction then the older read and echo statement. The only problem with dialog utility is it work slowly.
trap command
Consider following script example:
$ cat > testsign ls -R /
Save and run it as $ chmod +x testsign $ ./testsign Now if you press ctrl + c , while running this script, script get terminated. The ctrl + c here work as signal, When such signal occurs its send to all process currently running in your system. Now consider following shell script:
66
$ cat > testsign1 # # Why to trap signal, version 1 # Take_input1() { recno=0 clear echo "Appointment Note keeper Application for Linux" echo -n "Enter your database file name : " read filename if [ ! -f $filename ]; then echo "Sorry, $filename does not exit, Creating $filename database" echo "Appointment Note keeper Application database file" > $filename fi echo "Data entry start data: `date`" >/tmp/input0.$$ # # Set a infinite loop # while : do echo -n "Appointment Title:" read na echo -n "time :" read ti echo -n "Any Remark :" read remark echo -n "Is data okay (y/n) ?" read ans if [ $ans = y -o $ans = Y ]; then recno=`expr $recno + 1` echo "$recno. $na $ti $remark" >> /tmp/input0.$$ fi echo -n "Add next appointment (y/n)?" read isnext if [ $isnext = n -o $isnext = N ]; then cat /tmp/input0.$$ >> $filename rm -f /tmp/input0.$$ return # terminate loop fi done } # # # Call our user define function : Take_input1 # Take_input1
Save it and run as $ chmod +x testsign1 $ ./testsign1 It first ask you main database file where all appointment of the day is stored, if no such database file found, file is created, after that it open one temporary file in /tmp directory, and puts today's date in that file. Then one infinite loop begins, which ask appointment 67
title, time and remark, if this information is correct its written to temporary file, After that, script asks user , whether he/she wants to add next appointment record, if yes then next record is added , otherwise all records are copied from temporary file to database file and then loop will be terminated. You can view your database file by using cat command. Now problem is that while running this script, if you press CTRL + C, your shell script gets terminated and temporary file are left in /tmp directory. For e.g. try it as follows $./testsign1 After given database file name and after adding at least one appointment record to temporary file press CTRL+C, Our script get terminated, and it left temporary file in /tmp directory, you can check this by giving command as follows $ ls /tmp/input* Our script needs to detect such signal (event) when occurs; To achieve this we have to first detect Signal using trap command. Syntax: trap {commands} {signal number list} Signal Number 0 1 2 3 9 When occurs shell exit hangup interrupt (CTRL+C) quit kill (cannot be caught)
To catch signal in above script, put trap statement before calling Take_input1 function as trap del_file 2 ., Here trap command called del_file() when 2 number interrupt ( i.e. CTRL+C ) occurs. Open above script in editor and modify it so that at the end it will look like as follows:
68
$ vi testsign1 # # signal is trapped to delete temporary file , version 2 # del_file() { echo "* * * CTRL + C Trap Occurs (removing temporary file)* * *" rm -f /tmp/input0.$$ exit 1 }
Take_input1() { recno=0 clear echo "Appointment Note keeper Application for Linux" echo -n "Enter your database file name : " read filename if [ ! -f $filename ]; then echo "Sorry, $filename does not exit, Creating $filename database" echo "Appointment Note keeper Application database file" > $filename fi echo "Data entry start data: `date`" >/tmp/input0.$$ # # Set a infinite loop # while : do echo -n "Appointment Title:" read na echo -n "time :" read ti echo -n "Any Remark :" read remark echo -n "Is data okay (y/n) ?" read ans if [ $ans = y -o $ans = Y ]; then recno=`expr $recno + 1` echo "$recno. $na $ti $remark" >> /tmp/input0.$$ fi echo -n "Add next appointment (y/n)?" read isnext if [ $isnext = n -o $isnext = N ]; then cat /tmp/input0.$$ >> $filename rm -f /tmp/input0.$$ return # terminate loop fi done # end_while } # # Set trap to for CTRL+C interrupt i.e. Install our error handler # When occurs it first it calls del_file() and then exit # trap del_file 2 # # Call our user define function : Take_input1
69
Run the script as: $ ./testsign1 After giving database file name and after giving appointment title press CTRL+C, Here we have already captured this CTRL + C signal (interrupt), so first our function del_file() is called, in which it gives message as "* * * CTRL + C Trap Occurs (removing temporary file)* * * " and then it remove our temporary file and then exit with exit status 1. Now check /tmp directory as follows $ ls /tmp/input* Now Shell will report no such temporary file exit.
Excute above script as follows: $ chmod +x shiftdemo.sh $ ./shiftdemo -f foo bar Current command line args are: $1=-f, $2=foo, $3=bar After shift command the args are: $1=foo, $2=bar, $3= You can also move the positional parameters over more than one place by specifying a number with the shift command. The following command would shift the positional parameters two places: shift 2
You can use shift command to parse the command line (args) option. For example consider the following simple shell script:
$ vi convert while [ "$1" ] do if [ "$1" = "-b" ]; then ob="$2" case $ob in 16) basesystem="Hex";; 8) basesystem="Oct";; 2) basesystem="bin";; *) basesystem="Unknown";; esac shift 2 elif [ "$1" = "-n" ] then num="$2" shift 2 else echo "Program $0 does not recognize option $1" exit 1 fi done output=`echo "obase=$ob;ibase=10; $num;" | bc` echo "$num Decimal number = $output in $basesystem number system(base=$ob)"
Save and run the above shell script as follows: $ chmod +x convert $ ./convert -b 16 -n 500 500 Decimal number = 1F4 in Hex number system(base=16) $ ./convert -b 8 -n 500 500 Decimal number = 764 in Oct number system(base=8) $ ./convert -b 2 -n 500 500 Decimal number = 111110100 in bin number system(base=2) $ ./convert -b 2 -v 500 Program ./convert does not recognize option -v $ ./convert -t 2 -v 500 Program ./convert does not recognize option -t $ ./convert -b 4 -n 500 500 Decimal number = 13310 in Unknown number system(base=4) $ ./convert -n 500 -b 16 500 Decimal number = 1F4 in Hex number system(base=16) Above script is run in variety of ways. First three sample run converts the number 500 ( -n 500 ) to respectively 1F4 (hexadecimal number i.e. -b 16), 764 (octal number i.e. -b 16) , 111110100 (binary number i.e. -b 16). It use -n and -b as command line option which means: -b {base-system i.e. 16,8,2 to which -n number to convert} -n {Number to convert to -b base-system} 71
Fourth and fifth sample run produce the error "Program ./convert does not recognize option -v". This is because these two (-v & -t) are not the valid command line option. Sixth sample run produced output "500 Decimal number = 13310 in Unknown number system(base=4)". Because the base system 4 is unknown to our script. Last sample run shows that command line options can given different ways i.e. you can use it as follows: $ ./convert -n 500 -b 16 Instead of $ ./convert -b 16 -n 500 All the shell script command can be explained as follows: Command(s)/Statements while [ "$1" ] do Explanation Begins the while loop; continue the while loop as long as script reads the all command line option Now start to parse the command line (args) option using if command our script understands the -b and -n options only all other option are invalid. If option is -b then stores the value of second command line arg to variable ob (i.e. if arg is -b 16 then store the 16 to ob) For easy understanding of conversion we store the respective number base systems corresponding string to basesystem variable. If base system is 16 then store the Hex to basesystem and so on. This is done using case statement. Once first two command line options (args) are read, we need next two command line option (args). shift 2 will moves the current values stored in the positional parameters (command line 72
done output=`echo "obase=$ob;ibase=10; $num;" | BC` echo "$num Decimal number = $output in $basesystem number system(base=$ob)"
args) to the left two position. Now check the next command line option and if its -n option then stores the value of second command line arg to variable num (i.e. if arg is -n 500 then store the 500 to num) and shift 2 will moves the current values stored in the positional parameters (command line args) to the left two position. If command line option is not -n or -b then print the error "Program ./convert does not recognize option xx" on screen and terminates the shell script using exit 1 statement. End of loop as we read all the valid command line option/args. Now convert the given number to given number system using BC Show the converted number on screen.
As you can see shift command can use to parse the command line (args) option. This is useful if you have limited number of command line option. If command line options are too many then this approach works slowly as well as complex to write and maintained. You need to use another shell built in command - getopts. Next section shows the use of getopts command. You still need the shift command in conjunction with getopts and for other shell scripting work.
getopts command
This command is used to check valid command line argument are passed to script. Usually used in while loop. Syntax: getopts {optsring} {variable1} getopts is used by shell to parse command line argument. As defined in man pages: "optstring contains the option letters to be recognized; if a letter is followed by a colon, 73
the option is expected to have an argument, which should be separated from it by white space. Each time it is invoked, getopts places the next option in the shell variable variable1, When an option requires an argument, getopts places that argument into the variable OPTARG. On errors getopts diagnostic messages are printed when illegal options or missing option arguments are encountered. If an illegal option is seen, getopts places ? into variable1." Examlpe: We have script called ani which has syntax as ani -n -a -s -w -d Options: These are optional argument -n name of animal -a age of animal -s sex of animal -w weight of animal -d demo values (if any of the above options are used their values are not taken) Above ani script is as follows:
74
$ vi ani # # Usage: ani -n -a -s -w -d # # # help_ani() To print help # help_ani() { echo "Usage: $0 -n -a -s -w -d" echo "Options: These are optional argument" echo " -n name of animal" echo " -a age of animal" echo " -s sex of animal " echo " -w weight of animal" echo " -d demo values (if any of the above options are used " echo " their values are not taken)" exit 1 } # #Start main procedure # # #Set default value for variable # isdef=0 na=Moti age="2 Months" # may be 60 days, as U like it! sex=Male weight=3Kg # #if no argument # if [ $# -lt 1 ]; then help_ani fi while getopts n:a:s:w:d opt do case "$opt" in n) na="$OPTARG";; a) age="$OPTARG";; s) sex="$OPTARG";; w) weight="$OPTARG";; d) isdef=1;; \?) help_ani;; esac done if [ $isdef -eq 0 ] then echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (user define mode)" else na="Pluto Dog" age=3 sex=Male weight=20kg echo "Animal Name: $na, Age: $age, Sex: $sex, Weight: $weight (demo mode)"
75
Save it and run as follows $ chmod +x ani $ ani -n Lassie -a 4 -s Female -w 20Kg $ ani -a 4 -s Female -n Lassie -w 20Kg $ ani -n Lassie -s Female -w 20Kg -a 4 $ ani -w 20Kg -s Female -n Lassie -a 4 $ ani -w 20Kg -s Female $ ani -n Lassie -a 4 $ ani -n Lassie $ ani -a 2 See because of getopts, we can pass command line argument in different style. Following are invalid options for ani script $ ani -nLassie -a4 -sFemal -w20Kg No space between option and their value. $ ani -nLassie-a4-sFemal-w20Kg $ ani -n Lassie -a 4 -s Female -w 20Kg -c Mammal -c is not one of the valid options.
Introduction - 5
Linux contains powerful utility programs. You can use these utility to
Locate system information For better file management To organize your data System administration etc
Following section introduce you to some of the essential utilities as well as expression. While programming shell you need to use these essential utilities. Some of these utilities (especially sed & awk) requires understanding of expression. After the quick introduction to utilities, you will learn the expression.
76
sname Sr.No Name 11 Vivek 12 Renuka 13 Prakash 14 Ashish 15 Rani smark Sr.No 11 12 13 14 15 Mark 67 55 96 36 67
77
You can redirect output of cut utility as follows $cut -f2 sname > /tmp/sn.tmp.$$ $cut -f2 smark > /tmp/sm.tmp.$$ $cat /tmp/sn.tmp.$$ Vivek Renuka Prakash Ashish Rani $cat /tmp/sm.tmp.$$ 67 55 96 36 67 General Syntax of cut utility: Syntax: cut -f{field number} {file-name} Use of Cut utility: Selecting portion of a file.
78
General Syntax of paste utility: Syntax: paste {file1} {file2} Use of paste utility: Putting lines together. Can you note down basic difference between cut and paste utility?
79
You can clearly see that each occurrence of character 'h' is replace with '3' and '2' with 'x'. tr utility translate specific characters into other specific characters or range of characters into other ranges. h -> 3 2 -> x Consider following example: (after executing command type text in lower case) $ tr "[a-z]" "[A-Z]" hi i am Vivek HI I AM VIVEK what a magic WHAT A MAGIC {Press CTRL + C to terminate.} Here tr translate range of characters (i.e. small a to z) into other (i.e. to Capital A to Z) ranges. General Syntax & use of tr utility: Syntax: tr {pattern-1} {pattern-2} Use of tr utility: To translate range of characters into other range of characters. After typing following paragraph, I came to know my mistake that entire paragraph must be in lowercase characters, how to correct this mistake? (Hint - Use tr utility) $ cat > lcommunity.txt THIS IS SAMPLE PARAGRAPH WRITTEN FOR LINUX COMMUNITY, BY VIVEK G GITE (WHO ELSE?) OKAY THAT IS OLD STORY.
80
After crating file issue command $ awk '/good/ { print $3 }' inventory 10 12 5 awk utility, select each record from file containing the word "good" and performs the action of printing the third field (Quantity of available goods.). Now try the following and note down its output. $ awk '/good/ { print $1 " " $3 }' inventory General Syntax of awk utility: Syntax: awk 'pattern action' {file-name} For $ awk '/good/ { print $3 }' inventory example, /good/ Is the pattern used for selecting lines from file. This is the action; if pattern found, print on of such action. Here $3 means third {print $3} record in selected record. (What $1 and $2 mean?) inventory File which is used by awk utility which is use as input for awk utility. Use of awk utility: To manipulate data.
81
sed utility is used to find every occurrence of tea and replace it with word milk. sed Steam line editor which uses 'ex' editors command for editing text files without starting ex. (Cool!, isn't it? no use of text editor to edit anything!!!) Find tea word or select all lines having the word tea Replace (substitute) the word milk for s//milk/ the tea. g Make the changes globally. /tea/ Syntax: sed {expression} {file} Use of sed utility: sed is used to edit (text transformation) on given stream i.e a file or may be input from a pipeline.
82
oaky! how are u luser? what still I remeber that name. Above command prints those lines which are unique. For e.g. our original file contains 12333 twice, so additional copies of 12333 are deleted. But if you examine output of uniq, you will notice that 12333 is gone (Duplicate), and "what still I remeber that name" remains as its. Because the uniq utility compare only adjacent lines, duplicate lines must be next to each other in the file. To solve this problem you can use command as follows $ sort personame | uniq General Syntax of uniq utility: Syntax: uniq {file-name}
83
Introduction - 6
In the chpater 5, "Quick Tour of essential utilities", you have seen basic utilities. If you use them with other tools, these utilities are very useful for data processing or for other works. In rest part of tutorial we will learn more about patterns, filters, expressions, and off course sed and awk in depth.
So you must know how to construct regular expression. In the next part of LSST you will learn how to construct regular expression using ex editor. For this part of chapter/tutorial create 'demofile' - text file using any text editor.
84
Example: $ ex demofile The : (colon) is ex prompt where you can type ex text editor command or regular expression. Its time to open our demofile, use ex as follows: $ ex demofile "demofile" [noeol] 20L, 387C Entering Ex mode. Type "visual" to go to Normal mode. : As you can see, you will get : prompt, here you can type ex command, type q and press ENTER key to exit from ex as shown follows: (remember commands are case sensetive) :q vivek@ls vivek]$ After typing the q command you are exit to shell prompt.
85
Deleting lines
86
Give command :1, d I love linux. NOTE Here 1 is 1st line and d command indicates deletes (Which deletes the 1st line). You can even delete range of line by giving command as :1,5 d
Coping lines
Give command as follows :1,4 co $ :1,$ p I love linux. It is different from all other Os .... ..... . (DOT) is special command of linux. Okay! I will stop. I love linux. It is different from all other Os My brother Vikrant also loves linux. NOTE Here 1,4 means copy 1 to 4 lines; co command stands for copy; $ is end of file. So it mean copy first four line to end of file. You can delete this line as follows :18,21 d Okay! I will stop. :1,$ p I love linux. It is different from all other Os My brother Vikrant also loves linux. He currently lerarns linux. Linux is cooool.
87
Linux is now 10 years old. Next year linux will be 11 year old. Rani my sister never uses Linux She only loves to play games and nothing else. Do you know? . (DOT) is special command of linux. Okay! I will stop.
88
Quitting the ex
Give command :q q command quits from ex and you are return to shell prompt. Note use wq command to do save and exit from ex.
89
Hello World. This is vivek from Poona. .... .. ..... . (DOT) is special command of linux. Okay! I will stop. Using above command, you are substituting all lines i.e. s command will find all of the address line for the pattern "Linux" and if pattern "Linux" found substitute pattern "Unix". Command Explanation :1,$ Substitute for all line s Substitute /Linux/ Target pattern If target pattern found substitute the Unix/ expression (i.e. Unix/ ) Even you can also use contextual address as follows :/sister/ p Rani my sister never uses Unix :g /sister/ s/never/always/ :p Rani my sister always uses Unix Above command will first find the line containing pattern "sister" if found then it will substitute the pattern "always" for the pattern "never" (It mean find the line containing the word sister, on that line find the word never and replace it with word always.) Try the following and watch the output very carefully. :g /Unix/ s/Unix/Linux 3 substitutions on 3 lines Above command finds all line containing the regular expression "Unix", then substitute "Linux" for all occurrences of "Unix". Note that above command can be also written as follows :g /Unix/ s//Linux Here // is replace by the last pattern/regular expression i.e. Unix. Its shortcut. Now try the following :g /Linux/ s//UNIX/ 3 substitutions on 3 lines :g/Linux/p 90
Linux is cooool. Linux is now 10 years old. Rani my sister always uses Linux :g /Linux/ s//UNIX/ 3 substitutions on 3 lines :g/UNIX/p UNIX is cooool. UNIX is now 10 years old. Rani my sister always uses UNIX By default substitute command only substitute first occurrence of a pattern on a line. Let's take another example, give command :/brother/p My brother Vikrant also loves linux who also loves unix. Now in above line "also" word is occurred twice, give the following substitute command :g/brother/ s/also/XYZ/ :/brother/p My brother Vikrant XYZ loves linux who also loves unix. Make sure next time it works :g/brother/ s/XYZ/also/ Note that "also" is only once substituted. If you want to s command to work with all occurrences of pattern within a address line give command as follows: :g/brother/ s/also/XYZ/g :p My brother Vikrant XYZ loves linux who XYZ loves unix. :g/brother/ s/XYZ/also/g :p My brother Vikrant also loves linux who also loves unix. The g option at the end instruct s command to perform replacement on all occurrences of the target pattern within a address line.
91
After giving this command ex will ask you question like - replace with UNIX (y/n/a/q/^E/^Y)? Type y to replace the word or n to not replace or a to replace all occurrence of word.
Finding words
Command like :g/the/p It is different from all other Os My brother Vikrant also loves linux who also loves unix. Will find word like theater, the, brother, other etc. What if you want to just find the word like "the" ? To find the word (Let's say Linux) you can give command like :/\<Linux\> Linux is cooool. :g/\<Linux\>/p Linux is cooool. Linux is now 10 years old. Rani my sister never uses Linux The symbol \< and \> respectively match the empty string at the beginning and end of the word. To find the line which contain Linux pattern at the beginning give command :/^Linux Linux is cooool. As you know $ is end of line character, the ^ (caret) match beginning of line. To find all occurrence of pattern "Linux" at the beginning of line give command :g/^Linux Linux is cooool. Linux is now 10 years old. And if you want to find "Linux" at the end of line then give command :/Linux $ Rani my sister never uses Linux Following command will find empty line: :/^$ To find all blank line give command: :g/^$ To view entire file without blank line you can use command as follows: :g/[^/^$] Hello World. This is vivek from Poona.
92
I love linux. It is different from all other Os My brother Vikrant also loves linux who also loves unix. He currently learn linux. Linux is cooool. Linux is now 10 years old. Next year linux will be 11 year old. Rani my sister never uses Linux She only loves to play games and nothing else. Do you know? . (DOT) is special command of linux. Okay! I will stop. Command Explanation g All occurrence /[^ [^] This means not Empty line, Combination of /^$ ^ and $. To delete all blank line you can give command as follows :g/^$/d Okay! I will stop. :1,$ p Hello World. This is vivek from Poona. I love linux. It is different from all other Os My brother Vikrant also loves linux who also loves unix. He currently learn linux. Linux is cooool. Linux is now 10 years old. Next year linux will be 11 year old. Rani my sister never uses Linux She only loves to play games and nothing else. Do you know? . (DOT) is special command of linux. Okay! I will stop. Try u command to undo, to undo what you have done it, give it as follows: :u :1,$ p Hello World. This is vivek from Poona. .... ...
93
[:alpha:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]
Letters A to Z or a to z Delete character or ordinary control character (0x7F or 0x00 to 0x1F) Digit (0 to 9) Printing character, like print, except that a space character is excluded Lowercase letter (a to z) Printing character (0x20 to 0x7E) Punctuation character (ctrl or space) Space, tab, carriage return, new line, vertical tab, or form feed (0x09 to 0x0D, 0x20) Uppercase letter (A to Z) Hexadecimal digit (0 to 9, A to F, a to f)
For e.g. To find digit or alphabet (Upper as well as lower) you will write :/[0-9A-Za-Z] Instead of writing such command you could easily use predefined classes or range as follows :/[[:alnum:]] The . (dot) matches any single character. For e.g. Type following command :g/\<.o\> She only loves to play games and nothing else. Do you know? This will include lo(ves), Do, no(thing) etc. * Matches the zero or more times For e.g. Type following command :g/L* Hello World. This is vivek from Poona. .... .... :g/Li* Linux is cooool. Linux is now 10 years old. Rani my sister never uses Linux
95
:g/c.*and . (DOT) is special command of linux. Here first c character is matched, then any single character (.) followed by n number of single character (1 or 100 times even) and finally ends with and. This can found different word as follows command or catand etc. In the regular expression metacharacters such . (DOT) or * loss their special meaning if we use as \. or \*. The backslash removes the special meaning of such meatcharacters and you can use them as ordinary characters. For e.g. If u want to search . (DOT) character at the beginning of line, then you can't use command as follows :g/^. Hello World. This is vivek from Poona. .... .. ... . (DOT) is special command of linux. Okay! I will stop. Instead of that use :g/^\. . (DOT) is special command of linux.
96
97
Pattern { action 1 action 2 action N } awk reads the input from given file (or from stdin also) one line at a time, then each line is compared with pattern. If pattern is match for each line then given action is taken. Pattern can be regular expressions. Following is the summery of common awk metacharacters: Metacharacter Meaning . (Dot) Match any character * Match zero or more character ^ Match beginning of line $ Match end of line \ Escape character following [] List {} Match range of instance + Match one more preceding ? Match zero or one preceding | Separate choices to match
In above file fields are Sr.No,Product,Qty,Unit Price. Field is the smallest element of any record. Each fields has its own attributes. For e.g. Take Qty. field. Qty. fields attribute is its numerical (Can contain only numerical data). Collection of fields is know as record. So 1. Pen 5 20.00 ----> Is a Record.
98
Collection of record is know as database file. In above text database file each field is separated using space (or tab character) and record is separated using new-line character ( i.e. each record is finished at the end of line ). In the awk, fields are access using special variable. For e.g. In above database $1, $2, $3, $4 respectively represents Sr.No, Product, Qty, Unit Price fields. (Don't confuse $1,$2 etc with command line arguments of shell script) For this part of tutorial create text datafile inven (Shown as above). Now enter following simple awk program/command at shell prompt: $ awk '{ print $1 $2 "--> Rs." $3 * $4 }' inven 1.Pen--> Rs.100 2.Pencil--> Rs.20 3.Rubber--> Rs.10.5 4.Cock--> Rs.91 Above awk program/command can be explained as follows: Explanation print command is used to print contains of variables or text enclose in " text ". Here $1, $2, $3,$4 are all the special '{ print $1 $2 "--> Rs." $3 variable. $1, $2, etc all of the variable contains value of * $4 } ' field. Finally we can directly do the calculation using $3 * $4 i.e. multiplication of third and fourth field in database. Note that "--> Rs." is string which is printed as its. Note $1,$2 etc (in awk) also know as predefined variable and can assign any value found in field. Type following awk program at shell prompt, $ awk '{ print $2 }' inven Pen Pencil Rubber Cock awk prints second field from file. Same way if you want to print second and fourth field from file then give following command: $awk '{ print $2 $4}' inven Pen20.00 Pencil2.00 Rubber3.50 Cock45.50 $0 is special variable of awk , which print entire record, you can verify this by issuing following awk command: $ awk '{ print $0 }' inven 99 awk program statement
1. Pen 5 20.00 2. Pencil 10 2.00 3. Rubber 3 3.50 4. Cock 2 45.50 You can also create awk command (program) file as follows:
$ cat > prn_pen /Pen/ { print $3 }
And then you can execute or run above "prn_pen" awk command file as follows $ awk -f prn_pen inven 5 10 In above awk program /Pen/ is the search pattern, if this pattern is found on line (or record) then print the third field of record. { print $3 } is called Action. On shell prompt , $ awk -f prn_pen inven , -f option instruct awk, to read its command from given file, inven is the name of database file which is taken as input for awk. Now create following awk program as follows:
$cat > comp_inv 3 > 5 { print $0 }
Run it as follows: $ awk -f comp_inv inven 2. Pencil 10 2.00 Here third field of database is compared with 5, this the pattern. If this pattern found on any line database, then entire record is printed.
Run it as follows. $awk -f def_var inven Printing Rec. #1(1. Pen 5 20.00),And # of field for this record is 4 100
Printing Rec. #2(2. Pencil 10 2.00),And # of field for this record is 4 Printing Rec. #3(3. Rubber 3 3.50),And # of field for this record is 4 Printing Rec. #4(4. Cock 2 45.50),And # of field for this record is 4 NR and NF are predefined variables of awk which means Number of input Record, Number of Fields in input record respectively. In above example NR is changed as our input record changes, and NF is constant as there are only 4 field per record. Following table shows list of such built in awk variables. awk Meaning Variable FILENAME Name of current input file Input record separator character RS (Default is new line) Output field separator string (Blank is OFS default) Output record separator string (Default ORS is new line) NF Number of input record NR Number of fields in input record OFMT Output format of number Field separator character (Blank & tab FS is default)
math
$1 $1 $1 $1 $1 " " " " " + " - " / " x " mod $2 " $2 " $2 " $2 " " $2 = = = = " " " " " = $1 + $1 $1 / $1 * " $1 $2 $2 $2 $2 % $2
20 x 3 = 60 20 mod 3 = 2 (Press CTRL + D to terminate) In above program print $1 " + " $2 " = " $1 + $2, statement is used for addition purpose. Here $1 + $2, means add (+) first field with second field. Same way you can do (subtraction ), * (Multiplication), / (Division), % (modular use to find remainder of division operation).
math1
Run the program as follows $ awk -f math1 15 1+5=6 In the above program, no1, no2, ans all are user defined variables. Value of first and second field are assigned to no1, no2 variable respectively and the addition to ans variable. Value of variable can be print using print statement as, print no1 " + " no2 " = " ans. Note that print statement prints whatever enclosed in double quotes (" text ") as it is. If string is not enclosed in double quotes its treated as variable. Also above two program takes input from stdin (Keyboard) instead of file. Now try the following awk program and note down its output.
$ cat > bill { total = $3 * $4 recno = $1 item = $2 print recno item " Rs." total }
102
3.Rubber Rs.10.5 4.Cock Rs.91 Here we are printing the total price of each product (By multiplying third field with fourth field). Following program prints total price of each product as well as the Grand total of all product in the bracket.
$ cat > bill1 { total = $3 * $4 recno = $1 item = $2 gtotal = gtotal + total print recno item " Rs." total " [Total Rs." gtotal "] "
} Run the above awk program as follows: $ awk -f bill1 inven 1.Pen Rs.100 [Total Rs.100] 2.Pencil Rs.20 [Total Rs.120] 3.Rubber Rs.10.5 [Total Rs.130.5] 4.Cock Rs.91 [Total Rs.221.5] In this program, gtotal variable holds the grand total. It adds the total of each product as gtotal = gtotal + total. Finally this total is printed with each record in the bracket. But their is one problem with our script, Grand total mostly printed at the end of all record. To solve this problem we have to use special BEGIN and END Patterns of awk. First take the example,
$ cat > bill2 BEGIN { print "---------------------------" print "Bill for the 4-March-2001. " print "By Vivek G Gite. " print "---------------------------" } { total = $3 * $4 recno = $1 item = $2 gtotal += total print recno item " Rs." total
103
Run it as $awk -f bill2 inven --------------------------Bill for the 4-March-2001. By Vivek G Gite. --------------------------1.Pen Rs.100 2.Pencil Rs.20 3.Rubber Rs.10.5 4.Cock Rs.91 --------------------------Total Rs.221.5 =============== Now the grand total is printed at the end. In above program BEGIN and END patters are used. BEGIN instruct awk, that perform BEGIN actions before the first line (Record) has been read from database file. Use BEGIN pattern to set value of variables, to print heading for report etc. General syntax of BEGIN is as follows Syntax: BEGIN { action 1 action 2 action N } END instruct awk, that perform END actions after reading all lines (RECORD) from the database file. General syntax of END is as follows: END { action 1 action 2 action N } In our example, BEGIN is used to print heading and END is used print grand total.
104
total = $3 * $4 recno = $1 item = $2 gtotal += total printf "%d %s Rs.%f\n", recno, item, total #printf "%2d %-10s Rs.%7.2f\n", recno, item, total
END { printf "---------------------------\n" printf "Total Rs. %f\n" ,gtotal #printf "\tTotal Rs. %7.2f\n" ,gtotal printf "===========================\n" }
Run it as follows: $ awk -f bill3 inven Bill for the 4-March-2001. By Vivek G Gite. --------------------------1 Pen Rs.100.000000 2 Pencil Rs.20.000000 3 Rubber Rs.10.500000 4 Cock Rs.91.000000 --------------------------Total Rs. 221.500000 =============== In above example printf statement is used to print formatted output of the variables or text. General syntax of printf as follows: Syntax: printf "format" ,var1, var2, var N If you just want to print any text using try printf as follows printf "Hello" printf "Hello World\n" In last example \n is used to print new line. Its Part of escape sequence following may be also used: \t for tab \a Alert or bell \" Print double quote etc For e.g. printf "\nAn apple a day, keeps away\t\t\tDoctor\n\a\a" It will print text on new line as : An apple a day, keeps away Doctor Notice that twice the sound of bell is produced by \a\a. To print the value of decimal
105
number use %d as format specification code followed by the variable name. For e.g. printf "%d" , no1 It will print the value of no1. Following table shows such common format specification code: Format Specification Code %c Character } { %d Decimal number such as 10,-5 etc } { %x Hexadecimal number such as 0xA, 0xffff etc } { %s String such as "vivek", "Good buy" } To run above example simply create any awk program file as follows
$ cat > p_demo BEGIN { n = 10 printf "%d", n printf "\nAn apple a day, keeps away\t\t\tDoctor\n\a\a" }
Meaning {
n = 10 printf "%d",n
n = 10 printf "%x",n
str1 = "Welcome to Linux!" printf "%s", str1 printf "%s", "Can print ?"
} END { printf "---------------------------\n" printf "\tTotal Rs. %6.2f\n" ,gtotal printf "===========================\n" }
Run it as $ awk -f bill4 inven Bill for the 4-March-2001. By Vivek G Gite. --------------------------1 Pen Rs. 100.00 2 Pencil Rs. 20.00 3 Rubber Rs. 10.50 4 Cock Rs. 91.00 --------------------------Total Rs. 221.50 =============== From the above output you can clearly see that printf can format the output. Let's try to understand formatting of printf statement. For e.g. %2d, number between % and d, tells the printf that assign 2 spaces for value. Same way if you write following awk program ,
$ cat > prf_demo { na = $1 printf "|%s|", na printf "|%10s|", na printf "|%-10s|", na }
107
Run it as follows (and type the God) $ awk -f prf_demo God |God| | God| |God | (press CTRL + D to terminate) printf "|%s|", na printf "| %10s|", na printf "|%10s|", na Print God as its Print God Word as Right justified. Print God Word as left justified. (- means left justified)
Same technique is used in our bill4 awk program to print formatted output. Also the statement like gtotal += total, which is equvalent to gtotal = gtotal + total. Here += is called assignment operator. You can use following assignment operator: Assignment operator += -= *= %= Use for Assign the result of addition Assign the result of subtraction Assign the result of multiplication Assign the result of modulo Example a += 10 d += c a -= 10 d -= c a *= 10 d *= c a %= 10 d %= c Equivalent to a = a + 10 a=a+c a = a - 10 a=a-c a = a * 10 a=a*c a = a % 10 a=a%c
if condition in awk
General syntax of if condition is as follows: Syntx: if ( condition ) { Statement 1 Statement 2 Statement N if condition is TRUE } else 108
{ Statement 1 Statement 2 Statement N if condition is FALSE } Above if syntax is selfexplontary, now lets move to next awk program
$ awk > math2 BEGIN { myprompt = "(To Stop press CTRL+D) > " printf "Welcome to MyAddtion calculation awk program v0.1\n" printf "%s" ,myprompt } { no1 = $1 op = $2 no2 = $3 ans = 0 if ( op == "+" ) { ans = $1 + $3 printf "%d %c %d = %d\n" ,no1,op,no2,ans printf "%s" ,myprompt } else { printf "Opps!Error I only know how to add.\nSyntax: number1 + number2\n" printf "%s" ,myprompt } } END { printf "\nGoodbuy %s\n" , ENVIRON["USER"] }
Run it as follows (Give input as 5 + 2 and 3 - 1 which is shown in bold words) $awk -f math2 Welcome to MyAddtion calculation awk program v0.1 (To Stop press CTRL+D) > 5 + 2 5+2=7 (To Stop press CTRL+D) > 3 - 1 Opps!Error I only know how to add. Syntax: number1 + number2
109
(To Stop press CTRL+D) > Goodbuy vivek In the above program various, new concept are introduce so lets try to understand them step by step BEGIN { myprompt = "(To Stop press CTRL+D) > " Start of BEGIN Pattern Define user define variable Print welcome message and value of myprompt variable. End of BEGIN Pattern Now start to process input Assign first, second, third, variables value to no1, op, no2 variables respectively If command is used for decision making in awk program. Here if value of variable op is "+" then addition is done and result is printed on screen, else error message is shown on screen. Stop all inputted lines are process. END patterns 110
printf "Welcome to MyAddtion calculation awk program v0.1\n" printf "%s" ,myprompt
if ( op == "+" ) { ans = no1 + no2 printf "%d %c %d = %d\n" ,no1,op,no2,ans printf "%s" ,myprompt } else { printf "Opps!Error I only know how to add.\nSyntax:number1+ number2\n" printf "%s" ,myprompt }
} END {
ENVIRON is the one of the predefined system variable that is array. Array is made up of different element. ENVIRON array is also made of elements. It allows you to access system variable (or variable in your environment). Give set command at shell prompt to see list of your environment variable. You can use variable name to reference any element in this array. For e.g. If you want to print your home directory you can write printf as follows: printf "%s is my sweet home", ENVIRON["HOME"]
Loops in awk
For loop and while loop are used for looping purpose in awk. Syntax of for loop Syntax: for (expr1; condition; expr2) { Statement 1 Statement 2 Statement N } Statement(s) are executed repeatedly UNTIL the condition is true. BEFORE the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop. AFTER each iteration of the loop, expr2 is evaluated. This is usually used to increment a loop counter. Example:
$ cat > while01.awk BEGIN{ printf "Press ENTER to continue with for loop example from LSST v1.05r3\n" } { sum = 0 i = 1 for (i=1; i<=10; i++) { sum += i; # sum = sum + i } printf "Sum for 1 to 10 numbers = %d \nGoodbuy!\n\n", sum exit 1
111
Run it as follows: $ awk -f while01.awk Press ENTER to continue with for loop example from LSST v1.05r3 Sum for 1 to 10 numbers = 55 Goodbuy Above for loops prints the sum of all numbers between 1 to 10, it does use very simple for loop to achieve this. It take number from 1 to 10 using i variable and add it to sum variable as sum = previous sum + current number (i.e. i). Consider the one more example of for loop:
$ cat > for_loop BEGIN { printf "To test for loop\n" printf "Press CTRL + C to stop\n" } { for(i=0;i<NF;i++) { printf "Welcome %s, %d times.\n" ,ENVIRON["USER"], i } }
Run it as (and give input as Welcome to Linux!) $ awk -f for_loop To test for loop Press CTRL + C to Stop Welcome to Linux! Welcome vivek, 0 times. Welcome vivek, 1 times. Welcome vivek, 2 times. Program uses for loop as follows: Set the value of i to 0 (Zero); Continue as long as value of i is less than NF (Remember NF is built in variable, which mean Number of Fields in record); increment i by 1 (i++) Print "Welcome...." message, with user name who is currently log on and value of i.
Here i++, is equivalent to i = i + 1 statement. ++ is increment operator which increase the value of variable by one and -- is decrement operator which decrease the value of
112
variable by one. Don't try i+++, to increase the value of i by 2 (since +++ is not valid operator), instead try i+= 2. You can use while loop as follows: Syntax: while (condition) { statement1 statement2 statementN Continue as long as given condition is TRUE } While loop will continue as long as given condition is TRUE. To understand the while loop lets write one more awk script:
$ cat > while_loop { no = $1 remn = 0 while ( no > 1 ) { remn = no % 10 no /= 10 printf "%d" ,remn } printf "\nNext number please (CTRL+D to stop):"; }
Run it as $awk -f while_loop 654 456 Next number please(CTRL+D to stop):587 785 Next number please(CTRL+D to stop): Here user enters the number 654 which is printed in reverse order i.e. 456. Above program can be explained as follows: no = $1 remn = 0 { while (no > 1) remn = no % 10 Set the first fields ($1) value to no. Set remn variable to zero Start the while loop Continue the loop as long as value of no is greater than one Find the remainder of no variable, and assign result to remn variable. 113
Divide the no by 10 and store result to no variable. Print the remn (remainder) variables value. End of while loop, since condition (no>1) is not true i.e false condition.. Prompt for next number
114
END{ }
Run it as follows: $ awk -f temp2final.awk filelist.conf Above awk Program can be explained as follows: sfile = $1 dfile = $2 cpcmd = "cp " $1 " " $2 printf "Coping %s to %s\n",sfile,dfile system(cpcmd) Set source file path i.e. first field ($1) from the file filelist.conf Set source file path i.e. second field ($2) from the file filelist.conf Use your normal cp command for copy file from source to destination. Here cpcmd, variable is used to construct cp command. Now print the message Issue the actual cp command using system(), function.
system() function execute given system command. For e.g. if you want to remove file using rm command of Linux, you can write system as follows system("rm foo") OR dcmd = "rm " $1 system(dcmd) The output of command is not available to program; but system() returns the exit code (error code) using which you can determine whether command is successful or not. For e.g. We want to see whether rm command is successful or not, you can write code as follows:
$ cat > tryrmsys { dcmd = "rm " $1 if ( system(dcmd) != 0 ) printf "rm command not successful\n" else printf "rm command is successful and %s file is removed \n", $1 }
Run it as (assume that file foo exist and bar does not exist) $ awk -f tryrmsys foo rm command is successful and foo file is removed bar rm command not successful
115
(Press CTRL + D to terminate) Our Second Example: As I write visual installation guide, I use to capture lot of images for my work, while capturing images I saved all images (i.e. file names) in UPPER CASE for e.g. RH7x01.JPG,RH7x02.JPG,...RH7x138.JPG. Now I would like to rename all files to lowercase then I tried with following two scripts: up2low and rename.awk up2low can be explained as follows: Statements/Command AWK_SCRIPT="rename.awk" awkspath=$HOME/bin/$AWK_SCRIPT Explanation Name of awk scripts that renames file Where our awk script is installed usguall it shoude installed under yourhome-directory/bin (something like /home/vivek/bin) ls -1 > /tmp/file1.$$ List all files in current working directory line by line and send output to /tmp/file1.$$ file. tr "[A-Z]" "[a-z]" < /tmp/file1.$$ > /tmp/file2.$$ Now convert all Uppercase filename to lowercase and store them to /tmp/file2.$ $ file. paste /tmp/file1.$$ /tmp/file2.$$ > /tmp/tmpdb.$$ Now paste both Uppercase filename and lowercase filename to third file called /tmp/tmpdb.$$ file rm -f /tmp/file1.$$ Remove both file1.$$ and file2.$$ files rm -f /tmp/file2.$$ if [ -f $awkspath ]; then See if rename.awk script installed, if awk -f $awkspath /tmp/tmpdb.$$ not installed give error message on else screen. If installed call the rename.awk echo -e "\n$0: Fatal error - $awkspath not found" script and give it /tep/tepdb.$$ path to echo -e "\nMake sure \$awkspath is set correctly read all filenames from this file. in $0 script\n" fi rm -f /tmp/tmpdb.$$ Remove the temporary file. rename.awk can be explained as follows:
116
Explanation This expression is quite tricky. Its something as follows: isdir1 = [ -d $1 ] Which means see if directory exists using [ expr ]. As you know [ expr ] is used to test whether expr is true or not. So we are testing whether directory exist or not. What does $1 mean? If you remember, in awk $1 is the first field. isdir2 = "[ -d " $2 " ] " As above except it test for second field as isdir2 = [ -d $2 ] i.e. Whether second field is directory or not. scriptname = "up2low" Our shell script name (up2low) and awkscriptname = "rename.awk" awk script name (rename.awk). sfile = $1 Source file dfile = $2 Destination file if ( sfile == scriptname || sfile == awkscriptname ) Make sure we don't accidentally next rename our own scripts, if scripts are in current working directory else if( ( system(isdir1) ) == 0 || system((isdir2)) == 0 Make sure source or destination are ) files and not the directory. We { check this using [ expr ] command printf "%s or %s is directory can't rename it to lower of bash. From the awk script you case\n",sfile,dfile can called or invoke (as official we next # continue with next recored called it) the [ expr ] if directory do } exists it will return true (indicated by zero) and if not it will return nonzero value. else if ( sfile == dfile ) If both source and destination file { are same, it mean file already in printf "Skiping, \"%s\" is alrady in lower case no need to rename it to lowercase\n",sfile lower case. next } else # everythink is okay rename it to lowercase Now if source and destination files { are not mvcmd = "mv " $1 " " $2 printf "Renaming %s to %s\n",sfile,dfile Directories
117
system(mvcmd) }
Note that if you don't have files name in UPPER case for testing purpose you can create files name as follows:
$ for j in 1 2 3 4 5 6 7 8 9 10; do touch TEMP$j.TXT; done
Above sample command creates files as TEMP1.TXT,TEMP2.TXT,....TEMP10.TXT files. Run it as follows: $ up2low Letters or letters is directory can't rename it to lower case RH6_FILES or rh6_files is directory can't rename it to lower case Renaming RH7x01.JPG to rh7x01.jpg Renaming RH7x02.JPG to rh7x02.jpg Renaming RH7x03.JPG to rh7x03.jpg Renaming RH7x04.JPG to rh7x04.jpg Renaming RH7x05.JPG to rh7x05.jpg Renaming RH7x06.JPG to rh7x06.jpg .... .. .... Renaming RH7x138.JPG to rh7x138.jpg On my workstation above output is shown.
awk miscellaneous
You can even take input from keyboard while running awk script, try the following awk script:
$ cat > testusrip BEGIN { printf "Your name please:" getline na < "-" printf "%s your age please:",na getline age < "-" print "Hello " na, ", next year you will be " age + 1 }
118
Save it and run as $ awk -f testusrip Your name please: Vivek Vivek your age please: 26 Hello Vivek, next year you will be 27 Here getline function is used to read input from keyboard and then assign the data (inputted from keyboard) to variable. Syntax: getline variable-name < "-" | | | 1 2 3 1 --> getline is function name 2 --> variable-name is used to assign the value read from input 3 --> Means read from stdin (keyboard) To reading Input from file use following Syntax: getline < "file-name" Example: getline < "friends.db" To reading Input from pipe use following Syntax: "command" | getline Example:
$ cat > awkread_file BEGIN { "date" | getline print $0
} Run it as $ awk -f awkread_file Fri Apr 12 00:05:45 IST 2002 Command date is executed and its piped to getline which assign the date command output to variable $0. If you want your own variable then replace the above program as follows
$ cat > awkread_file1 BEGIN { "date" | getline today
119
print today
} Run it as follows: $ awk -f awkread_file1 Try to understand the following awk script and note down its output. temp2final1.awk
Start the sed command Use substitute command to replace Linux with 's/Linux/UNIX(system UNIX(system v). General syntax of substitute is v)/' s/pattern/pattern-to-substitute/' demofile1 Read the data from demofile1 General Syntax of sed Syntax: sed -option 'general expression' [data-file] sed -option sed-script-file [data-file] Option can be: 120
Option -e -f
-n
Meaning Read the different sed command from command line. Read the sed command from sed script file. Suppress the output of sed command. When -n is used you must use p command of print flag.
Example $ sed -e 'sed-commands' data-file-name $ sed -e 's/Linux/UNIX(system v)/' demofile1 $sed -f sed-script-file data-file-name $ sed -f chgdb.sed friends.tdb $ sed -n '/^\*..$/p' demofile2
121
Run the above sed script as follows: $ sed -f chg1.sed inven1 Price of all items changes from 1st-April-2001 1. Pen 5 19.5 2. Pencil 10 2.60 3. Rubber 3 4.25 4. Cock 2 51.00 In above sed script, the 1i\ is the (i) insert command. General Syntax is as follows: Syntax: [line-address]i\ text So, 1i\ Price of all items changes from 1st-April-2001 means insert the text "Price of all items changes from 1st-April-2001" at line number 1. Same way you can use append (a) or change (c) command in your sed script, General Syntax of append Syntax: [line-address]a\ text Example: /INDIA/ a\ E-mail: vg@indiamail.co.in Find the word INDIA and append (a) "E-mail: vg@indiamail.co.in" text. General Syntax of change as follows: Syntax: [line-address]c\ text Example: /INDIA/ c\ E-mail: vg@indiamail.co.in Find the word INDIA and change e-mail id to "vg@indiamail.co.in" Rest of the statements (like /Pen/s/20.00/19.5/) are general substitute statements.
122
123
$sed -e '/^\*\{2,3\}$/,/^\*\{2,3\}$/d' demofile2 > /tmp/fi.$$ $cat /tmp/fi.$$ Above expression can be explained as follows Expression ^ \* \{2,3\} $ , ^\*\{2,3\}$ d Meaning Beginning of line Find the asterisk or star (\ remove the special meaning of '*' metacharacter) Find next two asterisk End of line Next range or search pattern Same as above Now delete all lines between *** and *** range
You can group the commands in sed - scripts as shown following example
$ cat > dem_gsed /^\*\{2,3\}$/,/^\*\{2,3\}$/{ /^$/d s/Linux/Linux-Unix/ }<
Now save above sed script and run it as follows: $ sed -f dem_gsed demofile2 > /tmp/fi.$$ $ cat /tmp/fi.$$ Above sed scripts finds all line between *** and *** and performance following operations 1) Delete blank line, if any using /^$/d expression. 2) Substitute "Linux-Unix" for "Linux" word using s/Linux/Linux-Unix/ expression. Our next example removes all blank line and converts multiple spaces into single space, for this purpose you need demofile3 file. Write sed script as follows:
$ cat > rmblksp /^$/d s/ */ /g<
Run above script as follows: $ sed -f rmblksp demofile3 Welcome to word of sed what sed is? I don't know what sed is but I think Rani knows what sed Is -------------------------------------------------124
Above script can be explained as follows: Expression Meaning /^$/d Find all blank line and delete is using d command. Find two or more than two blank space and replace it with s/ */ /g single blank space Note that indicates two blank space and indicate one blank space.
For our next and last example create database file friends Our task is as follows for friends database file: 1)Find all occurrence of "A'bad" word replace it with "Aurangabad" word 2)Exapand MH state value to Maharastra 3)Find all blank line and replace with actual line (i.e. ========) 4)Instert e-mail address of each persons at the end of persons postal address. For each person e-mail ID is different To achieve all above task write sed script as follows:
125
$ cat > mkchgfrddb s/A.bad/Aurangabad/g s/MH/Maharastra/g s/^$/===================================================================/g /V.K. /{ N N a\ email:vk@fackmail.co.in } /M.M. /{ N N a\ email:mm@fackmail.co.in } /R.K. /{ N N a\ email:rk@fackmail.co.in } /A.G. / { N N a\ email:ag@fackmail.co.in } /N.K. / { N N a\ email:nk@fackmail.co.in }
Run it as follows: $ sed -f mkchgfrddb friends > updated_friendsdb $ cat updated_friendsdb Above script can be explained as follows: Expression s/A.bad/Aurangabad/g Meaning Substitute Aurangabad for A'bad. Note that here second character in A'bad is ' (single quote), to match this single quote we have to use . (DOT - Special Metacharcter) that matches any single character.
126
Substitute Maharastra for MH Substitute blank line with actual line Match the pattern and follow the command between { and }, if pattern found. Here we are finding each friends initial name if it matches then we are going to end of his address (by giving N command twice) and appending (a command) friends e-mail address at the end.
Our last examples shows how we can manipulate text data files using sed. Here our tutorial on sed/awk ends but next version (LSST ver 2.0) will cover more real life examples, case studies using all these tools, plus integration with shell scripts etc.
127
Q.5.Write Script to see current date, time, username, and current directory Answer: See Q5 shell Script. Q.6.Write script to print given number in reverse order, for eg. If no is 123 it must print as 321. Answer: See Q6 shell Script. Q.7.Write script to print given numbers sum of all digit, For eg. If no is 123 it's sum of all digit will be 1+2+3 = 6. Answer: See Q7 shell Script. Q.8.How to perform real number (number with decimal point) calculation in Linux Answer: Use Linux's bc command Q.9.How to calculate 5.12 + 2.5 real number calculation at $ prompt in Shell ? Answer: Use command as , $ echo 5.12 + 2.5 | bc , here we are giving echo commands output to bc to calculate the 5.12 + 2.5 Q.10.How to perform real number calculation in shell script and store result to third variable , lets say a=5.66, b=8.67, c=a+b? Answer: See Q10 shell Script. Q.11.Write script to determine whether given file exist or not, file name is supplied as command line argument, also check for sufficient number of command line argument Answer: See Q11 shell Script. Q.12.Write script to determine whether given command line argument ($1) contains "*" symbol or not, if $1 does not contains "*" symbol add it to $1, otherwise show message "Symbol is not required". For e.g. If we called this script Q12 then after giving , $ Q12 /bin Here $1 is /bin, it should check whether "*" symbol is present or not if not it should print Required i.e. /bin/*, and if symbol present then Symbol is not required must be printed. Test your script as $ Q12 /bin $ Q12 /bin/* Answer: See Q12 shell Script Q.13. Write script to print contains of file from given line number to next given number of lines. For e.g. If we called this script as Q13 and run as $ Q13 5 5 myf , Here print contains of 'myf' file from line number 5 to next 5 line of that file. Answer: See Q13 shell Script Q.14. Write script to implement getopts statement, your script should understand following command line argument called this script Q14, Q14 -c -d -m -e
128
Where options work as -c clear the screen -d show list of files in current working directory -m start mc (midnight commander shell) , if installed -e { editor } start this { editor } if installed Answer: See Q14 shell Script Q.15. Write script called sayHello, put this script into your startup file called .bash_profile, the script should run as soon as you logon to system, and it print any one of the following message in infobox using dialog utility, if installed in your system, If dialog utility is not installed then use echo statement to print message : Good Morning Good Afternoon Good Evening , according to system time. Answer: See Q15 shell Script Q.16. How to write script, that will print, Message "Hello World" , in Bold and Blink effect, and in different colors like red, brown etc using echo command. Answer: See Q16 shell Script Q.17. Write script to implement background process that will continually print current time in upper right corner of the screen , while user can do his/her normal job at $ prompt. Answer: See Q17 shell Script. Q.18. Write shell script to implement menus using dialog utility. Menu-items and action according to select menu-item is as follows: Menu-Item Date/time Calendar Purpose To see current date time To see current calendar Action for Menu-Item Date and time must be shown using infobox of dialog utility Calendar must be shown using infobox of dialog utility First ask user name of directory where all files are present, if no name of directory given assumes current directory, then show all files only of that directory, Files must be shown on screen using menus of dialog utility, let the user select the file, then ask the confirmation to user whether he/she wants to delete selected file, if answer is yes then delete the file , report errors if any while deleting file to user. Exit/Stops the menu driven program i.e. this script
Delete
Exit
Note: Create function for all action for e.g. To show date/time on screen create function show_datetime(). Answer: See Q18 shell Script.
129
Q.19. Write shell script to show various system configuration like 1) Currently logged user and his logname 2) Your current shell 3) Your home directory 4) Your operating system type 5) Your current path setting 6) Your current working directory 7) Show Currently logged number of users 8) About your os and version ,release number , kernel version 9) Show all available shells 10) Show mouse settings 11) Show computer cpu information like processor type, speed etc 12) Show memory information 13) Show hard disk information like size of hard-disk, cache memory, model etc 14) File system (Mounted) Answer: See Q19 shell Script. Q.20.Write shell script using for loop to print the following patterns on screen for2 for3 for4
for5
for6
for7
for8
for8
for9
130
Answer: Click on above the links to see the scripts. Q.21.Write shell script to convert file names from UPPERCASE to lowercase file names or vice versa. Answer: See the rename.awk - awk script and up2sh shell script.
Introduction
This is new chapter added to LSST v1.05r3, its gives more references to other material available on shell scripting on Net or else ware. It also indicates some other resources which might be useful while programming the shell. Appendix - A Information This tutorial/document is useful for beginners who wish to learn Linux file system, it covers basic concept of file system, commands or utilities related with file system. It will explain basic file concepts such as what is file & directories, what are the mount points, how to use cdrom or floppy drive under Linux. This command reference is specially written for the LSST. It contains command name, general syntax followed by an example. This is useful while programming shell and you can used as Quick Linux Command Reference guide.
131