Sie sind auf Seite 1von 12

GNU/Linux Shell

Programming
Click to add Text
What is Shell Script ?
 Normally shells are interactive.
 It means shell accept command from you (via
keyboard) and execute them. But if you use command
one by one (sequence of 'n' number of commands) , the
you can store this sequence of command to text file and
tell the shell to execute this text file instead of entering
the commands. This is know as shell script.
 Shell script defined as:
"Shell Script is series of command written in plain text
file. Shell script is just like batch file is MS-DOS but have
more power than theShellMS-DOS batch file."
Programming
Fundamentals 2
How to write shell script
 Following steps are required to write shell script:
 (1) Use any editor like vi or gedit to write shell script.
 (2) After writing shell script set execute permission for your script as follows
syntax:
chmod permission your-script-name
 Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name
 Note: This will set read write execute(7) permission for owner, for group and
other permission is read and execute only(5).
 (3) Execute your script as
syntax:
bash your-script-name
sh your-script-name
./your-script-name
 Examples:
Examples:
$ bash bar
$ sh bar
$ ./bar Shell Programming
Fundamentals 3
Control Structures
 if .. then
 if .. then .. else
 if .. then .. elif
 for .. in
 for
 while
 until
 case
Shell Programming
Fundamentals 4
Control Structure if .. then
 $ cat > ifdemo
echo –n “Word1: ”
read word1
echo –n “Word2: ”
read word2
if test “$word1” = “$word2”
then
echo “Match”
fi
echo “End of program.”

 $ ifdemo
Word1: apple
Word2: apple
Match
End of program.
Shell Programming
Fundamentals 5
Control Structure if .. then .. else
 $ cat > chk_regfile

if [ $# -eq 0 ]
then
echo “At least one argument reqd.”
exit 1
fi

if test –f “$1”
then
echo “$1 is a regular file in the working directory”
else
echo “$1 is NOT a regular file in the working directory”
fi

 -e file exists
-d file exists and is a directory file
-s file exists and has a length > 0
-r file exists and is readable
-w file exists and is writeable
-x file exists and Shell
is Programming
executable
Fundamentals 6
Control Structure if .. then .. elif
 $ cat > if_elif
echo –n “word 1: ”
read word1
echo –n “word 2: ”
read word2
echo –n “word 3: ”
read word3
if [ “$word1” = “$word2” –a “$word2” = “$word3” ]
then
echo “Match: words 1, 2 and 3”
elif [ “$word1” = “$word2” ]
then
echo “Match: words 1 and 2”
elif [ “$word1” = “$word3” ]
then
echo “Match: words 1 and 3”
elif [ “$word2” = “$word3” ]
then
echo “Match: words 2 and 3”
else
echo “No match”
fi Shell Programming
Fundamentals 7
Control Structure while
 $ cat > numcount
number=0
while [ $number –lt 10 ]
do
echo –n “$number”
let number=“$number” + 1
done
echo

Shell Programming
Fundamentals 8
Control Structure until
 $ cat > guesswhat
secretword = omega
wordguess = blank
echo “Try to guess the secret word”
echo
until [ “$wordguess”=“$secretword” ]
do
echo –n “Your guess: ”
read wordguess
done
echo “Excellent!”

Shell Programming
Fundamentals 9
break and continue
 for, while and until loop can be
interrupted with a break or continue
statement.
 Break transfers control after done, i.e.,
the loop is broken.
 Continue transfers control to done,
skipping the intermediate steps.
Shell Programming
Fundamentals 10

break and continue example
$ cat > brk_cont
for index in 1 2 3 4 5 6 7 8 9 10
do
if [ $index –le 3 ]; then
echo “continue”
continue
fi
echo $index
if [ $index –ge 8 ]; then
echo “break”
break
fi
done
 $ bash brk_cont
continue
continue
continue
4
5
6
7
8
break
Shell Programming
Fundamentals 11
Control Structure case
 $ cat > casedemo
echo –n “Enter A, B or C: ”
read letter
case “$letter” in
a|A)
echo “You entered A”;;
b|B)
echo “You entered B”;;
c|C)
echo “You entered C”;;
*)
echo “You did not enter A, B, C”;;
esac
 * matches any string of characters
 ? matches any single character
 […] defines a character class
 | denotes or

Shell Programming
Fundamentals 12

Das könnte Ihnen auch gefallen