Make sure the script runs in the bash shell.. Make sure the script runs in the Korn shell.. $ cat first.bash #!/bin/ksh echo Antwerp Note that while first.bash will technically work as a
Trang 1sourLab 4: Working With Shell Script
Goal: In this lab, you will learn how to write a shell script.
1 Introduction to scripting
1 Write a script that outputs the name of a city.
$ echo 'echo Antwerp' > first.bash
$ chmod +x first.bash
$ /first.bash
Antwerp
2 Make sure the script runs in the bash shell.
$ cat first.bash
#!/bin/bash
echo Antwerp
3 Make sure the script runs in the Korn shell.
$ cat first.bash
#!/bin/ksh
echo Antwerp
Note that while first.bash will technically work as a Korn shell script, the name ending
in bash is confusing.
4 Create a script that defines two variables, and outputs their value.
$ cat second.bash
#!/bin/bash
var33=300
var42=400
echo $var33 $var42
Trang 25 The previous script does not influence your current shell (the variables do not exist outside of the script) Now run the script so that it influences your current shell.
source second.bash
6 Is there a shorter way to source the script ?
./second.bash
7 Comment your scripts so that you know what they are doing.
$ cat second.bash
#!/bin/bash
# script to test variables and sourcing
# define two variables
var33=300
var42=400
# output the value of these variables
echo $var33 $var42
2 Scripting tests and loops
1 Write a script that uses a for loop to count from 3 to 7.
#!/bin/bash
for i in 3 4 5 6 7
do
echo Counting from 3 to 7, now at $i
done
2 Write a script that uses a for loop to count from 1 to 17000.
#!/bin/bash
for i in `seq 1 17000`
do
echo Counting from 1 to 17000, now at $i
done
Trang 33 Write a script that uses a while loop to count from 3 to 7.
#!/bin/bash
i=3
while [ $i -le 7 ]
do
echo Counting from 3 to 7, now at $i
let i=i+1
done
4 Write a script that uses an until loop to count down from 8 to 4.
#!/bin/bash
i=8
until [ $i -lt 4 ]
do
echo Counting down from 8 to 4, now at $i
let i=i-1
done
5 Write a script that counts the number of files ending in txt in the current directory.
#!/bin/bash
let i=0
for file in *.txt
do
let i++
done
echo "There are $i files ending in txt"
6 Wrap an if statement around the script so it is also correct when there are zero files ending in txt.
#!/bin/bash
ls *.txt > /dev/null 2>&1
if [ $? -ne 0 ]
then echo "There are 0 files ending in txt"
else
let i=0
for file in *.txt
do
let i++
done
echo "There are $i files ending in txt"
fi
3 Parameters and options
1 Write a script that receives four parameters, and outputs them in reverse order.
echo $4 $3 $2 $1
2 Write a script that receives two parameters (two filenames) and outputs whether those files exist.
#!/bin/bash
if [ -f $1 ]
then echo $1 exists!
else echo $1 not found!
fi
if [ -f $2 ]
then echo $2 exists!
else echo $2 not found!
fi
3 Write a script that asks for a filename Verify existence of the file, then verify that you own the file, and whether it is writable If not, then make it writable.
4 Make a configuration file for the previous script Put a logging switch in the config
Trang 4file, logging means writing detailed output of everything the script does to a log file
in /tmp.
4 More scripting
1 Write a script that asks for two numbers, and outputs the sum and product (as shown here).
Enter a number: 5
Enter another number: 2
Sum: 5 + 2 = 7
Product: 5 x 2 = 10
#!/bin/bash
echo -n "Enter a number : "
read n1
echo -n "Enter another number : "
read n2
let sum="$n1+$n2"
let pro="$n1*$n2"
echo -e "Sum\t: $n1 + $n2 = $sum"
echo -e "Product\t: $n1 * $n2 = $pro"
2 Improve the previous script to test that the numbers are between 1 and 100, exit with an error if necessary.
echo -n "Enter a number between 1 and 100 : "
read n1
if [ $n1 -lt 1 -o $n1 -gt 100 ]
then
echo Wrong number
exit 1
fi
3 Improve the previous script to congratulate the user if the sum equals the product.
if [ $sum -eq $pro ]
then echo Congratulations $sum == $pro
fi
4 Write a script with a case insensitive case statement, using the shopt nocasematch option The nocasematch option is reset to the value it had before the scripts started.
#!/bin/bash
#
# Wild Animals Case Insensitive Helpdesk Advice
#
if shopt -q nocasematch; then
nocase=yes;
else
nocase=no;
shopt -s nocasematch;
fi
echo -n "What animal did you see ? "
read animal
case $animal in
"lion" | "tiger")
echo "You better start running fast!"
;;
"cat")
echo "Let that mouse go "
;;
"dog")
echo "Don't worry, give it a cookie."
;;
Trang 5"chicken" | "goose" | "duck" )
echo "Eggs for breakfast!"
;;
"liger")
echo "Approach and say 'Ah you big fluffy kitty.'"
;;
"babelfish")
echo "Did it fall out your ear ?"
;;
*)
echo "You discovered an unknown animal, name it!"
;;
esac
if [ nocase = yes ] ; then
shopt -s nocasematch;
else
shopt -u nocasematch;
fi
Scoring
Part 1 - 25%
Part 2 - 25%
Part 3 - 25%
Part 4 - 25%