LinuxSir.cn,穿越时空的Linuxsir!

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

【问】如何定时发送消息?

[复制链接]
发表于 2006-6-3 13:36:24 | 显示全部楼层 |阅读模式
Shell 脚本编程问题:
1. 如果该终端是服务器端,则考虑实现一个Shell脚本,往各个用户终端定时重复发送一条系统消息,该消息存于文件msg.txt中。
2. 如果是客户端,则考虑实现如下一个脚本:
   1)根据脚本指定的参数列表(用户名列表),向每个用户定时发送一则通知,该通知存于文件msg.txt中。
   2)根据指定的定时时间间隔一直向用户发送通知,直到该用户响应(即发送回复消息)或者屏蔽消息接收功能为止。
我自己是有试着写了一下,可是始终无法成功地实现上述功能。
刚开始学Shell脚本编程,有好多问题都不懂,请大家帮忙想一想,先谢了。
 楼主| 发表于 2006-6-3 20:39:41 | 显示全部楼层

我写的脚本的问题

对于客户端,我写了如下一个脚本,可是在for循环下面的if语句中出现错误。运行是可以运行,但是会打印出这样一些错误消息:./send: line 19: [: too many arguments

脚本代码如下:

#!/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;
    break;        #exit the while loop
fi
sleep 3600;        #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

请帮忙看看for循环下面紧跟着的那个if语句错在哪儿?我实在看不出来。
回复 支持 反对

使用道具 举报

发表于 2006-6-5 09:54:17 | 显示全部楼层
你每句后面都跟分号干嘛?冒充C语言阿?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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