21-31的shell实例脚本,这里粘贴的都是经过本人的实际实验环境调试实现的,仅供参考。
| |||||||||
Eg28、while循环实例- #!/bin/bash ## filename: /etc/cron.daily/monitor_disk_space.cron # set admin email so that you can get email ADMIN="xh_ry@163.com" # set alert level 90% is default ALERT=90 LANG=C df -PH|egrep -v '^Filesystem|tmpfs|cdrom'|awk '{ print $5 " " $1 }' | while read output; do usep=$(echo $output | awk '{ print $1 }' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then ( echo -n "Running out of space " echo -n \"$partition ($usep%)\" echo -n " on \"$(hostname)\" as on \"$(date)\"." )| mail -s "Alert: Almost out of disk space $usep" $ADMIN fi Done 这里直接执行了就会一直发送给xh_ry@163.com 的邮件了。
所以要中止,需要关闭linux或者killall 28eg.sh | |||||||||
Eg29、until实例-host-online #!/bin/bash ## filename: until-host_online_to_ssh.sh read -p "Enter IP Address:" ipadd echo $ipadd until ping -c 1 $ipadd &> /dev/null do sleep 60 done ssh $ipadd
| |||||||||
Eg30、 #!/bin/bash ## filename: until-user_online_to_write.sh username=$1 if [ $# -lt 1 ] ; then echo "Usage: `basename $0` <username> [<message>]" exit 1 fi if grep "^$username:" /etc/passwd > /dev/null ; then : else echo "$username is not a user on this system." exit 2 fi until who|grep "$username" > /dev/null ; do echo "$username is not logged on." sleep 6 done shift ; msg=$* [[ X"$msg" == "X" ]] && msg="Hello, $username" echo "$msg" | write $username 这个例子的情况不是很好,后续会更新。 | |||||||||
Eg31、while-until-for综合实例 #!/bin/bash ## filename: while-until-for_sum.sh # 使用当型循环求 sum(1..100) ((i=0,s=0)) # i=0 ; s=0 while ((i<100)) ; do ((i++,s+=i)) ; done echo sum\(1..100\)=$s # 使用直到型循环求 sum(1..100) ((i=0,s=0)) until ((i==100)) ; do ((i++,s+=i)) ; done echo sum\(1..100\)=$s # 使用C风格的 for 循环求 sum(1..100) for ((s=0,i=1;i<=100;s+=i,i++)) ; do : ; done echo sum\(1..100\)=$s
| |||||||||
-----------shell55例脚本分隔符---------------- |