Linux Shell Scripting Advanced
Linux Shell Scripting Advanced
Advanced Issues
Yusuf Altunel
1
Content
Commands
Command Line Arguments
Redirection of Input/Output
Pipes and Filters
Programming in Background
Conditionals
if
test
if else fi
Loops
for loop
do loop
2
The read Statement
Use to get input (data from user) from keyboard and
store (data) to variable.
Syntax:
read variable1, variable2,...variableN
Example: Write a shell script to
first ask user, name
then waits to enter name from the user via keyboard.
Then user enters name from keyboard (after giving name you
3
Example (read Statement)
$ 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!
4
Wild Cards
Wild card
Meaning Examples
/Shorthand
$ date;who
Will print today's date followed by users who are currently
login.
6
Command Line Arguments
1. Telling the command/utility
which option to use.
2. Informing the utility/command
which file or group of files to process
Let's take rm command,
is used to remove file,
which of the file?
how to tail this to rm command
rm command does not ask the name of the file
So what we do is to write command as follows:
$ rm {file-name}
rm : is the command
file-name :file to remove
7
Arguments - Specification
$ myshell foo bar
10
redirection symbols: ‘>’
There are three main redirection symbols: >,>>,<
(1) > Redirector Symbol
Syntax:
Linux-command > filename
For e.g.
To send output of ls command give
$ ls > myfiles
11
redirection symbols: ‘>>’
(2) >> Redirector Symbol
Syntax:
Linux-command >> filename
To output Linux-commands result
to the END of the file.
if file exist:
it will be opened
new information/data will be written to the END of the file,
without losing previous information/data,
if file does not exist, a new file is created.
For e.g.
To send output of date command
to already exist file give command
$ date >> myfiles
12
redirection symbols: ‘<’
13
Pipes
A pipe is a way
to connect the output of one program
to the input of another program
without any temporary file.
Definition
"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
14
Pipe - Examples
Command using Pipes Meaning or Use of Pipes
16
Filter: Examples
Suppose you have a file
called 'hotel.txt'
with 100 lines data,
you would like to print the content
only between the line numbers 20 and 30
and then store this result to the file 'hlist'
The appropriate command:
$ tail +20 < hotel.txt | head -n30 >hlist
Here head command is filter:
takes its input from tail command
tail command starts selecting
from line number 20 of given file
i.e. hotel.txt
and passes this lines as input to the head,
whose output is redirected
to the 'hlist' file.
17
Filter: Examples
18
Processing in Background
Use ampersand (&)
at the end of command
To start the execution in background
and enable the user to continue his/her processing
during the execution of the command
without interrupting
$ ls / -R | wc -l
This command will take lot of time
to search all files on your system.
19
Commands Related With Processes
For this purpose Use this Command Examples
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 {Proc-name} $ killall httpd
For background processing (With &, use to put linux-command & $ ls / -R | wc -l &
particular command and program in background)
if condition
used for making decisions in shell script,
If the 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
21
if condition - Examples
$ 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
22
Example: 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
the statement echo -e "\n\nFile $1, found and successfully echoed"
is proceed by shell.
if cat command is not successful
then it returns non-zero value
indicates some sort of failure
the statement echo -e "\n\nFile $1, found and successfully
echoed"
is skipped by our shell.
23
test command or [ expr ]
Syntax:
test expression or [ expression ]
24
test command - Example
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
25
Mathematical Operators
Normal
Mathematical
Arithmetical/
Operator Meaning But in Shell
Mathematical
in Shell Script
Statements
For test
For [ expr ] statement
statement with
if command with if command
26
String Operators
Operator Meaning
27
File and Directory Operators
Test Meaning
28
Logical Operators
Operator Meaning
29
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
30
if...else…fi
$ vi isnump_n
-Example
#!/bin/sh Try it as follows:
#
$ chmod 755 isnump_n
# Script to see whether argument is
$ isnump_n 5
positive or negative
5 number is positive
#
if [ $# -eq 0 ]
$ isnump_n -45
then -45 number is negative
echo "$0 : You must give/supply one
integers"
exit 1 $ isnump_n
fi ./ispos_n : You must
if test $1 -gt 0 give/supply one integers
then
echo "$1 number is positive" $ isnump_n 0
else echo "$1 number is negative“ 0 number is negative
fi
31
Loops in Shell Scripts
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 modifies
the value of the test (condition) variable.
32
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 statements between do and done)
done
33
for Loop: Example
Example:
$ cat > testfor •The for loop first creates i variable
for i in 1 2 3 4 5 •and assigned a number to i from the
do list of numbers 1 to 5,
•The shell executes echo statement for each
echo "Welcome $i times" assignment of i.
•This process will continue until all the
done items in the list were not finished,
•because of this it will repeat 5 echo
statements.
34
for loop - Example
$ vi chessboard
for (( i = 1; i <= 9; i++ )) ### Outer for loop ###
do
for (( j = 1 ; j <= 9; j++ )) ### Inner for loop ###
do
tot=`expr $i + $j`
tmp=`expr $tot % 2`
if [ $tmp -eq 0 ]; then
echo -e -n "\033[47m "
else
echo -e -n "\033[40m "
fi
done
echo -e -n "\033[40m" #### set back background colour to
black
echo "" #### print the new line ###
done
35
while Loop
Syntax:
while [ condition ]
do
command1
command2
..
....
done
Example:
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
36
End of Chapter
37