Date post: | 14-Apr-2018 |
Category: |
Documents |
Upload: | vivek-tamrakar |
View: | 220 times |
Download: | 0 times |
of 32
7/28/2019 210_1.pdfppt
1/32
Linux Shell Programming
7/28/2019 210_1.pdfppt
2/32
Overview
Command line arguments Arithmetic in shell scripts Read and echo Commands in shell
scriptsTaking decision:
If then-fi
If then else-fi The test command(file test,string tests) Nested if-elsesThe Case control structure
7/28/2019 210_1.pdfppt
3/32
Overview
The loop control structure
The while ,until and for loop structure
The break and continue statementsDirectory stacks manipulation
Job control, history and processes
Built in functions
7/28/2019 210_1.pdfppt
4/32
Command line arguments
cat sample.sh
echo Program:$0
The number of arguments is $#The arguments are $*
grep$1 $2
echo \n Job Over$sample.sh hello test.txt
7/28/2019 210_1.pdfppt
5/32
Command line arguments
Special variables are assigned toarguments in shell procedure
First argument $1Second argument $2
7/28/2019 210_1.pdfppt
6/32
Arithmetic in shell scripts
Use to perform arithmetic operations. Syntax:
expr op1 math-operator op2
Examples:$ expr 1 + 3$ expr 2 - 1$ expr 10 / 2$ expr 20 % 3
7/28/2019 210_1.pdfppt
7/32
Arithmetic in shell scripts
$ expr 10 \* 3
$ echo `expr 6 + 3`
7/28/2019 210_1.pdfppt
8/32
Read and echoCommands
cat search.sh#search for pattern in a file
echo \n Enter the string to be searched:\c
read pnameecho \n Enter the file name:\c
read fname
echo \n Searching for $pname from file $fname \n
grep $pname $fnameecho \n Search completed\n
7/28/2019 210_1.pdfppt
9/32
Taking decision
If then-fiif condition is true
then
execute commandsfi
7/28/2019 210_1.pdfppt
10/32
If then else-fi
if condition is true
then
execute commands
elseexecute commands
fi
7/28/2019 210_1.pdfppt
11/32
If then else-fi
if grep test sample.txt
then
echo String foundelse
echo String not found
fi
7/28/2019 210_1.pdfppt
12/32
test command ( numeric)
Operator
Meaning
-eq Equal to-ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
7/28/2019 210_1.pdfppt
13/32
test command ( numeric)
test : uses operator to evaluate the condition, returns true orfalse
exit : used by if for taking decisions
$x=5; y=7; z=7.2
$test $x eq $y ; echo $?
1
$test $x lt $y ; echo $?
0
$test $z gt $y ; echo $?
1
$test $z eq $y ; echo $?
0
7/28/2019 210_1.pdfppt
14/32
test command
if test $# -ne 3
then
echo 3 arguments are required
exit 3
elseif grep $1 $2 >$3
then
echo String found
elseecho String not found
fi
fi
7/28/2019 210_1.pdfppt
15/32
test command (file test)
Test Exit status
-e file True if file exists
-f file True if file exists and is a regular file-r file True if file exists and is readable
-w file True if file exists and is writeable
-x file True if file exists and is executable
-d file True if file exists and is directory
-s file True if file exists and has size > 0
7/28/2019 210_1.pdfppt
16/32
test command (file test)
ls l sample.txt
-rw-rw-rw- 1 student SENG 870 Jun 8 15:52sample.txt
$[ -f sample.txt]; echo $?0$ [-x sample.txt]; echo $?
1
$ [! w sample.txt ] || echo File is writableFile is writable
7/28/2019 210_1.pdfppt
17/32
test command (file test)
$cat testfile.sh
if [! e $1] ; then
echo File does not exists
elif [! r $1] ; then
echo File is not readable
elif [! w $1] ; then
echo File is not writable
else
echo File is both readable and writable
fi
7/28/2019 210_1.pdfppt
18/32
The Case control structure
case expression in
pattern1) execute command;;
pattern2) execute command;;pattern3) execute command;;
esac
7/28/2019 210_1.pdfppt
19/32
The Case control structure
$ cat menu.shecho MENU\n
1.List of files\n2.Processes\n 3.Todays Date\n4.Users ofsystem\n5.Quit \n Enter your choice :\c
read choicecase $choice in
1) ls l ;;
2) ps ;;
3) date ;;4) who ;;
5) exit
esac
7/28/2019 210_1.pdfppt
20/32
The loop control structure
while
until
for loopBreak
continue
7/28/2019 210_1.pdfppt
21/32
The while ,until and for loopstructure
while condition is true
do
execute commandsdone
7/28/2019 210_1.pdfppt
22/32
7/28/2019 210_1.pdfppt
23/32
7/28/2019 210_1.pdfppt
24/32
Job control processes
ps list the processes running on the system
kill send a signal to one or more processes (usually to "kill"
a process)
jobs listing processes
bg put a process in the background
fg put a process in the foreground
7/28/2019 210_1.pdfppt
25/32
history
To display the command list
7/28/2019 210_1.pdfppt
26/32
Built in functions
hash Command hash command maintains a hash table When a command is executed
it searches for a command in variable$PATH.
$ hash
hits command
1 /usr/bin/cat2 /usr/bin/ps
4 /usr/bin/ls
7/28/2019 210_1.pdfppt
27/32
Built in functions
$ hash -d cat
$ hash
hits command2 /usr/bin/ps
4 /usr/bin/ls
7/28/2019 210_1.pdfppt
28/32
Built in functions
set is a shell built-in command:
- used to set and modify the internal
variables
7/28/2019 210_1.pdfppt
29/32
Built in functions
$ cat set.sh
var="Welcome to CDAC"
set -- $var
echo "\$1=" $1echo "\$2=" $2
echo "\$3=" $3
$ ./set.sh
$1=Welcome$2=to
$3=CDAC
7/28/2019 210_1.pdfppt
30/32
unset
- set the shell variable to null
$ cat unset.sh
var="welcome to CDAC"echo $var
unset var
echo $var$ ./unset.sh
welcome to CDAC
7/28/2019 210_1.pdfppt
31/32
let
Performs arithmetic operations on shell variables
$ cat arith.shlet arg1=12
let arg2=11let add=$arg1+$arg2
let sub=$arg1-$arg2
let mul=$arg1*$arg2
let div=$arg1/$arg2
echo $add $sub $mul $div
$ ./arith.sh
23 1 132 1
7/28/2019 210_1.pdfppt
32/32
Reference
1.http://www.thegeekstuff.com/2010/08/bash-shell-builtin-commands/
2.UNIX Concepts And Application
Sumitabha Das3.http://linuxcommand.org/lts0080.php