Académique Documents
Professionnel Documents
Culture Documents
Scripts shell
le 22 et les 25-26 février 2019
3. Introduction
3. Mon 1er script shell
4. Variables positionnelles
5. Tableau
6. Les codes de retour
7. Evaluations
7. Booléens et opérations
8. test
9. Tests améliorés
10. Tests arithmétiques améliorés
11. Compter
11. expr
12. Arithmétique des shells modernes
13. bc
14. Structures
14. if
15. case
16. while
17. until
18. for
19. select
20. Groupes
21. Debug
22. sed avancé
23. Exercices
23. Menu
24. Solution
25. Table de multiplication
26. Solution
27. Information sur un utilisateur
28. Solution
29. index.html
30. Solution
Permissions et lancement :
$ chmod 755 script.sh
$ ./script.sh
mon premier script shell
$# : Nombre d'argument
$1 : 1er argument
$2 : 2eme argument
...
$* et $@
set -- a b c
shift
$?
de 0 a 255
0 signi e succès, OK
Positionner le code retour avec :
return
exit
$ true; echo $?
0
$ false;echo $?
1
! [ -e file ]
[[ ! -e file ]]
[ -z "$VAL" ]
[[ -z $VAL ]]
Correspondance de chaînes
[[ $VAL = y* ]] # VAL commence par "y"
[[ $VAL =~ ^y ]] # VAL commence par "y"
Sous-shell
$ ( exit 4 ) || echo "En échec"
En échec
$ A=10
$ ( echo "A=$A"; A=20; echo "A=$A" )
A=10
A=20
$ echo "A=$A"
A=10
Option Effet
set - Arrête le script au premier code retour faux
e
set - Analyse les commandes sans les éxecuter
n
set - Af che les commandes (telles que dans le code)
v
set - Af che les commandes au fur et à mesure après évaluation
x
#!/bin/bash age)
question() { printf '%20s : ' "$1"; read $2; } affiche Nom "$nom"
affiche() { printf '%20s : %s\n' "$1" "$2"; } affiche Prenom "$prenom"
affiche Sexe "$sexe"
STEP=nom question Age age
until [[ $STEP == "done" ]];do if [[ $age =~ ^[0-9]+$ ]];then
clear STEP=fin;
echo else
if [[ -n $ERROR ]];then ERROR="'$age' n'st pas un age valide. Veuillez entrer
printf '\t\t\e[1;31m%s\e[0m\n' "$ERROR" fi;;
else echo; fin)
fi; affiche Nom "$nom"
echo;echo; affiche Prenom "$prenom"
ERROR="" affiche Sexe "$sexe"
case "$STEP" in affiche Age "$age"
nom) printf '\n\n\n\n'
question Nom nom; STEP=done;;
STEP=prenom;; esac
prenom) done
affiche Nom "$nom"
question Prenom prenom
STEP=sexe;;
sexe)
affiche Nom "$nom"
affiche Prenom "$prenom"
echo
affiche 1 homme
affiche 2 femme
question "Sexe" r
case "$r" in
1) sexe=homme
STEP=age;;
2) sexe=femme
STEP=age;;
*) ERROR="'$r' n'est pas une réponse valide. Répondre 1 o
esac;;
$ ./userinfo.sh root
utilisateur : root
UID : 0
groupe : root
groupes :
#!/bin/bash affiche() {
aide() { printf '%16s : %s\n' "$1" "$2"
cat <<END }
$0 <username>
Affiche des informations sur un utilisateur # Corps du code
END if [ $# -ne 1 ];then
} echo "$0 requiere un argument" >&2
aide
# fonctions sur /etc/paswd exit 1
user_exist() { fi
awk -v u="$1" -v f=1 -F: '$1==u{f=0}END{exit f}'</etc/passwd case "$1" in
} -h|--help)
passwd_field() { aide;
awk -v u="$1" -F: '$1==u{print $'$2'}'</etc/passwd exit 0;;
} esac
user_id() { if ! user_exist "$1";then
passwd_field "$1" 3 echo "$1 n'existe pas" >&2
} aide
user_group() { exit 2
passwd_field "$1" 4 fi
}
affiche "utilisateur" "$1"
# fonctions sur /etc/group affiche "UID" "$(user_id "$1")"
group_name() { affiche "groupe" "$(group_name $(user_group "$1") )"
awk -v i="$1" -F: '$3==i{print $1}'</etc/group affiche "groupes" "$(user_groups "$1")"
}
user_groups_list() {
awk -F: -v u="$1" '$4~u{print $1}'</etc/group
}
# fonctions d'affichage
user_groups() {
local A=$(user_groups_list "$1")
echo $A
}
#!/bin/bash {
cat <<END
list_files() { <html>
local D=${1:-'.'} <head><title>Index de ${CIBLE}</title></head>
ls -1 $D/*jpg $D/*png $D/*html 2>/dev/ <body><h1>Index de ${CIBLE}</h1><hr/>
} <table>
list_dir() { <tr><th>Type</th><th>Nom</th><th>Taille</th>
local D=${1:-'.'} <th>Date de modification</th></tr>
local i END
for i in $D/*;do for d in $(list_dir "$CIBLE");do
[ -d "$i" ] && echo $i echo "<tr><td>dir</td><td>$(basename "$
done echo "<td></td><td>$(mtime "$d")</td></
} done
mtime() { for f in $(list_files "$CIBLE");do
stat -c %y "$1"|sed 's/\..*//' cat <<END
} <tr><td>$(ext "$f")</td><td>$(basename "$f")</t
sz() { <td>$(sz "$f")</td><td>$(mtime "$f")</td></tr>
du -sh "$1"|awk '{print $1}' END
} done
ext() { cat <<END
echo ${1##*.} </table>
} </body></html>
END
CIBLE=${1:-"$PWD"} }>${CIBLE}/index.html