用for循环语句的时候,如果不带参数,则表示从命令行取参数,如
#!/bin/sh
for params
do
echo "You supplied $params as a command line option"
# echo $params
done
#for params等同于for params in "$@" ,取命令行参数
运行 为 $forparam2.sh myfile1 myfile2
You supplied myfile1 as a command line option
You supplied myfile2 as a command line option
用命令行做参数查找指定文件
#!/bin/sh
for loop
do
find / -name $loop -print
done
运行$forfind passwd myfile
until循环
$pg until_who
#!/bin/sh
IS_ROOT=`who | grep root`
until [ "$IS_ROOT" ]
do
sleep $
done
echo "Watch it.roots in " | mail simon
这段脚本不断的搜寻who命令中的用户root,变量IS-ROOT保存grep命令结果,并向用户simon发送邮件,通知他用户root已经登录。
while循环
设有一文本文件
$more whileread
Louise Conrad:Accounts:ACC8987
Peter James:Payroll:PR489
Fred Terms:Customer:CUS012
James Lenod:Accounts:ACC887
Frank Pavely:Payroll:PR489
用while循环读取每行
more whileread.sh
#!/bin/sh
while read LINE
do
echo $LINE
done < names.txt