CS160B Assignment 1

Objective: Write a shell script to output information about the machine that the user is logged into. The output of this shell script should be formatted in an easy-to-read_format. See the course examples. You must use variables, command and arithmetic substitution to accomplish this.

Writing Your Script

This shell script, getinfo.sh, must output some information about itself. The information relies on two standard shell variables: LOGNAME and PATH. If either of these variables does not have a value when your shell script starts, it should abort with a message indicating the problem. You may not use temporary files nor if statements. Your script must accomplish the following:

  1. A welcome message indicating your name. If you have entered a name using chfn, that name will be the output. Otherwise, your login name will be the output. Hint: use the Operators on variables to accomplish this.

  2. The host you are logged in on

  3. The date and time you last logged in, not including this time. Hint: Try last -2 $LOGNAME. If this is the first time you have logged into this system, it is acceptable for the output to indicate the last login is the current login session.

  4. How many local (non-environment) and environment variables you have defined. Hint: You may find printenv or set useful commands. Also, this requires some arithmetic.

  5. The directories in your search path, in the order they are searched, one-per-line.

  6. How long the system has been up.

  7. The filesystem you are currently on (this is called the mount point), the percentage of free space it has currently, and the space it currently has free. Of course, this last piece of information will change if you change the directory to another filesystem. Hint: the command df "$PWD" will give information about the current filesystem.

  8. The IP address of your machine of the format inet xxx.xxx.xxx.xxx where xxx are numbers. Hint: the command ifconfig or ip addr may be useful

Testing Your Script

Give your script executeable status and then run your script a few times to test the output:

Sample Output


$ ./getinfo.sh 
grwoo
Thanks for spending some time with me today:  Grace Woo
You are logged onto:
hills.ccsf.edu
Here is the info for the last time you were logged in
grwoo pts/9 Tue Oct 31 22:32 - 22:33 (00:00) wtmp begins Sun Oct 1 03:41:58 2017
Total number of environment variables on your machine
22
Total number of local shell variables on your machine
53
Here are the directories in your search PATH:
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
/sbin
/users/grwoo/.local/bin
/users/grwoo/bin
 22:34:01 up 78 days, 15:30, 53 users,  load average: 0.01, 0.02, 0.05
Filesystem             1K-blocks     Used Available Use% Mounted on
/dev/mapper/vg01-users  52403200 15545536  36857664  30% /users
Your IP address is:
inet 147.144.1.2 netmask 255.255.255.0 broadcast 147.144.1.255

$ cd /boot
$ /users/grwoo/examples/getinfo.sh 
grwoo
Thanks for spending some time with me today:  Grace Woo
You are logged onto:
hills.ccsf.edu
Here is the info for the last time you were logged in
grwoo pts/9 Tue Oct 31 22:32 - 22:33 (00:00) wtmp begins Sun Oct 1 03:41:58 2017
Total number of environment variables on your machine
22
Total number of local shell variables on your machine
53
Here are the directories in your search PATH:
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
/sbin
/users/grwoo/.local/bin
/users/grwoo/bin
 22:34:14 up 78 days, 15:31, 53 users,  load average: 0.01, 0.02, 0.05
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sda1        1038336 240660    797676  24% /boot
Your IP address is:
inet 147.144.1.2 netmask 255.255.255.0 broadcast 147.144.1.255

$ cd
$ LOGNAME=
$ examples/getinfo.sh
examples/getinfo.sh: line 4: LOGNAME: Who is logged in?

    

Turning In Your Assignment