How to write a shell scriptabitur
Introduction
A shell is a command line interpretor. It takes commands and executes them. As such, it implements a programming language. The Bourne shell is ud to create shell scripts -- ie. programs that are interpreted/executed by the shell. You can write shell scripts with the C-shell; however, this is not covered here.
Creating a Script
in other wordsSuppo you often type the command
find . -name file -print
and you'd rather type a simple command, say
sfind file
Create a shell script
% cd ~/bin
answer的过去式
% emacs sfind
% page sfind
find . -name $1 -print
% chmod a+x sfind
% rehash
% cd /usr/local/bin
% sfind tcsh
清明节 英语./shells/tcsh
Obrvations
This quick example is far from adequate but some obrvations:
1. Shell scripts are simple text files created with an editor.
2. Shell scripts are marked as executeable
%chmod a+x sfind
3. Should be located in your arch path and ~/bin should be in your arch path.
4. You likely need to rehash if you're a Csh (tcsh) ur (but not again when you login).
5. Arguments are pasd from the command line and referenced. For example, as $1.
#!/bin/sh
All Bourne Shell scripts should begin with the quence
care是什么意思
#!/bin/sh
From the man page for exec(2):
"On the first line of an interpreter script, following the "#!", is the name of a program which should be
ud to interpret the contents of the file. For instance, if the first line contains "#! /bin/sh", then the con- tents of the file are executed as a shell script."
You can get away without this, but you shouldn't. All good scripts state the interpretor explicitly. Long ago there was just one (the Bourne Shell) but the days there are many interpretors -- Csh, Ksh, Bash, and others.
Comments
Comments are any text beginning with the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line.
Search Path
误解英语All shell scripts should include a arch path specifica- tion:
PATH=/usr/ucb:/usr/bin:/bin; export PATH
珠海小区A PATH specification is recommended -- often times a script will fail for some people becau they have a different or incomplete arch path.
The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command.
Argument Checking
A good shell script should verify that the arguments sup- plied (if any) are correct.
if [ $# -ne 3 ]; then
echo 1>&2 Usage: $0 19 Oct 91
exit 127
fi
This script requires three arguments and gripes accordingly.
Exit status
All Unix utilities should return an exit status.
# is the year out of range for me?
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of range
hopefullyexit 127
fi
<
# All done, exit ok
exit 0
A non-zero exit status indicates an error condition of some sort while a zero exit status indicates things worked as expected.
On BSD systems there's been an attempt to categorize some of the more common exit status codes. See
/usr/include/syxits.h.
Using exit status
Exit codes are important for tho who u your code. Many constructs test on the exit status of a command. The conditional construct is:
if command; then
command
fi
For example,
if tty -s; then
echo Enter text end with \^D
fi
Your code should be written with the expectation that others will u it. Making sure you return a meaningful exit status will help.
卓育英才
Stdin, Stdout, Stderr
Standard input, output, and error are file descriptors 0, 1, and 2. Each has a particular role and should be ud accordingly:
# is the year out of range for me?
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of my range
exit 127
circumcision
fi
<
# ok, you have the number of days since Jan 1, ...
ca `expr $days % 7` in
0)
echo Mon;;
1)
echo Tue;;
<
Error messages should appear on stderr not on stdout! Output should appear on stdout. As for input/output dialogue:
# give the fellow a chance to quit