Basic Shell Scripting
- Practice -

This page contains questions to build shell scripts to perform tasks. It is recommended that you perform this exercise after completing the online tutorial called lab5

--------------------------------------------------------------------------------------------------

Written Questions:

NOTE:    The answers to these written questions will be discussed in a review session prior to the midterm.
                The answers to these questions will not be posted on the website, so if you decide
                not to attend, it will be your responsibility to determine the answers to these questions
                by talking with your classmates or read my course notes or the course text.
 

  1. Write the command that is contained on the first line of a bash shell for each or the following situations:

  2.  

     

    Run in the bash shell regardless of the current shell (if available):
     

    Run in the z shell regardless of the current shell (if available)
     

    Run in the c shell regardless of the current shell (if available)
     

    Run in the Korn shell regardless of the current shell (if available)
     
     

  3. Write a portable bash shell script called ask to prompt the user for the user's full name.

  4. After the user enters their full name, display the message:

    Your name is <your full name> and your username is <your gothic username>
     

    Remember, that the gothic username will change depending on who is logged on.

    Write your shell script below:
     
     
     
     
     
     
     
     
     
     
     
     

  5. Write a portable bash shell script.called add that will prompt and read two numbers (integers only).

  6. After the user enters both numbers, display the sum of both integers. The output should appear as displayed
    (assume the first integer entered is 5 and the second integer entered is 6).
     

    $ add

    Please enter the first integer: 5

    Please enter the second integer: 6

    The sum of integers 5 and 6 are 11.

    Write your shell script below:
     
     
     
     
     
     
     
     
     
     
     
     

  7. Modify the previous ask shell script to accept the integers as arguments after the
    ask command. Refer to the example displayed below:

  8. $ ask  5  6

    The sum of integers 5 and 6 are 11.

    Write your shell script below:
     
     
     
     
     
     
     
     
     
     
     

  9. Write a portable korn shell script called switch which allows two parameters (arguments) to be passed.
    If so, print the two parameters in reverse order. Refer to the example below:

  10. $ switch 1   2

    2  1

    Write your shell script below:
     
     
     
     
     
     
     
     
     

  11. Challenge:    Can you modify the shell scripts ask and switch to only allow two parameters (arguments) to be passed
                          to the shell script for processing? Hint: you need to know control-flow statements discussed in next
                          week's notes. Refer to examples below:

  12. $ add

    Usage: add [first integer] [second integer]

    $ add 5

    Usage: add [first integer] [second integer]

    $add  5  6  7

    Usage: add [first integer] [second integer]

    $ add  6  3

    The sum of integers 6 and 3 are 9.

    $switch

    Usage: add [argument 1] [argument 2]

    $switch  4

    Usage: add [argument 1] [argument 2]

    $switch  4  5  7

    Usage: add [argument 1] [argument 2]

    $switch  4  5

    5  4

--------------------------------------------------------------------------------------------------

*** End of Practice ***

  E-mail Murray Saul