SHELL #!/bin/sh
# path to shell interpreter
#variables $# $0 $1, $2, … $* $?
# number of arguments given # script name # first, second, … arguments # list of all arguments # return value of the last command
$Myvar MyVar=value
# value of a variable named MyVar # assignment of value to MyVar, no spaces beside =
#structures if command; then instructions1 elif command; then instructions2 else instructions3 fi case $var in pattern1) instructions1 ;; pattern2) instructions2 ;; *) instructions3 ;; esac
# command would be in general the test # command (see below)
# pattern can be i.e.: yes|Yes|YES # or [Nn][Oo],…
for newvar in list; do instructions done
# newvar takes each value in list
while command; do instructions done
# i.e.: while [ $# -ge 1 ]; do # echo $1; shift # done
funcname () { instructions }
# create a function whose name is funcname # warning: $1 would be first argument
#useful commands read myvar echo “chaine” cat file `command`
# puts stdin until \n in myvar # prints chaine to stdout (for errors use echo “chaine” # 1>2& # echo file # value echoed by command ( ` is a backquote)
[ -args ] # -n chaine # -z chaine # chaine1 = chaine2 # chaine1 != chaine2
# you can use test –args instead la chaine n’est pas vide la chaine est vide les 2 chaines sont égales les 2 chaines sont différentes
# int1 # # # # #
égalité différents >= > <= <
-eq int2 -ne -ge -gt -le -lt
# -e name # -d # -f # -h # -r # -w # -x
name exite répertoire fichier lien symbolique peut être lu peut être écrit peut être exécuté
#! # -a # -o
not and or
# par exemple : # man test
[ -r $file –a ! -d $file ]
est-ce que file est lisible et n’est pas un répertoire ?
#redirections 1>&2 2> file > file >> file < file
# stdout to stderr # stderr to file # stdout, stderr to file (overwrites) # stdout, stderr to file (appends) # file to stdin
# mathematics echo '2 + 4' | bc –l
# compute 6 using bc
Exemple de programme #!/bin/sh
# interpreter
# prints all files in $* getrid () { for file in $*; do if [ -f $file ]; then echo ‘’$file’’ fi done return 0 } availFile=`getrid $*` echo ‘’$availFile’’
# function getrid
# return 0 if ok