Академический Документы
Профессиональный Документы
Культура Документы
Shell Script
o Shell script = program written in shells own programming language
o Text file containing collection of executable commands
o Can be created in any text editor
o No particular extension is needed, however may use .scr or .sh for your own convenience
(3) Commands
o Can consist of any Unix/Linux command that would run at command line
o Will be executed in order from top to bottom, when we execute the script
o Blank lines can be used anywhere without impact
o Commands can be combined using pipes etc.
(1) Type in the following script into a file called myscript.scr, using any text editor
#!/bin/bash
# Script file: myscript.scr. A very simple script
ls l | more
praveen.mitera@senecacollege.ca
Arguments and positional parameters
o Allow us to use a general script that is more useful
o Arguments = user-supplied data that follows the script name (=input to script)
o Positional parameters = pre-defined memory variables in the shell script
o 9 positional parameters ($1, $2$9) are used to store arguments entered by user
#!/bin/bash
# script to show how parameters are accepted
clear
echo script is : $0
echo No. of arguments : $#
echo first argument : $1
echo second argument : $2
echo process id : $$
Do the same as above, but pass the # of lines and filename as parameters to the script
head -$1 $2
tail -$1 $2
./script3.scr 3 file1 (will display 3 ilnes from top and 3 lines from bottom of file1)
praveen.mitera@senecacollege.ca
Mathematical operators (Eg. - + * / )
To evaluate mathematical expressions, use double round brackets around the expression
((num=15*6))
echo $num
In Bash shell, the return value of a program or command can be checked with $?
Assume that file file1 exists and file3 does not.
ls file1
echo $? (will return 0 : success)
ls file3
echo $? (will return 1 or another non-zero value: failure)
Script (backup)
cp -i $1 ~/Backup copies filename passed to script into ~/Backup directory under the same name
cp -i $1 ~/Backup/$1.bak copies filename passed to script into ~/Backup directory under same name followed by .bak
cp -i $1 $2 ~/Backup copies 2 filenames passed to script into ~/Backup directory under the same names
cp -i $@ ~/Backup copies all filenames passed to script into ~/Backup directory under the same names
praveen.mitera@senecacollege.ca
the FOR loop
Accept numbers 1 to 6 as argument list and display them using a for loop
for i in 1 2 3 4 5 6
do
echo $i
done
To run the same loop as above, but make it flexible by accepting 6 parameters from user:
for i in $1 $2 $3 $4 $5 $6
do
echo $i
done
For above loop, to accept ALL parameters that user enters (HINT: use $* or $@ to capture all parameters):
for i in $*
do
echo $i
done
if statement
if (( $# == 0 )) # checks if user entered atleast one parameter
then
echo " please enter atleast one parameter"
exit 1 # exit statement makes the script STOP
fi
if-then-else statement
egrep apples file1
if (( $? == 0 )) #checks if previous command was successfull
then
echo previous command was successfull"
else
echo previous command was NOT successfull"
fi
praveen.mitera@senecacollege.ca
The WHILE loop
while condition
do
Commands
done
#!/bin/bash
# to demonstrate the while loop
secretnumber=7
echo
echo "Guess the number (between 1 and 10)"
counter=1
read yourguess
while (( $yourguess != $secretnumber ))
do
echo "No. Try again."
(( counter = counter + 1 ))
echo "Guess the number (between 1 and 10)"
read yourguess
done
echo "Good. You took $counter guesses to guess the right number"
until condition
do
Commands
done
#!/bin/bash
secretnumber=7
echo
echo "Guess the number (between 1 and 10)"
counter=1
read yourguess
until (( $yourguess == $secretnumber ))
do
echo "No. Try again."
(( counter = counter + 1 ))
echo "Guess the number (between 1 and 10)"
read yourguess
done
echo "Good. You took $counter guesses to guess the right number"
praveen.mitera@senecacollege.ca