Un script peut-il s'appeler récursivement ? En fait, oui.
Exemple 33.8. Un script (inutile) qui s'appelle récursivement
#!/bin/bash
# recurse.sh
# Un script peut-il s'appeler récursivement ?
# Oui, mais est-ce d'une utilité quelconque ?
# Voir le script suivant.
ECHELLE=10
VALMAX=9
i=$RANDOM
let "i %= $ECHELLE" # Génère un nombre aléatoire entre 0 et $VALMAX.
if [ "$i" -lt "$VALMAX" ]
then
echo "i = $i"
./$0 # Le script lance récursivement une nouvelle instance de lui-même.
fi # Chaque fil du script fait de même jusqu'à ce que la valeur
#+ générée $i soit égale à $VALMAX.
# Utiliser une boucle "while" au lieu d'un test "if/then" pose des problèmes.
# Expliquez pourquoi.
exit 0
# Note :
# -----
# Ce script doit avoir le droit d'exécution pour fonctionner correctement.
# C'est le cas même s'il est appelé par une commande "sh".
# Expliquez pourquoi.
Exemple 33.9. Un script (utile) qui s'appelle récursivement
#!/bin/bash
# pb.sh : carnet de téléphones.
# Écrit par Rick Boivie et utilisé avec sa permission.
# Modifications par l'auteur du guide ABS
MINARGS=1 # Le script a besoin d'au moins un argument.
FICHIERDONNEES=./carnet_telephone
# Un fichier de données du répertoire courant, nommé
#+ "carnet_telephone", doit exister.
NOMPROG=$0
E_SANSARGS=70 # Erreur lorsque sans arguments.
if [ $# -lt $MINARGS ]; then
echo "Usage : "$NOMPROG" donnees-a-rechercher"
exit $E_SANSARGS
fi
if [ $# -eq $MINARGS ]; then
grep $1 "$FICHIERDONNEES"
# 'grep' affiche un message d'erreur si $FICHIERDONNEES n'existe pas.
else
( shift; "$NOMPROG" $* ) | grep $1
# Le script s'appelle récursivement.
fi
exit 0 # Le script sort ici.
# On peut mettre des commentaires sans '#' et des données après
#+ ce point.
# ------------------------------------------------------------------------
# Exemple d'un carnet d'adresses :
John Doe 1555 Main St., Baltimore, MD 21228 (410) 222-3333
Mary Moe 9899 Jones Blvd., Warren, NH 03787 (603) 898-3232
Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
Zoe Zenobia 4481 N. Baker St., San Francisco, SF 94338 (415) 501-1631
# ------------------------------------------------------------------------
$bash pb.sh Roe
Richard Roe 856 E. 7th St., New York, NY 10009 (212) 333-4567
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
$bash pb.sh Roe Sam
Sam Roe 956 E. 8th St., New York, NY 10009 (212) 444-5678
# Lorsqu'au moins un argument est passé au script, celui-ci n'affiche *que*
#+ le(s) ligne(s) contenant tous les arguments.
Exemple 33.10. Un autre script (utile) qui s'appelle récursivement
#!/bin/bash # usrmnt.sh, written by Anthony Richardson # Used with permission. # usage: usrmnt.sh # description: mount device, invoking user must be listed in the # MNTUSERS group in the /etc/sudoers file. # ---------------------------------------------------------- # This is a usermount script that reruns itself using sudo. # A user with the proper permissions only has to type # usermount /dev/fd0 /mnt/floppy # instead of # sudo usermount /dev/fd0 /mnt/floppy # I use this same technique for all of my #+ sudo scripts, because I find it convenient. # ---------------------------------------------------------- # If SUDO_COMMAND variable is not set we are not being run through #+ sudo, so rerun ourselves. Pass the user's real and group id . . . if [ -z "$SUDO_COMMAND" ] then mntusr=$(id -u) grpusr=$(id -g) sudo $0 $* exit 0 fi # We will only get here if we are being run by sudo. /bin/mount $* -o uid=$mntusr,gid=$grpusr exit 0 # Additional notes (from the author of this script): # ------------------------------------------------- # 1) Linux allows the "users" option in the /etc/fstab # file so that any user can mount removable media. # But, on a server, I like to allow only a few # individuals access to removable media. # I find using sudo gives me more control. # 2) I also find sudo to be more convenient than # accomplishing this task through groups. # 3) This method gives anyone with proper permissions # root access to the mount command, so be careful # about who you allow access. # You can get finer control over which access can be mounted # by using this same technique in separate mntfloppy, mntcdrom, # and mntsamba scripts.
Trop de niveaux de récursivité peut surcharger la pile du script, causant une erreur de segmentation (segfault).