UNIX: Exercise Sheet 8
-
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.
-
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".
-
Write a shell script which renames all .txt files
as .text files. The command basename might help you here.
-
Write a shell script called pidof which takes a
name as parameter and returns the PID(s) of processes with that name.
-
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.
-
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)