Categories
Computer Technologies and O/S

FIT9018: Week 9

Week 9 saw Andy delve further into utilizing the Unix shell followed by a tutorial on setting up the Apache HTTP server.

www.apache.org

On the unix shell, we explored shell scripts. The basics that we covered:

Pearl of the week – Common shell script errors:

[-z “$x”] error : no space near the [ ]’s
[ “$x”=”abc“ ] error : no space around =
y = 20 error : extra spaces around =

  • # -> comment character
  • x=y -> assign vaiable (no space)
  • shell keys words stored in .profile
  • Standard variables:
    • $$ the process id of the shell
    • $0 the name of the shell script (if applicable)
    • $1…$9 $n refers to the nth command line argument
    • $# contains the number of arguments
    • $* a list of all the command line arguments
    • $? Exit status (more on that shortly)
  • ; -> End of line
  • if, then, else, for, for in, if-then-elif, while, until, break continue, case, trap.

IF THEN ELSE:

if [ $var1 = $var2 ]
then
echo var1 and var2 are the same
else
echo var1 and var2 are different
fi

IF THEN:
if [ $# -eq 0 ]
then
echo Error : at least one argument must be specified
exit 1
fi

FOR:

for name in `cat users`
do
cp assignment1 /home/$name #file -> student's directory
chown $name /home/$name/assignment1 #change owner
echo Done $name
done

FOR IN:

for loop-index in argument-list
do
commands
done

IF THEN ELIF:

if [ "$word1" = "$word2" –a "$word2" = "$word3" ]
then
echo "Match: words 1, 2, & 3"
elif [ "$word1" = "$word2" ]
then
echo "Match: words 1 & 2"
elif [ "$word1" = "$word3" ]
then
echo "Match: words 1 & 3"
elif [ "$word2" = "$word3" ]
then
echo "Match: words 2 & 3"
else
echo No match
fi

CASE:

echo "Enter A, B or C: \c"
read character
case "$character" in
a|A)
echo You entered A
;;
b|B)
echo You entered B
;;
c|C)
echo You entered C
;;
*)
echo You did not enter A, B, or C
esac

UNTIL:

until [ $# = 0 ] # no of arguments in the command
do
if [ -d $1 ] # if first argument is a directory
then
echo Contents of $1:
ls -F $1 # list files in directory
fi
shift
echo There are $# items left on the command line
done

WHILE:

ans=y
while [ "$ans" = y ]
do
echo Enter a name
read name
echo $name >> file.names
echo –n "Continue [y/n]: "
read ans
done

Leave a Reply

Your email address will not be published. Required fields are marked *