CIT052 Index > Shell Input and Conditional Constructs

Shell Input and Conditional Constructs

Part 1

Write a script named lastname_firstname_checking.sh that does the following:

  1. Take a command-line argument, a user’s login name.
  2. If no argument was given, prompt the user for a login name and read it in.
  3. Check to see if the user is logged in (use the who command and pipe it to grep).
  4. If the user is not logged in, print a message like:
    username is not logged on.
  5. If the user is logged in, print all the processes that the user is running. (Use ps -aux and pipe it to grep.)

See pseudocode

Part 2

Write a “calculator” program in a script named lastname_firstname_calculator.sh. This script will ask users to enter a simple expression of the form number operator number, where the numbers are integers and the operator is either +, -, *, or /. If the operator is not one of those four, you must produce an error message. Your script must use the case construction.

In order to assign an asterisk * to a variable without having bash expand it as a pathname, you must put the following line in your script before you start processing user input:

GLOBIGNORE='*'

Hints

See pseudocode

Here is sample output from several runs of the program. Your program does not have to give exactly these prompts and error messages, but it must exhibit similar behavior.

Enter expression: 3 + 5
8

Enter expression: 15 - 2
13

Enter expression: 12*144
1728

Enter expression: 1892/4
473

Enter expression: 3^2
Unknown or missing operator in 3^2

When You Finish

Make sure the script files are named in the form lastname_firstname_checking.sh and lastname_firstname_calculator.sh, and upload them to Moodle.