| |
Shell and Shell Script Skills
- You are in your home directory. Give the shortest shell pattern that would match all the
files named "UNTITLED" in all the home directories on your machine. (How many of
these files are there currently?) You may assume that all home directories are
sub-directories under the same directory (though that may not always be true for all Unix
systems).
- What is the contents of file "hoopla" after the following sequence of
commands?
echo first >hoopla
var=">hoopla"
echo second $var
What appears on your screen after the following sequence of commands? var="hello ; echo goodbye"
echo $var
What appears on your screen after the following sequence of commands? var=".*"
echo First "$var" and then $var
Give your Unix command lines to perform the following functions. Where file
processing is required, use Unix commands, do not simply read the file into an editor to
answer the questions. Your answers must be made up of a Unix pipeline of commands
that could read from a file, or from standard input, and write to standard output.
For example, to perform the sort operation on a file that has leading numbers on each
line, the command would be "sort -n", and it could be used any of these ways:
$ sort -n filename # sort uses the filename
$ sort -n <filename # sort reads from stdin
$ cat filename | sort -n # sort also reads from stdin
Your answers must be able to use standad input in a similar way.
- Display the first 5 lines of a file, numbered with line numbers.
- Display the lines of a file in reverse order, last to first.
(Hint: sort can sort numbers backward as well as forward, using the right
command line option.)
- Display everything after the first 5 lines of a file.
That is, start the display of the file on line 6 and go to the end.
(Hint: "man tail")
- Display only lines 10-15 in a file. (Try doing this both with and without using sed.)
What happens if the file only has 12 lines? What happens if the file only has 3 lines?
- Display from the 20th line from the end of a file to the 15th line from the end of a
file.
For example, if the file had 27 lines, your command pipeline would show lines 7 (27-20=7)
to 12 (27-15=12). If the file had 127 lines, your command pipeline would show lines 107 to
112. What happens if the file only has 12 lines? What happens if the file only has 3
lines?
- The "-s" option to ls gives file sizes (in
disk blocks, usually either 512 or 1024 bytes).
Write an executable shell script named "biggest" that shows the
five biggest files in the current directory. Make sure your shell script starts with this
as the very first line: #!/bin/sh
Put the shell script into your "bin" directory. Hand in a
copy of your script.
- Look up how to use the Unix "crontab" command. You'll
find it in the back of the textbook as well as in the manual system online. Learn the
format of the crontab file from the book, or (if you are brave) from the actual man page
under "crontab Entry Format".
Create a file containing a crontab entry that executes the following command pipeline (or
one similar to it) at noon each day (use your own userid as the write
destination):
echo "Time for Lunch" | write abcd0001
You must modify the above line to include the five time fields, as given in the crontab
entry format, before you load the file into the cron system.
When you have added the five time fields to the beginning of the above line, use the
correct options to the crontab command to load your file into the cron system. Use
"crontab -l" to make sure it loaded correctly.
You might want to test that your command works by using different start times for the
command, first. (For example, set a start time about 1 minute from now and wait to see if
the message appears.)
You can add other crontab commands at other times, if you like. Experiment!
Unix system processes use the cron system to run tasks at specific times and on specific
days.
If you want to edit your crontab file directly using the "crontab -e"
command, rather than loading it from a file, you will be using the Desktop Text Editor
unless you set up some other editor using the $EDITOR or $VISUAL environment variables
(see text, p. 421/422).
Look up how to use the basic features of the Unix "find"
command. You'll find it in the back of the textbook as well as in the manual system
online. Look at the Examples; they may be easier to understand at first than the
explanation text.
Create and run the following shell script. #!/bin/sh
# -Ian! idallen@freenet.carleton.ca
find $HOME -mtime -1 -print
Describe what this script is finding. Replace the last argument "-print"
with "-ls" and re-run the script, to see more detail on the
files and directories found by this command. (You may need to pipe the output of
this script into "more" to paginate it on your terminal.) Change the
number from "-1" to "1". Re-run the script. Change the
number to "+1". Re-run the script. What is the difference between
"-1", "1", and "+1"?
Describe what the following Unix command line would do. Note the use of
back-quotes for command substitution around the find command and its
arguments: rm `find $HOME/.dt/Trash -mtime +14 -print`
If the find command found a file name with a blank in it, would the
above command do the right thing? What if the find command returned
a directory name - would the above command function correctly?
|