“Line Segments is Our Thoughts To Scribble on Unix…”

UNIX Shell Scripting

Session Plan
• Shell Programming
• Introduction • Uses of Shell Scripts • Variables

Types of Variables, Rules, Assignment Operations

Type Conversions (String to Integer & Float) • Statements

Non - Executable and Executable


10/17/08

Positional Parameters
2

• Signals and Trap
Confidential

© Tech Mahindra 2006

10/17/08

Introduction (Contd.)
What is Shell Programming ?
It is a series of user commands that can be done in a sequential manner, that carries out an entire procedure. Such programs are known as Shell Scripts. There are around 280 shell scripts that comes with the Unix Operating System Note : 1. Shell program written in Bourne shell are likely to work with Korn shell but the reverse is not true. 10/17/08 2. Scripts written for © Tech Mahindra 2006 one shell may not work with 10/17/08 Confidential

3

Uses of Shell Script
 Customizing your work environment Eg.: When you log in if you want to see the current date, time and welcome message.  Automating your daily tasks. Eg.: For back up all programs everyday at evening time.  Automating repetitive tasks. Eg.: Producing sales report every month.  Performing same operation on many files
Eg.: Converting cerr to cout in all .cc files

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

4

Variables
The Value which is going to have change in nature is known as variable. Types of Variables Shell Variables

System Variables
10/17/08
Confidential

User-defined variables

© Tech Mahindra 2006

10/17/08

5

Variables

(Contd.)

System Variables These Variables can be accessed anywhere from the shell. The following table shows some of the system variables. Variable Meaning PS2 PATH HOME LOGNAME MAIL
10/17/08
Confidential

System prompt 2, (By default value is “>”) Defines the path which the shell must search in order to execute any command or file. working directory of Stores the default the user Stores the login name of the user Defines the file where the mail of the user is stored.
© Tech Mahindra 2006 10/17/08

6

Variables
System Variables (Contd…) Variable IFS

(Contd.)

Meaning

SHELL TERM TZ

Defines Internal Field Separator, which is a space, a tab or a newline (By default the value is space). Eg.: echo $IFS Defines the name of your default working shell. Eg.: echo $SHELL Defines the name of the terminal on which we are working. Eg.: echo $TERM the name of the time zone on Defines which you are working.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

7

Variables

(Contd.)

User Defined Variables These are all defined by the user either in the shell prompt or in the shell script. Rules
 Variable can be any combination of alphabets, digits and    

10/17/08

underscore No commas or blanks are allowed within the variable First character must be either an alphabet or underscore User defined variable name can be of any length User defined variable is case sensitive (i.e., bms, Bms, BMS are all different variables) Keywords cannot be used as variable names
Confidential

© Tech Mahindra 2006

10/17/08

8

Variables (Contd.)
Note 1. By default the values assigned to the :

10/17/08

variables in Shell Prog. will be considered as String values. 2. User defined variables will have their scope till the execution of the script. 3. In Shell programming there is no need to declare the variables as in C language. 4. While assigning more than one value to a variable it has to be enclosed within quotes (either using “ or ‘). 5. Printing a null variable, only blank line appears on the screen. 6. No spaces should be given before and after =
Confidential

© Tech Mahindra 2006

10/17/08

9

Variables (Contd.)
Eg.:

a. var=20 Assigns a value 20 to the user defined variable var. b. name=bms perc=75 Assigns a value bms and 75 to the variable named name and perc respectively. While assigning a value to more than one variable in a line it has be separated by a delimiter (i.e., space) c. name=“bms” (or) name=‘bms’ (or) name=bms All remains the same. If we are assigning only one word to the variable the quotes are optional.

10/17/08

d. coll=“Jyoti Nivas” (or) coll=‘Jyoti Nivas’ While assigning moreMahindra 2006 word to the than one 10/17/08 Confidential © Tech

10

Variables (Contd.)
Eg.: e. spa= “ “ (or) spa=‘ ‘ (or) spa= Assigns a null value to the variable named spa. While printing a variable without any value. The shell will print a Blank Line. f. rd=‘welcome to shell programming’ readonly rd The variable rd becomes a read only variable. (i.e., We cannot change the value of the read only variable named rd till the end of the script). g. rd=‘welcome to shell programming’ echo $rd unset rd echo $rd First echo statement will print the value of the variable rd. The unset command is used to remove 10/17/08 the variable rd from the memory and the second 10/17/08 Confidential © Tech Mahindra 2006 echo statement will print a blank line

11

Variables (Contd.)
Type Conversion Initially the values assigned to the variable in Unix will be treated as a string value. We cannot perform any Arithmetic & Relational operators operation. 1. Converting String Value to Integer Value To convert the string value to an integer value in Unix we use the command expr preceded by the variables and enclosed within Accent Graves (or) Reverse Quotes denoted by ` . Syntax : `expr $<variable> <operator> $<variable>` Eg.: 10/17/08 `expr $a + $b`
Confidential

© Tech Mahindra 2006

10/17/08

12

Variables (Contd.)
2. Converting String Value to Float Value To convert the string value to an float value in Unix we use the command bc (Byte Calculation or Bench Calculator) followed by the variables and pipe sign. The bc command has to be used along with the echo command. Syntax : `echo $<variable> <operator> $<variable>|bc` Eg.: ` echo $a / $b|bc -l`

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

13

Operators
Arithmetic Operators +,: For Addition & Subtraction \* , “ * “ : For Multiplication / (Front Slash) : For Division % : For Modular Division (To Find the Remainder) Relation Operators -lt : Less Than -gt -le : Less than or equal to Equal to -eq : Equal to -ne : Greater Than -ge : Greater Than or : Not Equal to -o : OR Gate

Logical Operators -a : AND Gate Operation Operation ! : NOT Gate Operation 10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

14

Test Conditions
There are 3 different types of test conditions are available, they are a. Numerical Test b. String Test c. File Test Test conditions will be used only in conditional and looping statements. Keyword : [ … ] or test Syntax : a. [ <Condition> ] b. test <condition>
Confidential

10/17/08

© Tech Mahindra 2006

10/17/08

15

Test Conditions (Contd.)
a. Numerical Test This can be used with relational and logical operators. Used to check for numeric values. b. String Test Used to check for string values Condition Meaning

str1 = str2 str1 != str2 -n str -z str str
10/17/08

True if the strings are same True if the strings are different True if the length of the string is > 0 if the length of the string = True 0 True if the string is not a null string, Same as –n str.
Confidential

© Tech Mahindra 2006

10/17/08

16

Test Conditions

(Contd.)

c. File Test This is used to check for the status of the file.

Option -s file -f file -d file -r file -w file -x file

Meaning True if the file exists and has a size > 0 True if the given name is a file True if the given name is a directory True if the given name has a read permission True if the given name has a write permission True if the given name has a execute permission

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

17

Statements
An Instruction with proper syntax and semantics is known as statement. Types of Statement There are 2 types of statement they are NonExecutable and Executable Statements. a. Non – Executable Statement It is other wise known as Remarkable or Comment statement. Which will never get executed by the command interpreter, its only for the user reference. The Line preceded by # symbol will be considered as Non – Executable statement in Unix Eg.: 10/17/08 i. # Program For Factorial of the given
Confidential

© Tech Mahindra 2006

10/17/08

18

Statements

(Contd.)

b. Executable Statements Which are interpreted and executed by the command interpreter. There are various types of executable statements they are
    Input and Output Conditional Multiple Conditional Looping

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

19

Statements

(Contd.)

a. Input and Output Statement It is used to take the input and to display the output on the standard input and output device. (i.e., Keyboard & Monitor). Keyword : read (used for Input Statement) echo (used for Output Statement) Syntax : a. read <variable name> b. echo $<variable name> c. echo <comments> d. echo <comments> $<variable name> e. echo ${<variable name>-<value>}

(If we give

10/17/08

+ sign it will assign a null value to the variable name specified).
Confidential

© Tech Mahindra 2006

10/17/08

20

Statements

(Contd.)

Eg.: a. read nam Used to take the input for the variable nam during the runtime b. read nam nam1 nam2 The input values for the above variables has to be separated by a space. (i.e., mag bms balaji , now nam = mag , nam1 = bms, nam2 = balaji). c. echo $a Prints the value of the variable named a. To print the values of the variables it has to be preceded by $ sign. d. echo Value of A = $a (or) echo “ Value of A = 10/17/08 “$a 10/17/08 Confidential © Tech Mahindra 2006 Prints the value of a along with the comment line

21

Statements

(Contd.)

Eg.: e. echo ${fav-Jyoti Nivas} The above command assigns the value Jyoti Nivas to the variable fav. f. echo ${fav1+christ} Since we have a + sign within the flower braces. The command interpreter will assign a value null to the variable named fav1. g. echo –n “ Enter Your Choice “ Places the cursor position on the same line after printing the statement “ Enter Your Choice “ h. echo `date` Used to display the system date on the standard output device i.e., monitor. (To execute any built in commands using echo keyword it has to be enclosed within ascent graves).
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

22

Statements

(Contd.)

Eg.: i. echo * Used to list all the files in the current working directory. j. echo \* Used to display the * on the standard output device monitor. k. echo a + b Used to display the word a + b on the standard output device. l. echo `a + b` (Placed in Ascent Graves) Display an error message.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

23

Statements

(Contd.)

a. Conditional Statement Used to check the condition before executing the statements. It can have minimum one branch and maximum two branches for a single condition. There are different types of conditional statements, they are i. Simple if ii. if … else iii. Nested if … else iv. elif ladder

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

24

Statements
i.

(Contd.)

Simple if : Provides a single branch for a condition. Keyword : if , then , fi Syntax :

a. if ]

[ condition

a. if test <condition> then st1 fi

then st1 fi
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

25

Statements
Eg.: Without Using test K/w. echo “ Enter The Value of A &B“ read a read b if [ $a –gt $b ] then echo “ A is greater “ fi
10/17/08
Confidential

(Contd.)

Eg.: Using test K/w. echo “ Enter The Value of A &B“ read a read b if test $a –gt $b then echo “ A is greater “ fi

© Tech Mahindra 2006

10/17/08

26

Statements
ii. if … else

(Contd.)

Provides two branches for a condition. Keyword : if , then , else , fi Syntax : a. if ] [ condition a. if test <condition> then st1 else st2
10/17/08

then

st1 else st2 fi

fi

Confidential

© Tech Mahindra 2006

10/17/08

27

Statements
Eg.: Without Using test K/w. echo “ Enter The Value of A &B“ read a read b if [ $a –gt $b ] then echo “ A is greater “ else echo “ B is greater “
10/17/08

(Contd.)

Eg.: Using test K/w. echo “ Enter The Value of A &B“ read a read b if test $a –gt $b then echo “ A is greater “ else echo “ B is greater “ fi
© Tech Mahindra 2006 10/17/08

fi

Confidential

28

Statements
i. Nested if … else Syntax :
a. if [ condition1 ] then if [ condition1 ] st1 else st2 fi else
10/17/08

(Contd.)

b. if

test condition1

then if test condition1 then st1 else st2 fi else st2 st2
Confidential

fi
© Tech Mahindra 2006 10/17/08

29

Statements
i. elif ladder Syntax : a. if [ condition1 ] then st1 elif [ condition2 ] then st2 elif [ condition3]
10/17/08

(Contd.)

b. if

test condition1 st1

then elif test condition2 then st2 elif test condition3 then st3 fi
© Tech Mahindra 2006 10/17/08

then st3Confidential

30

Statements

(Contd.)

What is the difference between nested if … else and elif ladder statements in Unix ?

Nested if In the case of Nested if statement we need to end it with fi for each and every if statement.

elif ladder In the case of elif ladder we need to have only one fi statement irrespective of the number of if statements we have used.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

31

Statements

(Contd.)

c. Multiple Conditional Statements It is similar to that of switch statements in C. We will be using this provided when we have more than 2 branches for a single condition. Keyword : case , esac Syntax : case $<variable> in <value1>) st1 ;; <value2>) st2 ;; Similar to *) st3 default block ;; in C esac Note : Each and every case has to be ended with ;; , It is similar to that of break statements in C language.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

32

Statements
Eg.: echo “ Main Menu “

(Contd.)

echo “ 1. Addition “ echo “ 2. Subtraction “ echo “ 3. Multiplication” I/p : 1 echo “ 4. Exit “ echo “ Enter Your Choice “ read ch case $ch in 1) c=`expr 10 + 20` ;; 2) c = `expr 10 – 20`;; 3) c = `expr 10 \* 20`;; *) echo “ Enter Proper Value “;; O/p: Value of C = esac 30 echo “ Value of C = “ $c
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

33

Statements

(Contd.)

Eg.: 2. # Program to check whether the entered input is a lower case , upper case alphabet or a digit. echo “ Enter The Value “ read ch case $ch in [a-z]) echo “ Entered Lower Case Letters “ ;; [A-Z]) echo “ Entered Upper Case Letters “;; [0-9]) echo “ Entered Numbers”;; *) echo “ Special Characters “;; esac

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

34

Statements

(Contd.)

Eg.: 3. # Program to check whether the entered number from 1 to 10 is even or odd echo “ Enter The Numbers between 1 to 10 “ read ch case $ch in 1|3|5|7|9) echo “ Odd Numbers “ ;; 2|4|6|8|10) echo “ Even Numbers “;; *) echo “ Invalid Input “;; esac

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

35

Statements

(Contd.)

d. Looping Statement Used to perform the same set of operations for more than one time till the specified condition is true or false. There are 3 types of looping statements, they are i. while ii. until iii. for The first two are used for numerical operations. The final one is used for string manipulation, used to generate the tokens separately.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

36

Statements
i.

(Contd.)

while : This will execute the set of statements repeatedly until the specified condition is true Syntax : a. while [ <condition> ] while test b. <condition> do do statements statements

done done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

37

Statements

(Contd.)

Eg. : # Program to find the factorial of the given number echo “ Enter The Given Number “ read n i=1 f=1 while [ $i –le $n ] do f=`expr $f \* $i` # f=`expr $f “*” $i` i=`expr $i + 1 ` done echo “Given Number = “ $n echo “ Factorial of the Given Number = “ $f

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

38

Statements

(Contd.)

ii. until : This will execute the set of statements repeatedly until the specified condition is false Syntax : a. until [ <condition> ] b. until test <condition> do do statements statements

done done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

39

Statements

(Contd.)

Eg. : # Program to find the factorial of the given number using until loop echo “ Enter The Given Number “ read n i=1 f=1 until [ $i –gt $n ] do f=`expr $f \* $i` # f=`expr $f “*” $i` i=`expr $i + 1 ` done echo “Given Number = “ $n echo “ Factorial of the Given Number = “ $f
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

40

Statements
iii. for

(Contd.)

It allows us to specify a list of values which the control variable in the loop can take. The loop is then executed for each value mentioned in the list. Syntax : for <variable> in <list of values/string> do st1;

done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

41

Statements

(Contd.)

Eg. : # Program to print all the sub-directories present in the current directory echo “ Present Working Directory “ pwd echo “ The sub-directories under current directory are : “ for i in * do if [ -d $i ] then echo -n $i fi done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

42

Statements

(Contd.)

break and continue In shell programming we can use break and continue keywords only in the looping statements (i.e., while , until & for ). a. break It is used to come out of the loop. Syntax : a. break b. break n The first syntax is used to come out of the current loop. But the later one is used to come out of the specified number of loop.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

43

Statements

(Contd.)

Eg.: echo “ Demo For Break St. “ Note : i=1 j=1 while [ $i –le 10 ] In the above example do for every value of i the inner while [ $j –le 10] loop i.e., j will get executed do only till its value is equal to if [ $j –eq 5 ] 5. Once when j value then becomes 5 it comes out of break the j loop because of a break fi statement, inside it. j=`expr $j + 1` done i=`expr $i + 1` done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

44

Statements

(Contd.)

Eg.: echo “ Demo For Break St. Note : “ i=1 j=1 In the above example while [ $i –le 10 ] the outer loop will gets do executed only once and the while [ $j –le 10] inner loop will get executed do for 5 times, once the j value if [ $j –eq 5 ] becomes 5 it comes out of then both the loops because of break 2 the statement break 2 . It fi represents to the command j=`expr $j + 1` interpreter to come out of done the second loop. i=`expr $i + 1` done
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

45

Statements

(Contd.)

b. continue It is used to move the control to the beginning of the loop. (It is similar to Entry Point) Syntax : a. continue b. continue n The first syntax is used to move the control to the beginning of the current loop where continue has been represented. But the later one is used to move the control to the beginning of the nth loop from the specified point.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

46

Statements

(Contd.)

Eg.: echo “ Demo For Continue St. “ i=1 j=1 while [ $i –le 10 ] do Note : while [ $j –le 10] do In the above if [ $j –eq 5 ] example for every value of then i the inner loop i.e., j will continue get executed for infinite fi number of times because j=`expr $j + 1` once when it becomes 5 it done moves the control back to i=`expr $i + 1` the beginning of j loop and done the j value remains the same.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

47

Statements
Eg.: echo “ Demo For Continue i=1 j=1 while [ $i –le 10 ] do while [ $j –le 10] do if [ $j –eq 5 ] then continue 2 fi j=`expr $j + 1` done i=`expr $i + 1` done
10/17/08
Confidential

(Contd.)

St. “

Note : In the above example for always i remains 1 because the inner loop i.e., j will get executed for infinite number of times because once when it becomes 5 it moves the control back to the beginning of i loop because of the statement continue 2and the i value
© Tech Mahindra 2006 10/17/08

48

Try Out ?
 $echo * The above command will print all the files and directories available on the present working directory.  $echo \* The above command will print the * on the standard output device. Since it has been preceded by \.  $echo ‘***’ The above command will display *** since it has been enclosed within quotes.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

49

Try Out ?
 $echo date The above command will print the word date on the standard output device. The same can be given as echo ‘date’ (or) echo “date”  $echo `date` (Placed Within Ascent Graves ) The above command will execute the date command and prints the result of the date command.  $echo `*` (or) echo `?` (Placed Within Ascent Graves ) The above command will generate error message stating that bash – permission denied 10/17/08 because the * and ? are not commands to be placed
Confidential

© Tech Mahindra 2006

10/17/08

50

Hands On…

What will be the output of the above programs ? 2. 1. echo “ 1. ESG “ echo “ 1. ESG “ echo “ 2. TIM” echo “ 2. TIM” echo “ 3. HR” echo “ 3. HR” echo “ Enter Your Choice “ echo “ Enter Your Choice “ read ch read ch case $ch in case $ch in *) 1) echo “ Welcome to Enr” echo “ Invalid Input”;; 2) echo “ Welcome to ILI “ 1) echo “ Welcome to Enr”;; 2) 3) echo “ Welcome to HRD” echo “ Welcome to ILI “;; 3) echo “ Welcome to HRD”;; *) echo “ Invalid Input” esac esac O/p: O/p: For all the inputs it gives Syntax Error near Invalid inputs. unexpected 10/17/08 token (
Confidential

© Tech Mahindra 2006

10/17/08

51

Formatting the Output
tput command : The terminal capabilities are stored in a file termcap inside /etc directory. Different ways of using tput command is given below tput clear : Clears the screen tput cup r c : Moves the cursor to row r and column c Eg.: Note :
10/17/08

tput cup 10 5 Moves the cursor to row 10 column 5 Here cup denotes the cursor position.
Confidential

© Tech Mahindra 2006

10/17/08

52

Positional Parameters
It is otherwise called as Command Line arguments. It will be represented by $1 to $9 (Whenever the variable name preceded by $ will be represented as positional parameters). We can assign the values for the positional parameters from $1 to $9 either during the compile time or Parameter Meaning during the runtime (i.e., Using Command line arguments).Process Identification of the current $$ The shellshell reserves some variable names for its $? Exit status of the last executed use, they are listed command $! Process Identification of last background below. process Shell Settings $Current $#
10/17/08
Confidential

Total Number of Positional parameters
© Tech Mahindra 2006 10/17/08

53

Positional Parameters

(Contd.)

Paramete Meaning r $0 Name of the command being used $* $@ List of all shell arguments. Can’t yield each argument separately. Similar to $* , but yields each argument separately when enclosed in double quotes.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

54

Positional Parameters

(Contd.)

Setting Values of Positional Parameters a. During the Runtime # Program to check whether given two strings are equal or not using command line arguments. if [ $1 = $2 ] then echo Strings are Equal $1 $2 else echo Strings are not Equal $1 $2 fi echo Total Number of Positional Parameters $# echo List of Shell Arguments $* 10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

55

Positional Parameters

(Contd.)

I/P For the above Program Syntax : sh <name of the prog.> <value1 value2 . . .> Eg.: $ sh pos1.sh bms bms : In the above input $0 will have the name of the file i.e, pos1.sh as its value, $1 will have the value as bms & $2 has the value as bms. O/P : Strings are Equal Total Number of Positional parameters 2 List of Shell Arguments bms bms
Confidential

Note

10/17/08

© Tech Mahindra 2006

10/17/08

56

Positional Parameters

(Contd.)

b. During the Compile time # Program to assign the value for the positional parameter during the compile time. set Doubt Your Doubts Never Your Beliefs Note echo Value of \$1 $1: The slash has been preceded echo Value of \$2 $2 before the $ sign in echo statement to echo Value of \$3 $3 print that $1 and so on as it is , otherwise echo Value of \$4 $4 it will print the value of that positional Parameter. echo Value of \$5 $5 echo Value of \$6 $6$1 will have the value Doubt and $2 will have the value Your and so on.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

57

Positional Parameters

(Contd.)

b. During the Compile time # Program to assign the value for the positional parameter during the compile time. set Happiness what U Give Makes U More Happy Than The Note : Happiness What U Receive. are assigning a value for While we echo Value ofthe positional parameter, it can assign \$9 $9 echo Value of values only to 9 positional parameters by \$10 $10 default. echo Value of \$12 $12 In the above program while we are echo Value ofaccessing the positional parameter $10 will \$20 $20 have the first value followed by 0 i.e, Happiness0 , $12 = Happiness2 $20 = what0
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

58

Positional Parameters

(Contd.)

How to Resolve the Ambiguity ? To print the entire value of the string given in the set command using positional parameter we have to use shift command. Using shift command we can move the control to the specified number of tokens and regenerate the positional parameters from that point. Syntax : shift <number> Eg.: shift 3 Note : The number represented in shift command should be a positive number (i.e., >= 0 and <= the total no. of words given in the set command) . If we are not giving any values by default it takes as 10/17/08 1.
Confidential

© Tech Mahindra 2006

10/17/08

59

Positional Parameters

(Contd.)

b. During the Compile time # Program to assign the value for the positional parameter during the compile time using shift. set Happiness what U Give Makes U More Happy Than The Happiness What U Receive. Note echo Before Shifting : echo Value of \$1 $1 The shift command is used to move echo Value of \$2 $2 9 tokens towards the right and reinitialize the positional parameters. After echo Value of \$3 $3 shifting echo Value of \$9 $9 $1 = The $2 = Happiness shift 9 $3 echo After Shifting = What echo Value of \$1 $1 echo Value of \$2 $2 10/17/08 echo Value of \$3 $3
Confidential

© Tech Mahindra 2006

10/17/08

60

Positional Parameters

(Contd.)

# Demo For $* and $@ parameters cat “$*” # interpreted as cat “pos1.sh pos2.sh cat “$@” pos3.sh” # interpreted as cat “pos1.sh” I/P : “pos2.sh” “pos3.sh” sh pos10.sh pos1.sh pos2.sh pos3.sh O/P: While executing the first cat command will produce an error stating that there is no file named “pos1.sh pos2.sh pos3.sh”. But the later one will display the content of the files pos1.sh , pos2.sh & pos3.sh respectively. Note : If the above commands has been issued without quotes both will give the same results

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

61

Exporting Variables
A variable can be made available to its sub shell through export command Syntax export <Variable1 . . .> A variable can be exported either before or after it is defined. Eg.: i. export a ii. a=100 a=100 export a

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

62

Exporting Variables

(Contd)

Note : 1. We can export the variables only from the parent to the child program. But the reverse is not true. 2. The Changes Made in the child program will never get reflected to the parent program. 3. The Program which we are calling inside the parent program should have an execute permission. Otherwise it give an error message stating that permission denied. 4. We can export the Variables from one shell to another shell.
10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

63

Exporting Variables

(Contd)

#Sub program using the export # Program For Exporting the # to # Variable from one program Variable echo “ Value of A = “ $a # Another program a=`expr $a + 10` a=10 echo “ Value of A = “ $a export a echo “ Value of A in expo.sh “ $a #Calling expo1.sh programNote : The Values Changed in the expo1.sh Child Prog. will never get reflected echo “ After Calling expo1.sh “ $a to the Main Prog.

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

64

Signals & Trap Command
© © ©

Signals in general tend to terminate a process. trap command allows to handle the signal Important signals Signal Key Combination Ctrl d Ctrl c kill Code Value 1 2 15

Hang up Interrupt Terminate

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

65

Signals
Syntax list> Eg. : :

(Contd.)

trap “<commands>” <signal-

is hit

trap “ls –al” 2 Lists the current directory contents when ctrl-c

Note : 1. trap command, once it has been read in, lies in wait for those signals mentioned in the signal list. When such a signal shows up, the command is performed instead of 10/17/08 the default behavior
10/17/08 Confidential 2. trap command will © Tech Mahindra 2006 trap the signals only during the

66

Summary
        Uses of Shell Scripts Variables Type Conversions Statements Positional Parameters Export Signals Trap Commands

10/17/08
Confidential

© Tech Mahindra 2006

10/17/08

67

Sign up to vote on this title
UsefulNot useful