LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 815|回复: 0

帮忙看看问题出在哪儿

[复制链接]
发表于 2006-8-30 23:57:29 | 显示全部楼层 |阅读模式
如下一个shell脚本:
#!/bin/bash

#write a shell script to send some messages to others.
#Then mesg shut, to stop him/her to write back mesg.

if [ $# -eq 0 ] ; then        #script require argument
   echo "Usage: $0 username..."
   exit 1
fi

msgsend(){        #messages send function
while :                #repeat sending messages in time designated
do
for i in $@        #send messages to all users in the argument list
do
    write $i < msg.txt;
done
ERR=$(grep "messages disabled" out.txt);
if [ X$ERR != X ] ; then        #user refuse to accept messages
    rm out.txt;
    mesg n;
    killall send;        #exit the while loop
        killall sleep;
fi
sleep 5;        #send messages every hour
done
}

mesg y;                #enable message to send messages
if [ -s out.txt ] ; then
   rm out.txt;        #remove old output file
fi
exec 5>out.txt;        #output redirect
msgsend $@ 2>&5 &

#end script

该脚本目的是实现不断向用户发送消息,直到用户将消息关闭为止。
当用户执行 mesg n 之后,write会返回错误:×××has messages disabled
所以只要判断返回值出现"messages disabled"就行了。
我将返回的信息写入out.txt文件中,然后用grep来判断是否出现这样的字眼来判断用户是否关闭mesg。但是当用户mesg为y时,脚本运行正常,而mesg为n时,out.txt一直不断的打印"×××has mesages disabled"的字样,但就是停不下来。好像ERR=×××下面的if语句根本就没执行,这是怎么回事儿?我看了好久都不知道问题出在哪儿。谁能帮忙看看错在哪儿?先谢了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表