Shell Skills & Shell Scripts
- Can a file and directory have the same name?
Why, or why not?
- Count how many Unix manual pages contain the word "file" in their
descriptions.
- What is a safe way to find out what files a shell pattern matches?
- What do these shell file patterns match?
~
*
.*
.??*
../*
./*
What is the difference, if any, between these Unix pathnames?
Could they refer to different files?
.profile
./.profile
././././.profile
If "data" is a subdirectory of the current directory, then what are the
differences, if any, among the following commands?
$ ls data/..
$ ls .
$ ls
What single Unix commands would you use to:
| change to your home directory |
| display the list of files in the current directory |
| display "my name is fred" onto the screen |
| put the file "tmp.dat" in the current directory down into the directory
"data" underneath your home directory, leaving a copy behind in your current
directory? (Both the current directory and the "data" subdirectory each have a
separate copy of the file.) |
| put the file "tmp.dat" in the current directory down into the directory
"data" underneath your home directory, without leaving a copy behind in your
current directory? (After the command, the file will only exist in the "data"
subdirectory.) |
What will the following Unix commands do?
(Don't execute a Unix command if you aren't sure what it is going to do!)
rmdir ~
cat /etc/passwd
ls ../../fred/doc/tmp
diff .profile .profile
sort /etc/passwd
sort /etc/passwd >happy
One of your users has created a file called "-tmp", with a leading dash.
(The command "echo hi >-tmp" will do it.) They now
want to get rid of
this file. Why might the user have difficulty removing this file? (Try
it yourself.) How would you remove the file?
Sometimes the tabs on your terminal aren't set correctly, causing
text to move off the right edge of the screen and the screen to look
badly formatted. Despite its name, the Unix "reset" command may be
a cause of this. It happens like this:
$ reset
$ who
ian console Oct 1 08:31 (
:0)
ian pts/7 Oct 1 08:38 (
)
ian pts/5 Oct 1 08:31 (
:0.0)
ian pts/2 Sep 27 00:32 (
sun4-4a:2.0)
ian pts/6 Oct 1 08:31 (
:0.0)
The file /usr/lib/tabset/vt100 contains some mostly unprintable control characters
that, when displayed on your terminal, will reset the tabs correctly on your terminal.
$ cat /usr/lib/tabset/vt100
$ who
ian console Oct 1 08:31 (:0)
ian pts/7 Oct 1 08:38 ( )
ian pts/5 Oct 1 08:31 (:0.0)
ian pts/2 Sep 27 00:32 (sun4-4a:2.0)
ian pts/6 Oct 1 08:31 (:0.0)
Following the example at the start of Chapter 10 in the text,
write an executable shell script file named "tabset" that displays the
contents of this tab fixing file. Copy and modify the script "whocat"
developed in class as a starting point. (Do not change the contents of
whocat.)
For best results, a shell script file must start with a first line
saying: #!/bin/sh (see p.285/286)
The line must start in the first column; no blanks allowed.
You must turn on execute permissions on the file to run it.
Put your new "tabset" shell script in a directory named "bin" under
your home directory. (You may have to create "bin" first.)
Add your "bin" directory to the beginning of your search path by
modifying the PATH variable in your shell (p.293). Do not put a
colon
(':') as the last (or first) character in your PATH. It is a security
risk to have the trailing colon, since it means that files in your
current directory may be found and executed as commands.
Make sure that typing "tabset" at the shell prompt causes your new
"tabset" command to execute from within your "bin" directory.
Modify your .profile file to add your "bin" directory to your PATH
for
all your logins. Telnet to your machine, login (which causes your new
.profile to run), and make sure that typing "tabset" at the shell
prompt causes your new "tabset" command to execute.
The "cat" command has an option to turn control characters (normally
invisible) into printable escape sequences. Using this option, you can see printable
representations of the unprintable characters in the file /usr/lib/tabset/vt100.
What is the option letter that does this?
When you display the file with cat using this option, does the file
still reset the tabs on your terminal? How can you know?
Sorting a file will place lines that are identical one after the other. (They sort to
the same place.) The "uniq" command removes duplicate lines from a file, if they
are one after the other. The "wc" command counts lines. Write an executable
shell script called "uniqwc" (unique word count) that uses the
"sort", "uniq", and "wc" commands to count the number of unique
executable lines read from standard input. You would use your shell script like this:
$ cat .profile | ./uniqwc
22 87 560
$ ./uniqwc <.profile
22 87 560
(Your output may differ, depending on the exact contents of your file
".profile".)
For example, given the following input file:
a
b
a
b
c
b
c
the output of your shell script should be: 3 3 6
(The first number 3 correctly indicates the input has three unique lines.)
|