A set of commands may be placed in a file (using an editor).
The set may be run by starting a shell with the file as argument.
For example, if the file is called test_script
and contains
ls rm tmp*then typing
bash test_scriptwill run the two commands sequentially. Such a file is often called a shell script or batch file.
If the shell script is made into an executable file by
chmod a+x test_scriptThen typing its name at the prompt will also run the script.
If a command line finishes with an ampersand `&' then it will be run asynchronously. Without this, it will run synchronously.
The shell command interpreter reads the user input and then starts a
process using the exec
system call.
and then
A GUI command shell will start processes by the user clicking on icons.
It starts processes by using the exec
or
system
system calls.
PATH
(type echo $PATH
to see this).
The value of PATH
may be set by you or in a shell script
kill
command
kill
process_id
signal
fg
will continue it in the foreground
bg
will continue it in the background
command > filesaves the output from the command into the file
command < filetakes the input to the command from the file
command >> fileappends the output to the file
command | commandmakes the output from the first command into the input to the second. This is called a pipeline, and may have any number of commands in it
ls > tmpsaves ls output in tmp
ls -l >> tmp
who | wc -lcounts the number of people logged in
man cp | lprsends the man page for cp to the printer
tr '\040\010' '\012\012' < file | sort | uniqproduces an alphabetic listing of words in `file' (assuming words separated by spaces).
From now on we only discuss the Bourne shell, Korn shell, zsh and bash. The csh and tcsh have different syntax.
Assignment to a shell variable is by
variable=valueNB: there are no spaces either side of ``=''
To use the value of a variable, prefix it with a ``$''
x1=fred x2="a string" x3=4 x4="$x1 $x2" echo $x1 $x2 $x3 $x4There are some occassions when you need the value of the variable to be immediately followed by text. The variable name may be enclosed in curly brackets
x=fred echo ${x}dy
file_list=`ls` echo "The files are $file_list" echo "The last file is" last_file=`echo $file_list | sed 's/.* //'` echo $last_file
x=2 y=`expr $x + 2`bash also allows you to do arithmetic using the let command
x=3 let y=$x+4
ls; echo "files listed"
for vbl [in values] do commands... doneNB: the words ``for'', ``do'', ``done'' must be the first words on the line. The commands can be any set of commands.
for i in a b c do echo $i done
cd ~/.. for i in os* do echo "A student is $i" doneA common case is to loop through all the command line arguments (positional parameters).
for i in $* do echo "an arg was $i" doneA shorthand for this is to omit the
in $*
:
for i do echo "an arg was $i" done
Every command succeeds or fails. eg.
rm
. may succeed at removing a file, or
may fail to remove another file because of permission problems. An exit code
holds this value.
The exit code is not visible. The exit code of the last command is stored in the shell variable ``?''. A value of zero stands for success, anything else for failure.
cd /tmp echo > tmp$$ rm tmp$$ echo "Exit code of ok rm is $?" rm tmp$$ echo "Exit code of failed rm is $?"Generally, the exit code is not documented anywhere. The commands ``test'' and ``expr'' are the best documented, because they are often used in Boolean expressions.
while commands do commands doneThe list of commands in the Boolean part is executed each time round the loop. Usually this list is just one command, but it may be a pipeline. The exit code of the last command in this list is used as the Boolean value. If it is True (exit code zero), the commands in the body are executed.
i=1 while test $i -le 20 do echo $i let i=$i+1 doneI/O can be redirected from an entire while loop:
ls | while read file do echo "A file is $file" done
if commands then commands fiwith variations such as
if commands then commands else commands fi
if [ $# -lt 2 ] then echo "Usage: $0 files..." exit 1 fi
case value in pattern) commands;; ... pattern) commands;; esacThe value is some string or integer. The patterns the use shell globbing mechanism of * and ?. The matches of value to pattern take place strictly top-to-bottom, and the first match is used.
sed s/.*//the shell will read the * and attempt to glob it. sed will then get a list of files and complain. A quoting mechanism is used by the shell to stop it interpreting characters it shouldn't.
Interpretation of a single character is turned off by prefixing it with a backslash `\' as in
echo the amount is \$20To echo a `\' itself, use \\.
Enclosing something in single quotes '...' turns off all interpretation, including interpretation of $, * and \.
Enclosing something in double quotes "..." allows $variable substitution but no other.
echo The cost is $20 echo The cost is \$20 echo "The cost is $20" echo 'The cost is $20'
Single quotes in this sed command will turn off variable substitution. No quotes cause an error.
x="two words" echo "two words are too many" | sed "s/$x//"
sum=0 while read x do let sum=$sum+$x done echo "The sum was $sum"
script=$0 command=$1 for f in * do if [ -d $f ] then cd $f $script $command cd .. else $command $f fi doneProgram Development using Unix Home