You are on page 1of 24

Advanced Linux administration using

Fedora v. 9

Chapter 3
Writing and Executing a shell script

Lecturer: Mr. Kao Sereyrath, MScIT (SMU, India)
Director of Technology and Consulting Service (DCD Organization)
ICT Manager (CHC Microfinance Limited)

 A shell program can be smaller in size than a compiled program. if you routinely use the same command lines multiple times every day.  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” .  Shell script can save your time and typing. Advantage of using shell script  Hundreds of commands included with Fedora are actually shell scripts. For example startx command.

Because it won't wrap text. Creating and Executing shell script To create shell script :  You can use vi command. to create myenv file vi /etc/myenv To execute shell script :  First you need to set file permission chmod +x myenv .  Or you can use nano -w to disable line wrap  For example.

)  To execute myenv file in /etc directory . Creating and Executing shell script (Cont. first you need to create bin directory in user home directory. mkdir bin mv /etc/myenv bin myenv . Then put shell script file into that directory./myenv  To enable shell script as your Linux command.

For example $#. you can use and modify them in your shell program. Learn about variable  There are 3 types of variable: – Environment variables: Part of the system environment. For example PATH variable. $0 – User variable: Defined by you when you write a shell script. you cannot modify them. – Built-in variables: Unlike environment variables. .

. Learn about variable (Cont. You can use $2 to get second parameter.) 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.

) 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 .txt mycount=`wc -l myfile. Learn about variable (Cont.txt` echo "File size: "$mycount  The output is: File size: 33 myfile.

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 .

then echo “string1 is not empty” else echo “string1 is empty” 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 ].

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 .

) 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.

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 .

then echo "It is a directory" else echo "No it is not a directory" fi .txt" if [ -f $filename ]. then echo "$filename has Execute permission" else echo "$filename has no Execute permission" fi if [ -d "/root/bin/dir1" ].) filename="/root/bin/myfile. then echo "Yes $filename is a file" else echo "No $filename is not a file" fi if [ -x $filename ]. Learn about Operator (Cont.

Learn about Operator (Cont. then echo "$filename1 and $filename2 is executable" else echo "$filename1 and $filename2 is not executable" fi if [ ! -w $file1 ].) Logical operator ! To negate logical expression -a Logical AND -o Logical OR if [ -x $filename1 -a -x $filename2 ]. then echo “$file1 is not writable” else echo “$file1 is writable” fi .

then echo “Value is No” else echo “Invalid value” fi . then Statements elif [ expression ]. then Statements else Statements fi Example: if [ $var = “Yes” ]. if and case statement if statement syntax if [ expression ]. then echo “Value is Yes” elif [ $var = “No” ].

.. *) echo "Incorrect supplied value". 02 | 2) echo "February".. *) Statements... 3) echo "March"... str3 | str4) Statements. esac .) case statement syntax case str in str1 | str2) Statements. if and case statement (Cont. esac Example: case $1 in 001 |01 | 1) echo "January".

for. . then echo “copy for $filename failed” fi done Above example is used to copy all files from current directory to backup directory. while and until statement Example1: for statement for filename in * do cp $filename backup/$filename if [ $? -ne 0 ]. if [ $? -ne 0 ] statement is used to check status of execution.

while and until statement (Cont.for. The result is: You have passed following parameter: 1 domain1 2 domain2 .) 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.

) 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.for. while and until statement (Cont. user2. user3. . user4.

1. .for. while and until statement (Cont. 2. 3 to display.) Example: until statement loopcount=0 until [ $loopcount -ge 4 ] do echo $loopcount loopcount=`expr $loopcount + 1` done Above statement is used to output 0.

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