DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with the UNIX system shell

Shell programming exercises


2-1.
Create a shell program called time from the following command line:
banner `date | cut -c12-19`

2-2.
Write a shell program that gives only the date in a banner display. Be careful not to give your program the same name as a UNIX system command.

2-3.
Write a shell program that sends a note to several people on your system.

2-4.
Redirect the date command without the time into a file.

2-5.
Echo the phrase ``Dear colleague'' in the same file as the previous exercise, without erasing the date.

2-6.
Using the above exercises, write a shell program that sends a memo to the same people on your system mentioned in Exercise 2-3. Include in your memo:

2-7.
How can you read variables into the mv.file program?

2-8.
Use a for loop to move a list of files in the current directory to another directory. How can you move all your files to another directory?

2-9.
How can you change the program search, so that it searches through several files?

Hint:

for file
in $*


2-10.
Set the stty options for your environment.

2-11.
Change your prompt to the word Hello.

2-12.
Check the settings of the variables $HOME, $TERM, and $PATH in your environment.

Answers


2-1.


$ cat time
banner `date | cut -c12-19`
$
$ chmod u+x time


2-2.
   $ cat mydate
   banner `date | cut -c1-10`
   $

2-3.
   $ cat tofriends
   echo Type in the name of the file containing the note.
   read note
   mail janice marylou bryan < $note
   $
Or, if you used parameters for the logins (instead of the logins themselves) your program may have looked like this:
   $ cat tofriends
   echo Type in the name of the file containing the note.
   read note
   mail $* < $note
   $

2-4.


date | cut -c1-10 > file1


2-5.


echo Dear colleague >> file1


2-6.


$ cat send.memo
date | cut -c1-10 > memo1
echo Dear colleague >> memo1
cat memo >> memo1
echo A memo from M. L. Kelly >> memo1
mail janice marylou bryan < memo1
$


2-7.


$ cat mv.file
echo type in the directory path
read path
echo type in filenames, end with CTRL-d
while
read file
do
mv $file $path/$file
done
echo all done
$


2-8.


$ cat mv.file
echo Please type in directory path
read path
for file in $*
do
mv $file $path/$file
done
$

The command line for moving all files in the current directory is:

   $ mv.file *

2-9.
See the hint provided with exercise 2-9.

$ cat search
for file
in $*
do
if grep $word $file >/dev/null
then echo $word is in $file
else echo $word is NOT in $file
fi
done
$


2-10.
Add the following lines to your .profile:
   stty -tabs
   stty erase
   stty echoe

2-11.
Add the following command lines to your .profile:
   PS1=Hello
   export PS1

2-12.
Enter the following commands to check the values of the HOME, TERM, and PATH variables in your home environment:

Next topic: Summary of shell command language
Previous topic: Using shell variables

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004