Tuesday, August 12, 2008

Shell Scripting

© Moreniche

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 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

Meaning

BASH=/bin/bash

Our shell name

BASH_VERSION=1.14.7(1)

Our shell version name

COLUMNS=80

No. of columns for our screen

HOME=/home/vivek

Our home directory

LINES=25

No. of columns for our screen

LOGNAME=students

students Our logging name

OSTYPE=Linux

Our Os type

PATH=/usr/bin:/sbin:/bin:/usr/sbin

Our path settings

PWD=/home/students/Common

Our current working directory

SHELL=/bin/bash

Our shell name

USERNAME=vivek

User name who is currently login to this PC

How to define User defined variables (UDV)

To define UDV use following syntax
Syntax:
variable name=value

'value' is assigned to given 'variable name' and Value must be on right side = sign.

Example:
$ no=10# this is ok
$ 10=no# Error, NOT Ok, Value must be on right side of = sign.
To define variable called 'vech' having value Bus
$ vech=Bus
To define variable called n having value 10
$ n=10

How to print or access value of UDV (User defined variables)

To print or access UDV use following syntax
Syntax:
$variablename

Define variable vech and n as follows:
$ vech=Bus
$ n=10

To print contains of variable 'vech' type
$ echo $vech
It will print 'Bus',To print contains of variable 'n' type command as follows
$ echo $n

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"

Shell Arithmetic

Use to perform arithmetic operations.

Syntax:
expr op1 math-operator op2

Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `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

There are three types of quotes

Quotes

Name

Meaning

"

Double Quotes

"Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except \ and $).

'

Single quotes

'Single quotes' - Enclosed in single quotes remains unchanged.

`

Back quote


`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.

The read Statement

Use to get input (data from user) from keyboard and store (data) to variable.
Syntax:
read variable1, variable2,...variableN

Following script first ask user, name and then waits to enter name from the user via keyboard. Then user enters name from keyboard (after giving name you have to press ENTER key) and entered name through keyboard is stored (assigned) to variable fname.

$ vi sayH
#
#Script to read your name from key-board
#
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

Run it as follows:
$ chmod 755 sayH
$ ./sayH

Your first name please: vivek
Hello vivek, Lets be friend!

Wild cards (Filename Shorthand or meta Characters)

Wild card /Shorthand

Meaning

Examples

*

Matches any string or group of characters.

$ ls *

will show all files

$ ls a*

will show all files whose first name is starting with letter 'a'

$ ls *.c

will show all files having extension .c

$ ls ut*.c

will show all files having extension .c but file name must begin with 'ut'.

?

Matches any single character.

$ ls ?

will show all files whose names are 1 character long

$ ls fo?

will show all files whose names are 3 character long and file name begin with fo

[...]

Matches any one of the enclosed characters

$ ls [abc]*

will show all files beginning with letters a,b,c

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/rvi /bin/sleep /bin/touch /bin/view
/bin/pwd /bin/rview /bin/sort /bin/true /bin/wcomp
/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

More command on one command line

Syntax:
command1;command2
To run two command with one command line.

Examples:
$ date;who
Will print today's date followed by users who are currently login. Note that You can't use
$ date who
for same purpose, you must put semicolon in between date and who command.

Why Command Line arguments required

  1. Telling the command/utility which option to use.
  2. Informing the utility/command which file or group of files to process (reading/writing of files).

Let's take rm command, which is used to remove file, but which file you want to remove and how you will tail this to rm command (even rm command don't ask you name of file that you would like to remove). So what we do is we write command as follows:
$ rm {file-name}
Here rm is command and filename is file which you would like to remove. This way you tail rm command which file you would like to remove. So we are doing one way communication with our command by specifying filename Also you can pass command line arguments to your script to make it more users friendly. But how we access command line argument in our script.

Lets take ls command
$ Ls -a /*
This command has 2 command line argument -a and /* is another. For shell script,
$ myshell foo bar

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

Shell Script Name

No. Of Arguments to script

Actual Argument ($1,..$9)

$0

$#

$1

$2

$3

$4

$5

Sum

2

11

20

Math

3

4

-

7

D

0

Bp

3

-5

myf

+20

Ls

1

*

Cal

0

FindBS

4

4

8

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

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"

Redirection of Standard output/input i.e. Input - Output redirection

Mostly all command gives output on screen or take input from keyboard, but in Linux (and in other OSs also) it's possible to send output to file or to read input from file.

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 <>

Pipes

A pipe is a way to connect the output of one program to the input of another program without any temporary file.

Pipe - Redirecting output of 1st command to 2nd without creating 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."

Syntax:
command1 | command2

Examles:

Command using Pipes

Meaning or Use of Pipes

$ ls | more

Output of ls command is given as input to more command So that output is printed one screen full page at a time.

$ who | sort

Output of who command is given as input to sort command So that it will print sorted list of users

$ who | sort > user_list

Same as above except output of sort is send to (redirected) user_list file

$ who | wc -l

Output of who command is given as input to wc command So that it will number of user who logon to system

$ ls -l | wc -l

Output of ls command is given as input to wc command So that it will print number of files in current directory.

$ who | grep raju

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 <>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 <> u_sname

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.

Command Related with Process

Following tables most commonly used command(s) with process:

For this purpose

Use this Command

Examples*

To see currently running process

ps

$ ps

To stop any process by PID i.e. to kill process

kill {PID}

$ kill 1012

To stop processes by name i.e. to kill process

killall {Process-name}

$ killall httpd

To get information about all running process

ps -ag

$ ps -ag

To stop all process except your shell

kill 0

$ kill 0

For background processing (With &, use to put particular command and program in background)

linux-command &

$ ls / -R | wc -l &

To display the owner of the processes along with the processes

ps aux

$ ps aux

To see if a particular process is running or not. For this purpose you have to use ps command in combination with the grep command

ps ax | grep process-U-want-to see

For e.g. you want to see whether Apache web server process is running or not then give command

$ ps ax | grep httpd

To see currently running processes and other information like memory and CPU usage with real time updates.

top
See the output of top command.


$ top


Note
that to exit from top 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.

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.

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
#
# 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?

test command or [ expr ]

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]

Example:
Following script determine whether given argument number is positive.

$ cat > ispostive
#!/bin/sh
#
# Script to see whether argument is positive
#
if test $1 -gt 0
then
echo "$1 number is positive"
fi

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

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

Meaning

Normal Arithmetical/ Mathematical Statements

But in Shell

For test statement with if command

For [ expr ] statement with if command

-eq

is equal to

5 == 6

if test 5 -eq 6

if [ 5 -eq 6 ]

-ne

is not equal to

5 != 6

if test 5 -ne 6

if [ 5 -ne 6 ]

-lt

is less than

5 <>

if test 5 -lt 6

if [ 5 -lt 6 ]

-le

is less than or equal to

5 <= 6

if test 5 -le 6

if [ 5 -le 6 ]

-gt

is greater than

5 > 6

if test 5 -gt 6

if [ 5 -gt 6 ]

-ge

is greater than or equal to

5 >= 6

if test 5 -ge 6

if [ 5 -ge 6 ]

NOTE: == is equal, != is not equal.

For string Comparisons use

Operator

Meaning

string1 = string2

string1 is equal to string2

string1 != string2

string1 is NOT equal to string2

string1

string1 is NOT NULL or not defined

-n string1

string1 is NOT NULL and does exist

-z string1

string1 is NULL and does exist

Shell also test for file and directory types

Test

Meaning

-s file

Non empty file

-f file

Is File exist or normal file and not a directory

-d dir

Is Directory exist and not a file

-w file

Is writeable file

-r file

Is read-only file

-x file

Is file is executable

Logical Operators

Logical operators are used to combine two or more condition at a time

Operator

Meaning

! expression

Logical NOT

expression1 -a expression2

Logical AND

expression1 -o expression2

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

For e.g. Write Script as follows:

$ 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).

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 "1. Unix (Sun Os)"
echo "2. Linux (Red Hat)"
echo -n "Select your os choice [1 or 2]? "
read 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.

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:

           if condition
           then
                       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

For multilevel if-then-else statement try the following script:

$ 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

Loops in Shell Scripts

Loop defined as:
"Computer can repeat particular instruction again and again, until particular condition satisfies. A group of instruction that is executed repeatedly is called a loop."

Bash supports:

  • for loop
  • while loop

Note that in each and every loop,

(a) First, the variable used in loop condition must be initialized, then execution of the loop begins.

(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 list is
                     not finished (And repeat all statement between do and done)
            done

Before try to understand above syntax try the following script:

$ cat > testfor
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

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:

n=$1

Set the value of command line argument to variable n. (Here it's set to 7 )

i=1

Set variable i to 1

while [ $i -le 10 ]

This is our loop condition, here if value of i is less than 10 then, shell execute all statements between do and done

do

Start loop

echo "$n * $i = `expr $i \* $n`"

Print multiplication table as
7 * 1 = 7
7 * 2 = 14
....
7 * 10 = 70, Here each time value of variable n is multiply be i.

i=`expr $i + 1`

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)

done

Loop stops here if i is not less than 10 i.e. condition of loop is not true. Hence
loop is terminated.

The case Statement

The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to match several values against one variable. Its easier to read and write.
Syntax:

           case  $variable-name  in
                pattern1)   command
                                ...
                                ..
                                command;;
                pattern2)   command
                                ...
                                ..
                                command;;
                patternN)   command
                                ...
                                ..
                                command;;
                *)             command
                                ...
                                ..
                                command;;
           esac

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.

How to de-bug the shell script?

While programming shell sometimes you need to find the errors (bugs) in shell script and correct the errors (remove errors - debug). For this purpose you can use -v and -x option with sh or bash command to debug the shell script. General syntax is as follows:
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.

/dev/null - Use to send unwanted output of program

This is special Linux file which is used to send any unwanted output from program/command.
Syntax:
command > /dev/null

Example:
$ ls > /dev/null
Output of above command is not shown on screen its send to this special file. The /dev directory contains other device files. The files in this directory mostly represent peripheral devices such disks like floppy disk, sound card, line printers etc. See the file system tutorial for more information on Linux disk, partition and file system.

No comments:

Search

My Blog List