SHELL
#!/bin/sh # path to shell interpreter
#variables
$# # number of arguments given
$0 # script name
$1, $2, … # first, second, … arguments
$* # list of all arguments
$? # return value of the last command
$Myvar # value of a variable named MyVar
MyVar=value # assignment of value to MyVar, no spaces beside =
#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
for newvar in list; do # newvar takes each value in list
instructions
done
while command; do # i.e.: while [ $# -ge 1 ]; do
instructions # echo $1; shift
done # done
funcname () { # create a function whose name is funcname
instructions # warning: $1 would be first argument
}
#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
# int1 -eq int2 égalité
# -ne différents
# -ge >=
# -gt >
# -le <=
# -lt <
# -e name name exite
# -d répertoire
# -f fichier
# -h lien symbolique
# -r peut être lu
# -w peut être écrit
# -x peut être exécuté
#! 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
# prints all files in $*
getrid () { # function getrid
for file in $*; do
if [ -f $file ]; then
echo ‘’$file’’
fi
done
return 0 # return 0 if ok
}
availFile=`getrid $*`
echo ‘’$availFile’’