Shell
Shell
#variables
$# # number of arguments given
$0 # script name
$1, $2, … # first, second, … arguments
$* # list of all arguments
$? # return value of the last command
#structures
if command; then # command would be in general the test
instructions1 # command (see below)
elif command; then
instructions2
else
instructions3
fi
case $var in
pattern1) # pattern can be i.e.: yes|Yes|YES
instructions1 # or [Nn][Oo],…
;;
pattern2)
instructions2
;;
*)
instructions3
;;
esac
#useful commands
read myvar # puts stdin until \n in myvar
echo “chaine” # prints chaine to stdout (for errors use echo “chaine”
# 1>2&
cat file # echo file
`command` # value echoed by command ( ` is a backquote)
[ -args ] # you can use test –args instead
# -n chaine la chaine n’est pas vide
# -z chaine la chaine est vide
# chaine1 = chaine2 les 2 chaines sont égales
# chaine1 != chaine2 les 2 chaines sont différentes
#! not
# -a and
# -o or
# par exemple : [ -r $file –a ! -d $file ] est-ce que file est lisible et n’est pas un répertoire ?
# man test
#redirections
1>&2 # stdout to stderr
2> file # stderr to file
> file # stdout, stderr to file (overwrites)
>> file # stdout, stderr to file (appends)
< file # file to stdin
# mathematics
echo '2 + 4' | bc –l # compute 6 using bc
Exemple de programme
#!/bin/sh # interpreter
availFile=`getrid $*`
echo ‘’$availFile’’