3) Shell Scripts

Chapter 6

Shells

Different shells: sh, ksh, rsh, csh

Exercise 1

Find out which shells are on nations (hint: shells are binaries and end in "sh")
The default shell for a user is usually defined in the /etc/passwd file.
You can start a new shell by typing its name. You exit a shell by typing "exit".

Exercise 2

Have a look at /etc/passwd

Switch between different shells. Execute the following (humorous) commands in the csh and ksh:
cat "food in cans"
rm God
man: why did you get a divorce?
make fire
why not?
[Where is my brain?

Serious question: how can you tell whether there are different shells running in your terminal window?

Environment variables

Exercise 3

Type "printenv".
(In csh) Type: set newstuff="stuff"; echo $newstuff
Type: unset newstuff; echo $newstuff
Type: setenv NEWSTUFF stuff; printenv
Type: unsetenv NEWSTUFF; printenv

User login scripts

The csh uses ".login" (which is only executed when users login) and ".cshrc" (which is executed each time a new shell starts) as login scripts. The ksh uses ".profile" and ".kshrc" instead. The windows environment uses additional login files (such as .dtprofile) where you define the initial settings of the desktop environment. There are also global login files in the /etc directory that are executed before the user's files.

Exercise 4

Have a look at ".login" and ".cshrc".

Type: cat /usr/local/etc/motd
Find this command in .cshrc. Obviously it is misplaced in .cshrc and should be in .login.
Execute the last five lines of .cshrc separately.

Create a file (using echo) and look at its permissions. Type: umask 077. Create another file and look at its permissions.

Change umask permanently in your .cshrc. After editing the file type "source .cshrc" to make the changes valid for the current shell.

It is important to execute .cshrc immediately (by typing source .cshrc) after making changes. If you should make a mistake while editing .cshrc, you will not be able to login anymore! The only way to fix that then is to ftp from a different machine, get the .cshrc file (but don't overwrite you local .cshrc), make changes to it and ftp it back.

Type: setenv PATH .
Type: ls
Type: /bin/ls
How can you fix this problem?

Type: set prompt="%"

Type: history
Type: h
Type: alias h history
Type: h

Add aliases to your .cshrc that make "rm", "mv", "cp" safer. The Sun comes with a Trash bin that you can use by dragging and dropping files into it. You can also create one yourself that works on any Unix computer and for command line "rm". See the red comments here.

Shell scripts

Chapters 13 and 16

Any commands can be combined in a shell. We will be using the sh shell for the following examples.

Example 1

:
# date and users
#
date
who -u

Save this in a file called dw. To execute this script either type "sh dw" or type "chmod 700 dw" and then "dw".

Example 2

:
# A simple menu
#
quit=n
while test "$quit" = "n"
do
clear
echo " Menu "
echo "---------"
echo
echo "1. List Users"
echo "2. Show Date"
echo "3. Quit"
echo
echo "Enter Choice: ."
read choice
case $choice in
1) who
echo "press enter"
read dummy;;
2) date
echo "press enter"
read dummy;;
3) quit=y;;
*) echo "Invalid choice!"
echo "press enter"
read dummy;;
esac
done

Example 3

:
# if statement
#
echo "guess a number"
read number
if test "$number" -eq 7
then
echo "That is the number!"
else
echo "Wrong number"
fi

Exercise 5

Write a script that lets a user guess a number between 1 and 9 and stops only when the correct number is guessed.