UNIX: Exercise Sheet 8

  1. Use your favourite UNIX editor to create the simple shell script given in Section 8.4 of the notes. Run it, and see how the contents of the script relates to the output.
  2. Extend the script so that it generates a random secret number between 1 and 100 (c.f. Exercise Sheet 3, Question 16) and then keeps asking the user to guess the secret number until they guess correctly. The script should give the user hints such as "I'm sorry your guess is too low" or "I'm sorry your guess is too high".
  3. Write a shell script which renames all .txt files as .text files. The command basename might help you here.
  4. Write a shell script called pidof which takes a name as parameter and returns the PID(s) of processes with that name.
  5. Shell scripts can also include functions. Functions are declared as:

    function funcname() {
          statements
    }

    and invoked as funcname param1 param2... The parameters passed to the function are accessible inside the function through the variables $1, $2, etc. Now add a usage() function to your pidof script which prints usage instructions. Call usage() if the wrong number of parameters is passed to the script.
  6. Modify your .bash_profile script so that your PATH includes the current directory (.) and so that your environment variable EDITOR is set to emacs or vi (or whatever else you prefer to use). Run the modified script using source .bash_profile and check that the changes you made have been applied to the current shell (type set).

(Contents)