Shell Scripts
Here is the contents of a shell script called summary:
#!/bin/csh
if ($#argv != 1) then
echo "Wrong number of arguments"
else
if (-d $1) then
cd $1
echo "---- Start of directory $1"
foreach Name (*)
if (-d $Name) then
summary $Name
else
wc $Name # >> lst.sum
endif
end
echo "---- End of directory $1"
else
echo "Invalid directory name: $1"
endif
endif
Tasks:
- Explain what shell scripts are.
- Explain how the shell script can be executed. (Hint: there are 2 ways)
- Create a new directory under your cp1300 directory, called "test".
Inside "test" create two sub-directories "sub1" and "sub2". Place
some text files inside "test", "sub1", and "sub2".
- Change into ~/cp1300/test/ and make a copy of summary inside
"test" (You can make it executable if you wish).
- Once you get it working, analyse the shell script summary (try
and use it like this:
summary ./
), and describe what it
is doing.
Note: As it is written above, summary will not work properly.
What is causing the problem? And what can be done about it?
- Construct your own shell script, call it unsummary which takes
a single argument (a directory name) and proceeds to display the contents
of the lst.sum files that were created.
- Modify your unsummary script so that all of the lst.sum
files are removed instead of them being displayed.
Note: be VERY careful using the rm
command.
Because once something is removed, it is not trivial to restore it!
Don't try this question until you complete the previous version of
unsummary.