UNIX Day 4
Intermediate Level
Recap Day 3
Communication Commands
Security features in UNIX
The find Utility vi editor & its important features Introduction to Shell Programming Types of shell
Shell variables
2
Session Plan Day 4
Positional Parameters Special Parameters Selectional Constructs Iterational Constructs Usage of cc
Positional Parameters
A command in UNIX is interpreted by the shell When a command is entered and <Enter> key is pressed, the shell puts each word on the command line into special variables as follows
The command name is put into the variable called $0 The first argument is put into the variable called $1 The second argument is put into the variable called $2 and so on.
The UNIX shell creates variable up to $9 These variables are also called positional parameters of the command line.
set file1 file2 file3
$0 bash, $1- file1, $2 file2, $3 file3
4
Special Parameters
Besides the variables $0 to $9, the shell also assigns values
to the following variables, called Special Parameters:
$$ PID number of current shell $# contains the number of Command Line Arguments $0 Command Name $* contains the entire string of arguments
$? Exit Status of the last executed command
$! PID number of the last background command $@ Same as $* but fetches each argument separately when enclosed in double quotes
5
Shift Command
Used to shift the positions of positional parameters to left by some value Syntax:
shift [n]
test command (1/5)
Used extensively for evaluating shell script conditions It evaluates the condition on its right and returns a true or false exit status The return value is used by the construct for further execution In place of writing test explicitly, the user could also use [ ]
test command (2/5)
Operators used with test for evaluating numeral data are:
Operator -eq -lt Description Equals to Less than Syntax $ test value1 eq value2 $ test value1 lt value2
-gt
-le -ge -ne
Greater than
Less than or equals to Greater than or equals to Not equals to
8
$ test value1 gt value2
$ test value1 le value2 $ test value1 ge value2 $ test value1 ne value2
test command (3/5)
Operators used with test for evaluating string data are:
Operator = Description Equals to Syntax $ test str1 = str2
!=
-n -z
Not equals to
Result true if str1 is not a null string Result true is str1 is a null string
$ test str1 != str2
$ test n str1 $ test z str1
test command (4/5)
Operators used with test for evaluating file data are:
Operator Description Syntax
-f
-d -s -r
True if file1 exists and a regular file
True if file1 exists and is directory True if file1 exists and has size greater than 0 True if file1 exists and is readable
$ test f file1
$ test d file1 $ test s file1 $ test r file1
-w
-x
True if file1 exists and is writable
$ test w file1
True if file1 exists and is executable $ test x file1
10
test command (5/5)
Logical Operators used with test are:
Operator Description Syntax
!
-a -o
Negates the expression $ test ! val1 eq val2
Binary and operator Binary or operator $ test val1 le val2 a val3 ge val4 $ test val1 le val2 o val3 ge val4
11
Shell Quotes-Meanings
(single forward quotes)
Content written in is interpreted literally.
` ` (single backward quotes Grave accent)
Content written in `` is interpreted by shell as command and result is returned
" " (double quotes)
Content written in "" is interpreted literally except $.
12
expr command
Used for evaluating shell expressions. Used only for arithmetic operations.
Example 1: Example 2: $ expr 7 + 3 $ x=10 $ y=20 $ expr $x + $y # output: 10
# output: 30
To use returned value further, back quotes need to be used.
Example 3: $ x=10 $ y=20 $ z=`expr $x + $y`
# output: z=30
Arithmetic operator has to be preceded and followed by a space
13
Conditional Execution
Use of && and || operators (short circuit operators) Used to combine two commands && operator:
The second command is executed only when first is successful Example: $ cat file && echo File exists
|| operator:
The second command is executed only when the first is unsuccessful Example: $ cat file || echo File doesnt exist
14
Program Constructs
Selectional Constructs
if case
Iterational Constrcuts
for while until
15
Simple if Statement (1 of 2)
At the place of condition, a control command is given
If the command exit status is 0, a set of commands are executed If the command exit status is 1, set of commands are not executed and the program control goes to the next command that immediately follows the if block If the command exit status is 2, the commands are not executed and error message will be displayed on screen
16
Simple if Statement (2 of 2)
Syntax:
if <control command> then
<commands>
fi <next command>
17
Example of simple if statement (1/2)
$ cat SimpleIf.sh read x if test $x lt 15 then echo x is less than 15 fi echo Outside if <end of file> Case 2: x is 20 Outside if Case 1: x is 10 x is less than 15 Outside if
18
Example of simple if statement (2/2)
Sample values for data: Case 1: x=10 x is less than 15 Outside if Case 2: x=0 x is less than 15 Outside if Case 3: x= Outside if
$ cat SimpleIf1.sh x=<data> if test $x then echo x is less than 15 fi echo Outside if <end of file>
19
else Statement (1 of 2)
In simple if statement, when the exit status is 0, a set of commands are executed. But when it is 1, there is no alternate set of commands The statement else provides the same
20
else Statement (2 of 2)
Syntax:
if <control command>
then <commands>
else
<commands> fi <next command>
21
Example of simple if statement (2/2)
$ cat IfElse.sh
read x
if test $x lt 15 then echo x is less than 15 else echo x is not less than 15 fi echo Outside if
Case 1: Input value for x is 10: x is less than 15
Outside if
Case 2: Input value for x is 20:
x is not less than 15
Outside if
<end of file>
22
else if Statement (1 of 2)
The else if statement is used to check the exit status for a sequence of control commands
When one control commands exit status is 1, it checks for the next control command and so on When all the control commands exit status is 1 the else block is executed
Once the control commands exit status is 0, all the other else if statements are skipped
23
else if Statement (2 of 2)
Syntax:
if <control command1>
then <commands>
elif <control command2>
then <commands>
else
<commands> fi <next command>
24
Example of if construct
$ cat if.sh read x if [ $x -eq 10 ] then echo "Equal to 10" elif [ $x -gt 10 ] then echo "Greater than 10" else echo "Less than 10" fi echo Outside if <End of file>
25
Case 1: x is 10 Equal to 10 Outside if Case 2: x is 15 Greater than 10 Outside if
Case 3: x is 5 Less than 10 Outside if
case statement (1/2)
The case statement is a selectional construct that selects a choice from the set of available choices
It is very similar to if statement but it can not be used in the manner, if is used
26
case statement (2/2)
Syntax
case value in choice1) commands ;; choice2) commands ;; .... ....
The symbols ;; are used as block terminators.
esac
Note:
Value is compared with choices as string comparison Default choice is given as *) combination of characters
27
What is the output? (1/6)
$ cat case1.sh Case 1: x is 10 Ten Case 2: x is 15 Fifteen Case 3: x is 5 Five Case 4: x is 12
read x
case $x in 5) echo "Five";;
10) echo "Ten";;
15) echo "Fifteen";; *) echo "Wrong value";; esac <End of file>
Wrong value
Case 5: x is 3 Wrong value
28
What is the output? (2/6)
$ cat case2.sh
read x
case $x in 5) echo "Five";;
Case 1: x is 10
Ten Case 2: x is 15 Wrong value Case 3: x is 5 Five
10) echo "Ten";;
*) echo "Wrong value";; 15) echo "Fifteen";; esac <End of file>
Case 4: x is 12
Wrong value
29
What is the output? (3/6)
$ cat case2.sh
read x
case $x in 5) echo "Five";;
Case 1: x is 10
Ten Case 2: x is 15 Wrong value Case 3: x is 5 Five
10) echo "Ten";;
*) echo "Wrong value";; 5) echo Duplicate value";; esac <End of file>
Case 4: x is 12
Wrong value
30
What is the output? (4/6)
$ cat case2.sh
read x
case `test $x -lt 20` in 0) echo True";; Case 1: x is 10 Wrong value
1) echo False";;
2) echo Error";; *) echo "Wrong value" esac <End of file> Case 2: x is 25 Wrong value
31
What is the output? (5/6)
$ cat case1.sh
case `expr 10 + 5` in 5) echo "Five";; 10) echo "Ten";; 15) echo "Fifteen";; *) echo "Wrong value Fifteen
esac
<End of file>
32
What is the output? (6/6)
$ cat case1.sh
case `expr 10 + 5` in *) echo " Wrong value ";; 10) echo "Ten";; 15) echo "Fifteen";; 20) echo Twenty Wrong value
esac
<End of file>
33
for statement (1/2)
In for loop, we are giving a set of values to control the iteration of the loop
Set of commands written in for loop will be executed once for each value.
34
for statement (2/2)
Syntax:
for variable-name in value1 value2 .... valueN
do
<commands> done
35
What is the output?
$ cat for1.sh for val in A B C D E do echo $val done
Output: A B C D
<End of file>
36
What is the output?
$ cat for2.sh
count=0 for val in Good Bye Hello World do count=`expr $count + 1` done
Output:
No of iterations: 3
echo No of iterations: $count
<End of file>
37
What is the output?
$ cat for3.sh for val in date who exit cal do echo $val done
Output: date
who
exit cal
<End of file>
38
What is the output?
$ cat for4.sh
a= for val in U N I X do a=$a$val done
Output:
UNIX
echo $a
<End of file>
39
while statement
In while loop, we are giving one control command to control the iteration of the loop A set of commands will be executed as long as the control commands exit status is 0 When the control commands exit status becomes 1, the while loop is quitted
40
while statement
Syntax:
while control command do
<commands>
done
41
What is the output?
$ cat while1.sh
x=1 while [ $x -le 5 ] do echo ne $x\t x=`expr $x + 1` 1 2 3 4 5
done
<End of file>
42
until statement
A until loop is used to repeat set of commands as long as the control commands exit status is 1 When the control commands exit status becomes 0, the until loop is quitted
43
until statement
Syntax:
until control command do
<commands>
done
44
Example until statement
$ cat until1.sh Sample Execution:
echo Type command(end to exit):
read cmd until [ $cmd = end ] do `echo $cmd` read cmd done echo End of the script
Type command(end to exit):
date +%T 23:07:46
echo hello
hello type echo echo is a shell builtin end End of the script
<End of file>
45
What is the output?
$ cat until2.sh mul=1 num=1 until [ $num -eq 0 ] Input Values: Case 1: 2 4 6 0 num: 0 mul: 48 mul=`expr $mul \* $num` echo n Enter the num(0 for exit): Case 2: -2 4 -6 0
do
read num
done echo -e num: $num \n mul: $mul <End of file>
46
num: 0
mul: 48
Useful Shell Scripting commands
break
To come out of a loop. Syntax:
break [n]
Usage:
break #will come out of the current loop break n # will come of the specified number(n) of loops
47
What is the output?
$ cat break.sh read num var=2 while [ $var -lt $num ] do rem=`expr $num % $var` if [ $rem -eq 0 ] then echo Not Prime Number break fi var=`expr $var + 1` done if [ $var -ge $num ] then echo Prime Number fi
48
Input Values:
Case 1: 9 Not Prime Number
Case 2: 7 Prime Number
Useful Shell Scripting commands
continue
To jump to the start of loop. Syntax:
continue [n]
Usage:
continue #will go to the beginning of the current loop continue n # will go to the beginning of the nth loop
exit
To prematurely terminate a program.
49
What is the output?
$ cat for3.sh for val in date who exit cal do if [ $val = exit ] then continue fi Output: date Who cal
echo $val
done <End of file>
50
Trap command
trap
To alter the effects of certain events that generate signals, which generally tends to halt a process. Syntax: trap command signal list
51
Self Study: Shell Programming Constructs Demo
52
Program Development Tools
cc C language Compiler
Phases in creation of executable program.
Source Code
.c
Object Code
.o
Executable Code
a.out
54
cc - Important options
-c
Compiles the given C file. No linking would be done. Syntax
cc c <filename.c>
-o
Specify a given name for the final executable file. Syntax
cc o <file1> <filename.o>
55
Can you answer these questions?
What is the output of the following command(s):
1. $ var=10 $ [ $var eq 10 ] $ echo $? 2. $ expr `echo 10` + 15 3. $ echo `echo 10` + 15 4. $ set date who echo Hello cal $ echo `$3` 5. $ echo UNIX && echo RDBMS 6. $ echo UNIX || echo RDBMS
Amit has a C file named prog1.c. He wants the executable to be named as amit. How will he do this?
56
Summary
Positional Parameters Special Parameters Selectional Constructs Iterational Constructs Usage of cc
57
Module Expectations: Are you meeting it?
The candidate should be conversant with the following
Unix architecture Usage of various types of Unix commands Unix utilities Security features in Unix vi editor Shell programming
58
Learning approach
The following are strongly suggested for a better learning and understanding of this course:
Noting down the key concepts in the class, explained by the educator Analyze all the examples / code snippets provided Study and understand the self study topics Completion and submission of all the assignments, on time Completion of the self review questions in the lab guide Study and understand all the artifacts including the reference materials / e-learning / supplementary materials specified Completion of the project (if applicable for this course) on time inclusive of individual and group activities Taking part in the self assessment activities Participation in the doubt clearing sessions
59
Thank You
60