Foreach en Bash
Publié le 25/05/2012 Bash
Pour traiter tous les éléments, ligne par ligne (depuis un ls ou en lisant un fichier par cat) :
for file in `ls -1`; do echo $file done
for ligne in `cat authorized_keys2`; do echo $ligne done
while read ligne; do echo "--$ligne--" done < /tmp/fichier
Avec un numéro incrémental :
switch="SW-XXX"
for nb in `seq 1 26`; do
echo "ln -s /usr/share/munin/plugins/snmp__if_err_ snmp_${switch}_if_err_$nb"
ln -s /usr/share/munin/plugins/snmp__if_err_ snmp_${switch}_if_err_$nb
echo "ln -s /usr/share/munin/plugins/snmp__if64cric_ snmp_${switch}_if64cric_$nb"
ln -s /usr/share/munin/plugins/snmp__if64cric_ snmp_${switch}_if64cric_$nb
done
En spécifiant la liste des éléments : (ATTENTION : PAS D'ESPACE ENTRE LES ELEMENTS !!)
for process in {"apache2","sshd","postfix","ntpd","dovecot","spamd","mysql","qpsmtpd"}; do
echo $process
done