Protection des caractères spéciaux Fonctions
"$var1 $var2" fonction_1 ()
( " ) % {
# commandes…
Ce document d’accompagnement du stage '$var1 $var2' }
« Programmation Shell et Langages de Scripts » ( * " " +# !
\$var fonction_1 valeur_1 valeur_2...
rappelle les points principaux à retenir
' $ & "" . "
concernant la programmation pour shells Bourne $1# $2/ "% $#!
' * " " +!
et Korn. local variable
% -
Structures de contrôle return valeur
Évaluation des expressions Boucles " 0 !
variable=valeur while cmd_1 ; do
commandes
done Motifs du shell
, ' & * +! *
tableau[rang]=valeur
until cmd_1 ; do " & ( ' *"1" +#
! ?
${variable} commandes
done " & ' #
" # \* \? \\
${tableau[rang]} , ' & $!
for variable in liste ; do ' *# ?# \#
" # [liste]
${variable-valeur} commandes
done ' l# i# s# t# e
" #
${variable=valeur} , ' " [b-e]
" ! ' b# c# d# e
#
break [^liste]
${variable?valeur}
! " % for# while until! 2 " & '
${#variable} continue
- % !
" # Redirections
${variable#motif} Tests
if cmd_1 ; then commande < fichier
"
cmd_2 #
$ # commande > fichier
${variable%motif} elif cmd_3 ; then
cmd_4 #
" commande >> fichier
$ # else
cmd_5 3 #
${variable##motif} ${variable%%motif}
fi commande 2> fichier
$ $ % ! #
~utilisateur/ $ !
# $ # $ ! commande 2>> fichier
" #
case expression in 3 #
ab{c,d,e}fg
motif_1 ) cmd_1 ;; commande 2>&1
abcfg abdfg abefg & - #
$(commande) motif_2 | motif_3 ) cmd_2 ;;
* ) cmd_3 ;; commande <<- ETIQUETTE
" # lignes à envoyer
$((expression)) esac
# $ # vers l’entrée standard
" " & ' de la commande
! # $ #
$ ! ETIQUETTE
" 0 !
Exécution des commandes test pwd
test condition !
Ligne shebang [ condition ]
#! /bin/sh
export
export variable
% ! , ! 7 ' " &
Pipeline " " & 5 " $ !
commande | commande | commande -eq / -/ env
3 -ne / / "
Liste de pipelines -lt *-le+/ * +-/
pipeline ; pipeline set
-gt *-ge+/ * +-/ set
*$ & +
7 ( 5 % " #
pipeline & pipeline
-n set options
*$ ' +
pipeline && pipeline -z ! "' 5
" ( 5 -a $ %
*$ +
pipeline || pipeline 8# 8# 9# : -u %
*$ + 7 5 -v "" $
Commandes composées -a $ # -x " $
{ liste de pipelines } -b & " % # unset
* " "" + -c & ' # unset variable
( liste de pipelines ) -d # !
* 4 + -f " # getopts
-g % 4;<6 # while getopts "ab:c" variable ; do
-G # case $variable in
Commandes internes essentielles a) echo "opt° –a";;
-h 0"% & #
echo -k % =0 # b) echo "opt° –b, arg. $OPTARG";;
echo arguments -N " ' # c) echo "opt° –c";;
" ! *) echo "opt° invalide"; exit 1;;
-O - #
-n " esac
-p % "" * +# done
-e ' & !
-r 1 #
read -s 4 #
read variables… shift $((OPTIND – 1))
-S = # echo "arguments restants :"
" "
-u % 4><6 # echo "$@"
* 5 % IFS+!
-w 1 # exit 0
6 ' % ) & ! #
-x 1 $ ! 0 ""
% REPLY! , $ !
" 5 ! " *?: '
exec
exec commande -ef / "1" 0 & & /# +# " OPTARG! >
-nt / " "" & / # # " "
" * + !
exec redirections -ot / " " & / " OPTIND!
& & ! 7 5 shift
source -t " shift n
source script cd " "" 5
. script cd repertoire $0 # $n+1 $1# $n+2 $2#
' ! # !
exit cd – #
exit valeur cd $ !
@@AAA!% ! @
" * + 0 !
bg 0 ! ) 1 2 col ( 1 1 )
$ application -b )* )
(Ctrl-Z) man col | col -b > [Link]
[1]+ Stopped application compress /
$ bg cp /
Ce document regroupe les principales commandes [1]+ application & -R * 1
susceptibles d’être employées régulièrement par les $
utilisateurs Unix, en rappelant leurs options les plus -p
bunzip2 3 .bz2 -d . " "
courantes. Pour avoir plus de détail sur une
bzip2 / cp fichier [Link]
commande particulière, on consultera le manuel Unix
(commande man). al cp -Rdp fichier_* /autre/repertoire
cal 5 2003 crontab ( crontab
cat / ) -l
propos -n -e crontab
apropos shell -v ) . " -r crontab
arch cat [Link] [Link] [Link] csh + /
at, atq, atrm ! [Link] csplit 3 *
cc / / -f *
-f " cd / * csplit -f prefixe fichier '/^$/' {*}
$ at now + 10 minutes < [Link] cd /usr/src/linux prefixe00 prefixe01 !
$ at 20:55 -f demarre_magnetoscope.sh cd – " *
awk # $% * cut + "
NF cd -b ) "
* -f "
FS
NR & ' chgrp / -d )
-R * 1 ls -l | cut -b 20-28
$1, $2...$NF
chgrp equipe_2 fichier " ) ! 9 1!1
ls -l | awk '{somme+= $5} END{print
somme}' chmod
-R * 1
chmod 644 texte ate .)
asename ( ) -d " !
* chmod 755 script + :
$ basename /usr/src/linux/signal.c date +"Date = %D, Heure = %X"
signal.c chmod u+s executable date -d 20030401 +"%A"
$ basename /usr/src/linux/signal.c .c * + 14#3 ; ;&< * ='
signal dd / " *
chown
$ if=
-R * 1
bash + , - chown user1.equipe1 fichier of=
batch * .) chsh / ! bs= !
batch << FIN count= !
-l
tri_des_enregistrements skip=
FIN -s "
chsh -s /bin/ksh seek=
bc / dd if=/dev/fd0 of=[Link] bs=1024
-l )" " )
cksum 5 6
clear 7 count=1440
$ pi=$(echo "a(1)*4" | bc -l) df > .)
$ echo $pi cmp /
-l " -k % 1
3.14159265358979323844
-s 8 * * -P 12
$
if cmp –s fichier1 fichier2; then ... df –k /tmp/sauvegarde
diff ? * fc ( ) 8 " * 8 d 4#3 ,#3
-i @ * 8 * FCEDIT -u 4#3
-b 8 fg 0 ) ! 8* 1 -g ,#3
-u * patch fgrep +. . grep -F -r
-r * 1 file . 8 if [ $(id -u) == 0 ]; then...
diff -u original copie > [Link] find 0 obs
dirname ) -name motif
$ dirname /usr/src/linux/signal.c join G
-regex expr join fichier_1 fichier_2 > fichier_3
/usr/src/linux -atime n ) .
$ ill 7 * !
-ctime n )
dos2unix / * 3 * 4
-mtime n )
-numéro "
du + " " -perm mode )
-l
-a " kill -9 30582
-size n & '
-s
-type t .
killall 7 * 2
-x 1 .) -i *
-print *
-k % 1 -l
-exec ...\{\} \; " E
killall xterm
cho \{\}
-e ) ) . " ksh + F
-ok ...\{\}\; exec *
-n * 1 1 find /tmp -ctime +30 -ok rm \{\} \; ess 1 1
echo "Message d'avertissement" >&2 find /home -name core -exec rm \{\} \; & * ! more '
echo -n "Votre choix :" fold / 8 ! lex , 8 .
echo -e "\r effectué : " $i "%" ftp ? ln /
echo –e "\007" fuser # -f
ed ( 1!1 -k * +#,F# -s . "
egrep +. . grep -E -i * *. ln -sf [Link] appli
emacs ( , -m .) logger H .)
A B1C $ Dxemacs fuser -k /mnt/cdrom login 0 .)
env * * rep ! logname 5
-i 3 * * lp 0 " 2 8
-E
env -d 8
-F :
* -n
env -i /bin/sh -i @
-v pr -l 66 appli.c | lp -d listing
* ls 8
expand / * -l
grep -i "MoTiF" fichiers_* -a E
-t -d
grep -v "absent" fichier
-i " -i 1 I
expand –i < [Link] > [Link] groups 8
gunzip 3 .gz -l
export > * 8 * -R * 1
export REP_APPLI=/usr/local/lib/appli/ gzip /
ls -al /home/usera
VERSION_APPLI=1.5 ead ls -lR /var/ftp/pub > /var/ftp/liste-
export VERSION_APPLI -c n [Link]
expr (* -n n )
expr 4 "*" 3 + 2 affiche 14 hostid 8
& ) ' @@$$$
hostname
alse ( iconv / * 8 ) *
until false; do ... iconv –f LATIN1 –t UTF8 < fichier
nohup 5 - rm 5
-f
$ nohup ~/bin/calcul & -i !
[1] 17300 -r $
$ exit rm -rf /home/usera/tmp
Ce document regroupe les principales commandes d ' ' rmdir ;
susceptibles d’être employées régulièrement par les rsh 5 <(
utilisateurs Unix, en rappelant leurs options les plus -c ( - , ssh.
courantes. Pour avoir plus de détail sur une -x
commande particulière, on consultera le manuel Unix cript 5
(commande man). asswd $a fic /
$ passwd sed > $
# passwd utilisateur -e "..."
patch ! # -f fic
ake -pn ( - -n '
$ patch –p1 ../new_version/patch_1
; ?
man pathchk 7 ' p
numéro ! -p - d
-a if pathchk "$rep/$fic" ; then... n #
-t " perl * " s
-k ! # apropos -w
sed -ne '1,/^$/p' < [Link]
man 1 c ping 8
man -k socket '$9 '
-c -
md5sum $ %&
sh ; :
-w
mkdir sleep
pr " ' sleep 14
-p -h texte ! '$9 ! ,2@ .
-m mode ' ( -l n sleep 3m
mkdir -p /var/lib/new-app/font/big -t $9 ,1 .
mkfifo )*)+ , - . printf ! sort 8 '
-m mode ' ( printf "%05d %4.2f" $x $y -b - -
mkfifo -m 666 /tmp/fifo_serveur ps ' -f / 0
mknod -ax ,:;&. -r
b c - ( -u ( ,:;&. split &
mknod /dev/hda1 b 3 1 -e ,;< 7. -l n
/ 0 102 -f ( ,;< 7. -b n
more , less. -w split -b 1440k gros_fichier disquette_
mv & ps -aux (BSD) ssh <(
-f ' ps -ef (SysV) ssh user@hostname
for i in *.JPG; do mv $i ${i%JPG}jpg; pwd ssh –l user hostname /usr/bin/commande
done
uota ! ' ! strings =
3"4 /
cp = <(
stty
ice 5 -a
renice % '
sane
-n valeur renice +20 14210
-echo ' (
nl 6 ' rev * ' , tac. -icanon min 0 time 1
-f a rlogin <( ( #
, ssh.
su 5 *& 4*& uniq D ! . ! (
- -u ' ! ! [Link]
sum ; A - - -d ' ! ! J - =
-c - ' K =
ac ( #'
^$ , .
tail ' unix2dos &
unzip & .zip * ! B
-num - ! [ ]
-f uptime 8 <(
[eéêèë]
tail -f /var/log/messages users 6 [0-9]
tar ' uudecode & .uu [[:upper:]]
-f fic ' uuencode - \i (
(
-c i>
-t ' Classes de caractères
7 E$F G ? gvim
-x ' alpha ( - !
-z ! 4 B , .
ait '
digit
$ ./commande &
-j ! -B , . xdigit
[1] 2927 ./commande
-v - $ wait 2927 alnum ( !
tar -czf [Link] /home/usera/* [1]+ Done ./commande lower
tar -xzf [Link] $ upper /
tcl * 8 , wish. wc 6 - ( ' blank ( -
tee whereis C - space (
-a / ' punct
...| tee hublot_1.log | ... which ' ( graph < - -
telnet <( who % ! print < - - -
, ssh. cntrl ( A
whoami *&
test 8< ' wish * 8 -- (! 8H
;< < [...] Éléments des expressions rationnelles étendues
if test –f $fichier ; then args |
time find . -name "*.c" | xargs grep "init()" Y|y
touch % ' ' acc 4 < < ! +
-t MMJJhhmm ! yes = ? B
-r fichier ' , Iy . [+-]?[[:digit:]]+
tr 8 ( yes | rm -r /var/old-backup/ { } -
tr ‘àâäçéèêïîôöùû’ ‘aaaceeeiioouu’|... ( )
cat
true C # zip Équivalences pour les expressions rationnelles
while true; do ...
simples
tty '
Expressions rationnelles \| |
name <(
\+ +
-m < \? ?
grep sed find –regex
-n 'A \{ \} {}
-r <( \( \) ()
grep –e awk perl
-s < ( '
-a
uncompress & .Z Éléments communs 00GGG 0
unexpand - \ (
, expand. prix=25\$