CIT 052 Index > Looping and Functions

Looping and Functions in the Shell

You will write a shell script with the name lastname_firstname_menu.sh that repeatedly displays the following menu:

[1] Find user
[2] List large files
[3] Disk usage
[4] View Log File
[5] Exit

Your choice >

If you enter a number other than one through five, the script must give you an error message.


If you enter 1 (Find user), the script will:


If you enter 2 (List large files), the script will:


If you enter 3 (Disk usage), the script will do a df command.


When you choose command 4 (View Log File), the script will display a file named logfile.txt with appropriate headings.

Your script will create this file as follows: Every time a command is entered, you will append a line to a file named logfile.txt. The line will have the user name ($LOGNAME), the menu choice, and the output of the date command, separated by percent signs. The menu choice must be given in words, as shown in the sample output. Don’t just give the menu choice as a number—when you look at the log file a month from now, you want to know that a user did a “disk usage” command, not “3.”

Invalid commands and the exit command do not have to be entered in the log file if you don’t want to.


If you enter 5 (Exit), the script will exit.


Menu options 1, 2, and 4 (find user, list large files, and view log file) must be implemented as separate functions. Add any other functions that you feel will make your job easier.

Hints

Sample Output

Here is sample output from a run of this script. User input is in boldface so that you can see it more easily; it won’t show up in bold when you run your script.

david@tux:/media/sda1/cit052> ./your_name.sh

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 1
User name > root
Found root in group 0

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 7
Please enter a number from 1 to 5.

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 1
User name > fred
No user named fred

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 3
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda3              7341472   3453344   3888128  48% /
/dev/hda2               514060     39152    474908   8% /boot
/dev/hda5             16779340   7963024   8816316  48% /home
/dev/hda6              4200828    217504   3983324   6% /usr/local
shmfs                   184432         0    184432   0% /dev/shm
/dev/sda1                62436     45890     16546  74% /media/sda1

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 2
List files larger than > 7000
File Name                Size
CIT-52-05-awk.sxi        33509
CIT-52-Fin.doc           40960
CIT-52-Lab1.doc          25088
CIT-52-Lab2.doc          25088
CIT-52-Mid.doc           39424
CIT-52-Mid.sxw           10437
CIT-52-Q5.doc            23040
CIT-52-Q6.doc            23552
CIT-52-Q7.doc            23040
cit052.xml               16301
cit052.xml~              13716
green052.fo              20016
green052.fo~             20017
green052.html             8007
green052.html~            8008

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 4
User       Menu Choice          Time
david      Find user            Wed Dec  3 23:15:43 PST 2003
david      Find user            Wed Dec  3 23:15:46 PST 2003
david      Disk usage           Wed Dec  3 23:15:51 PST 2003
david      List files           Wed Dec  3 23:15:54 PST 2003

[1] Find user
[2] List large files
[3] Disk usage
[4] View log file
[5] Exit
Your choice > 5
david@tux:/media/sda1/cit052>

When You Finish

Make sure your script is named in the form lastname_firstname_menu.sh and upload it to Moodle.