Sie sind auf Seite 1von 24

Chapter 3

Writing and Executing a shell script


Advanced Linux administration using
Fedora v. 9
Lecturer: Mr. Kao Sereyrath, MScIT (SMU, India)
Director of Technology and Consulting Service (DCD Organization)
ICT Manager (CHC Microfinance Limited)
Contents
Advantage of using shell script
1
3
4
5
2
Creating and executing shell script
Learn about variable
Learn about operator
if and case statement
6
for, while and until statement
7
Create function in shell script
Advantage of using shell script
Hundreds of commands included with Fedora are actually
shell scripts. For example startx command.
Shell script can save your time and typing, if you routinely
use the same command lines multiple times every day.
A shell program can be smaller in size than a compiled
program.
The process of creating and testing shell scripts is also
generally simpler and faster than the development process.
You can learn more with Sams Teach Yourself Shell
Programming in 24 Hours
Creating and Executing shell script
To create shell script :
You can use vi command. Because it won't wrap text.
Or you can use nano -w to disable line wrap
For example, to create myenv file
vi /etc/myenv
To execute shell script :
First you need to set file permission
chmod +x myenv
Creating and Executing shell script (Cont.)
To execute myenv file in /etc directory
./myenv
To enable shell script as your Linux command, first you
need to create bin directory in user home directory. Then
put shell script file into that directory.

mkdir bin
mv /etc/myenv bin
myenv
Learn about variable
There are 3 types of variable:
Environment variables: Part of the system
environment, you can use and modify them in your shell
program. For example PATH variable.

Built-in variables: Unlike environment variables, you
cannot modify them. For example $#, $0

User variable: Defined by you when you write a shell
script.
Learn about variable (Cont.)
Positional parameter

if [ $# -eq 0 ]
then
echo "Tell me your name please"
else
echo "Welcome "$1
fi

$# is a positional parameter and $1 is to get first
parameter. You can use $2 to get second parameter.
Learn about variable (Cont.)
Using single quote to escape variable

var="Welcome to Linux"
echo 'The value of $var is '$var

The output is:

The value of $var is Welcome to Linux
Learn about variable (Cont.)
Using backtick ( ` ) to execute Linux command

ls -l >> myfile.txt
mycount=`wc -l myfile.txt`
echo "File size: "$mycount


The output is:

File size: 33 myfile.txt

Learn about Operator
Comparison operator

= To compare whether two strings are equal
!= To compare whether two strings are not equal
-n To evaluate whether the string length is greater than zero
-z To evaluate whether the string length is equal to zero

#String comparison
string1="abc"
string2="Abc"
if [ $string1 = $string2 ]
then
echo "string1 equal to string2"
else
echo "string1 not equal to string2"
fi
Learn about Operator (Cont.)
#String comparison
string1="abc"
string2="Abc"
if [ $string1 != $string2 ]; then
echo "string1 not equal to string2"
else
echo "string1 equal to string2"
fi

if [ $string1 ]; then
echo string1 is not empty
else
echo string1 is empty
fi
Learn about Operator (Cont.)
if [ -n $string2 ]; then
echo string2 has a length greater than zero
else
echo string2 has length equal to zero
fi

if [ -z $string1 ]; then
echo string1 has a length equal to zero
else
echo string1 has a length greater than zero
fi

Learn about Operator (Cont.)
Number comparison

-eq To compare Equal
-ge To compare Greater than or equal to
-le To compare Less than or equal to
-ne To compare Not equal
-gt To compare Greater than
-lt To compare Less than

if [ $number1 -gt $number2 ]; then
echo number1 is greater than number2
else
echo number1 is not greater than number2
fi
Learn about Operator (Cont.)
File operator

-d Check if it is a directory
-f Check if it is a file
-r Check if the file has Read permission
-w Check if the file has Write permission
-x Check if the file has Execute permission
-s Check if file exist and has length greater than zero
Learn about Operator (Cont.)
filename="/root/bin/myfile.txt"
if [ -f $filename ]; then
echo "Yes $filename is a file"
else
echo "No $filename is not a file"
fi
if [ -x $filename ]; then
echo "$filename has Execute permission"
else
echo "$filename has no Execute permission"
fi
if [ -d "/root/bin/dir1" ]; then
echo "It is a directory"
else
echo "No it is not a directory"
fi
Learn about Operator (Cont.)
Logical operator

! To negate logical expression
-a Logical AND
-o Logical OR

if [ -x $filename1 -a -x $filename2 ]; then
echo "$filename1 and $filename2 is executable"
else
echo "$filename1 and $filename2 is not executable"
fi
if [ ! -w $file1 ]; then
echo $file1 is not writable
else
echo $file1 is writable
fi
if and case statement
if statement syntax
if [ expression ]; then
Statements
elif [ expression ]; then
Statements
else
Statements
fi

Example:
if [ $var = Yes ]; then
echo Value is Yes
elif [ $var = No ]; then
echo Value is No
else
echo Invalid value
fi
if and case statement (Cont.)
case statement syntax
case str in
str1 | str2)
Statements;;
str3 | str4)
Statements;;
*)
Statements;;
esac

Example:
case $1 in
001 |01 | 1) echo "January";;
02 | 2) echo "February";;
3) echo "March";;
*) echo "Incorrect supplied value";;
esac
for, while and until statement
Example1: for statement

for filename in *
do
cp $filename backup/$filename
if [ $? -ne 0 ]; then
echo copy for $filename failed
fi
done

Above example is used to copy all files from current directory
to backup directory. if [ $? -ne 0 ] statement is used to
check status of execution.

for, while and until statement (Cont.)
Example2: for statement

echo "You have passed following parameter:"
i=1
for parlist in $@
do
echo $i" "$parlist
i=`expr $i + 1`
done

If you type myenv domain1 domain2. The result is:

You have passed following parameter:
1 domain1
2 domain2
for, while and until statement (Cont.)
Example: while statement

loopcount=0
while [ $loopcount -le 4 ]
do
useradd "user"$loopcount
loopcount=`expr $loopcount + 1`
done

Above statement is used to create user0, user1, user2, user3,
user4.
for, while and until statement (Cont.)
Example: until statement

loopcount=0
until [ $loopcount -ge 4 ]
do
echo $loopcount
loopcount=`expr $loopcount + 1`
done

Above statement is used to output 0, 1, 2, 3 to display.
Create function in shell script
You can create function by using below example.
myfunc()
{
case $1 in
1) echo "January";;
2) echo "February";;
3) echo "March";;
4) echo "April";;
5) echo "May";;
6) echo "June";;
7) echo "July";;
8) echo "August";;
9) echo "September";;
10) echo "October";;
11) echo "November";;
12) echo "December";;
*) echo "Invalid input";;
esac }
Calling function

myfunc 3



Output is

March

Das könnte Ihnen auch gefallen