Advanced Linux-Chapter IX-Shell Script
Advanced Linux-Chapter IX-Shell Script
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)
Contents
1
2 3 4
5 6
7
Shell script can save your time and typing, if you routinely
use the same command lines multiple times every day.
YoucanlearnmorewithSamsTeachYourselfShell
Programming in 24 Hours
fi
if [ -z $string1 ]; then echo string1 has a length equal to zero
else
echo string1 has a length greater than zero fi
-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
Example:
if [ $var = Yes ]; then echo Value is Yes elif [ $var = No ]; then echo Value is No else echo Invalid value fi
Example:
case $1 001 |01 02 | 2) 3) echo *) echo esac in | 1) echo "January";; echo "February";; "March";; "Incorrect supplied value";;
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.
user4.
Calling function
myfunc 3
Output is
March