Copyright(c) by Galaxy CD – http://www.galaxycd.com
55
do
command1
command2
command3
…………………
done
Vôùi variable coù theå gaùn ñöôïc caùc giaù trò val1, val2… thöïc hieän caùc leänh command1,
command2, …
Thí duï: WRITE laø moät shell_script gôûi thoâng baùo tôùi 3 ngöôøi duøng user1, user2, user3:
$
cat WRITE
for
i in user1 user2 user3
do
write $I < message_file
done
b) Caáu truùc :
for do done
Caáu truùc naøy cho pheùp thöïc hieän moät chuoãi leänh nhö nhau vôùi caùc ñoái ($1 $2 …) cuûa
shell_script ñöôïc goïi.
for
variable
do
command1
command2
command3
…………………
done
Vôùi variable coù theå gaùn ñöôïc caùc ñoái cuûa shell_scipt thöïc hieän caùc leänh command1,
command2, …
Thí duï:
Shell_script copy sao cheùp caùc file trong danh saùch ñoái vaøo danh muïc /users/user8 vaø
ñoåi nhoùm thaønh nhoùm student, ñoåi ngöôøi sôû höõu thaønh user8.
$
cat copy
for i
do if [-f $i]
then cp $i /users/user8
chgrp student /users/user8/$i
chown user8 /users/user8/$i
fi
done
$
ls
-l
total 10
-rw-------
1 phil animator 56 May 31 14:14:22 file1
-rw-------
1 phil animator 22 May 31 15:14:22 file2
$
copy file1 file2 toto