Unix 04
Unix 04
Intermediate Level
Recap Day 3
Communication Commands
Shell variables
2
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
Special Parameters
Besides the variables $0 to $9, the shell also assigns values
Shift Command
Used to shift the positions of positional parameters to left by some value Syntax:
shift [n]
-gt
-le -ge -ne
Greater than
Less than or equals to Greater than or equals to Not equals to
8
!=
-n -z
Not equals to
Result true if str1 is not a null string Result true is str1 is a null string
-f
-d -s -r
$ test f file1
$ test d file1 $ test s file1 $ test r file1
-w
-x
$ test w file1
!
-a -o
11
Shell Quotes-Meanings
(single forward quotes)
Content written in is interpreted literally.
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
# output: z=30
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
$ 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
$ 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
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
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>
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
26
esac
Note:
Value is compared with choices as string comparison Default choice is given as *) combination of characters
27
read x
case $x in 5) echo "Five";;
Wrong value
Case 5: x is 3 Wrong value
28
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
Case 4: x is 12
Wrong value
29
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
Case 4: x is 12
Wrong value
30
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
esac
<End of file>
32
esac
<End of file>
33
34
do
<commands> done
35
Output: A B C D
<End of file>
36
Output:
No of iterations: 3
37
$ cat for3.sh for val in date who exit cal do echo $val done
Output: date
who
exit cal
<End of file>
38
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
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
echo hello
hello type echo echo is a shell builtin end End of the script
<End of file>
45
do
read num
done echo -e num: $num \n mul: $mul <End of file>
46
num: 0
mul: 48
Usage:
break #will come out of the current loop break n # will come of the specified number(n) of loops
47
Input Values:
Case 1: 9 Not Prime Number
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
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
52
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
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
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