Basic Linux Shell
Basic Linux Shell
1.4 C Shell:
Next up was The C Shell (csh), so called because of the similar syntactical structures to the C
language. The UNIX man pages contain almost twice as much information for the C Shell as the pages
for the Bourne shell, leading most users to believe that it is twice as good. This is a shame because
there are several compromises within the C Shell which makes using the language for serious work
difficult (check the list of bugs at the end of the man pages!). True, there are so many functions
available within the C Shell that if one should fail another could be found. The point is do you really
want to spend your time finding all the alternative ways of doing the same thing just to keep yourself
out of trouble. The real reason why the C Shell is so popular is that it is usually selected as the default
login shell for most users. The features that guarantee its continued use in this arena are aliases, and
history lists. There are rumours however, that C Shell is destined to be phased out, with future UNIX
releases only supporting sh and ksh. Differences between csh and sh syntax will be highlighted where
appropriate.
The other thing you must do is cultivate an ability to read through these man pages and understand the
meaning behind the words. This is a trick that takes time, don’t think for a moment that what is written
in a man page is actually correct. It is simply someone else’s idea of how to explain what is supposed
to happen under known circumstances. Don’t forget that it’s rare for the code writer to be the
documentation writer too. This ability will put you in good stead for negotiating the most troublesome
passages. If it doesn’t make sense the first time, change your view and look at it from another angle.
Develop this skill well, it will pay dividends later.
I have included some syntactical information from my man pages within this document but I have kept
it to the minimum required and always used examples to explain each concept, something sadly lacking
in man pages generally. All examples will be repeated for each shell for comparison when appropriate,
although I now only use Bourne shell full time because of its portability (See 6.3 and 6.5).
#!/bin/sh
rm -f /tmp/listing.tmp > /dev/null 2>&1
touch /tmp/listing.tmp
ls -l [a-z]*.doc | sort > /tmp/listing.tmp
lpr -Ppostscript_1 /tmp/listing.tmp
rm -f /tmp/listing.tmp
Of course not all scripts are this simple but it does show that ordinary UNIX commands can be used
without any extra, fancy scripting constructs. If this script was executed any number of times the result
would be the same, a long listing of all the files starting with lower case letters and ending with a doc
extension from the current directory printed out on your local PostScript printer. Not very exciting.
Lets start to look at this in detail starting at the beginning.
However, it didn’t take long for someone to notice that this was extra typing 1 that could well be done
away with and hence the short hand first Line comment was born (See - 7.2.3.1 ).
Basically, all comments in shell scripts start with a hash sign (#). However, for the first line only,
UNIX reads past the hash to see what’s next. If it finds an exclamation point (!), or Bang!, as it’s
known, then what follows is taken as the path to the shell executable binary program. Not only that, but
all the command line arguments that the shell executable allows can also be stacked up on this line (See
- 7.1 ). With this feature it doesn’t matter what flavour of shell your environment is, you can execute a
script in any other flavour of shell, as if it was a real UNIX command. Lets look at the Bourne Shell
syntax rules more closely. It would be helpfull at this point to print out the sh man pages from your
system so that you can compare them with what I have here.
1
There is a general rule among UNIX systems programmers that states - “Any command which has
more than two letters in it’s name, is too difficult to spell!”
Advanced Shell Programming Page 3