Shell Script
Shell Script
shell programs or shell procedures. There is no restriction on the extension of the filename, though it is a universal convention that the extension .sh be used for shell programs. 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 the MS-DOS batch file."
Why to Write Shell Script ? Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.
Starting with scripts: Steps to create shell script 1. vi script1.sh 2. press i or I 3. type list of commands eg. #first shell script date cal <ESC>:wq 4. execute file 1. sh script.sh 2. chmod +x script.sh or chmod 755 script.sh script.sh Using if 1 if condition is true then execute commands fi
if condition is true then execute commands else execute commands fi if [ condition ] ; then execute statements elif [ condition ] ; then execute statements else execute statements fi
Case conditional case expression in pattern1) pattern2) *) esac execute cmds ;; execute cmds ;; ... ... execute cmds
Relational operators used by if Meaning Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to
String Tests used by test Exit Status True if string stg is not a null string True if string stg is a null string True if string s1 = s2 True if string s1 is not equal to s2 True if string stg is assigned and not null
File related Tests with test Test -e file -f file-r file-w file -x file -d file -s fileExit Status True if file exists True if file exists and is a regular file True if file exists and is a readable file True if file exists and is a writable file True if file exists and is a executable file True if file exists and is a directory file True if file exists and has a size greater than zero
while Loop while condition is true do execute commands done for loop for variable in list do execute commands done