Chinaunix

标题: 新手 Shell 脚本 请教前辈--[已解决] [打印本页]

作者: ballatong    时间: 2009-02-23 09:59
标题: 新手 Shell 脚本 请教前辈--[已解决]
debian 4.0

如下shell : detect.sh

#! /bin/sh
FILESRV= ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl
FILESRV_status= grep -c "down" /pub/Filesrv-status
if [ $FILESRV -eq 0 ]
then
mail -s "Fileserver down" may@126.com
fi


问题1:执行的时候一定要用  /bin/sh detect.sh 才可以 能不能直接用文件名 detect.sh (我是root)
问题2;FILESRV_status= grep -c "down" /pub/Filesrv-status 语句直接执行是可以的 但是放在这个shell文件里执行就报如下错误:
: No such file or directory
问题3:直接运行还报如下错误:
detect.sh: line 8: syntax error: unexpected end of file

我是新手请前辈指点

[ 本帖最后由 ballatong 于 2009-2-25 09:15 编辑 ]
作者: liaosnet    时间: 2009-02-23 10:19
标题: 回复 #1 ballatong 的帖子

1.用户环境PATH加上:.
2.语法错误...
   FILESRV_status=`grep -c "down" /pub/Filesrv-status`
3.line 8指向哪?~~?不知道..
作者: ly5066113    时间: 2009-02-23 10:20
标题: 回复 #2 liaosnet 的帖子
我猜需要 dos2unix
作者: ywlscpl    时间: 2009-02-23 10:21
如果要用命令的输出作为一个变量的值,一般用以下形式
var=`cmd`
作者: ballatong    时间: 2009-02-23 10:45
谢谢楼上的回复
问题2根据liaosnet    (暗夜星空) 的方法解决了
原来那个不是单引号

现在
问题1
怎么把path加到用户环境

问题3
执行还是报如下错误:

detect.sh: line 8: syntax error: unexpected end of file
作者: leetaedong    时间: 2009-02-23 10:51
1.
  不用将.加入PATH,会有问题的.
  这样运行, ./your_shell.sh
2.
用tim 的方法
dos2unix 之后再运行看看.
作者: ywlscpl    时间: 2009-02-23 10:53
标题: 回复 #5 ballatong 的帖子
1、echo $PATH
    把你的脚本放到上述目录之一中就可以不加路径就运行了
2、cat -A file看一下
原帖由 ly5066113 于 2009-2-23 10:20 发表
我猜需要 dos2unix

作者: ballatong    时间: 2009-02-23 11:11
按照楼上前辈的方法
cat -A detect.sh
后发现文件末行后多了^M$ 我猜是多了回车
去掉后就好了

#! /bin/sh
FILESRV= ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl
FILESRV_status=`grep -c "down" /pub/Filesrv-status`
if [ $FILESRV -eq 0 ]
then
   mail -s "Fileserver down" may@126.com
fi


但是新的问题有出来了,现在运行提示:




detect2.sh: line 4: [: -eq: unary operator expected
作者: liaosnet    时间: 2009-02-23 11:12
原帖由 ballatong 于 2009-2-23 11:11 发表
按照楼上前辈的方法
cat -A detect.sh
后发现文件末行后多了^M$ 我猜是多了回车
去掉后就好了

#! /bin/sh
FILESRV= ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl
FILESRV_status=`grep -c ...


if [ "$FILESRV" -eq 0 ]


作者: 我是DBA    时间: 2009-02-23 11:14
看来得好好学习,还是tim厉害,一眼就看出问题所在
作者: leetaedong    时间: 2009-02-23 11:16
哎,写个脚本也不容易啊.   
作者: ballatong    时间: 2009-02-23 11:20
请教前辈:liaosnet    (暗夜星空)



是什么意思?

还有if [ "$FILESRV" -eq 0 ] 加了 双引号后还是报告同样的错吴
作者: ballatong    时间: 2009-02-23 11:23
请教前辈:




是什么意思?
还有if [ "$FILESRV" -eq 0 ] 我加了双引号还是报同样的错误
作者: leetaedong    时间: 2009-02-23 11:26
在if 前,看看 $FILESRV 是什么结果.
echo $FILESRV
作者: ywlscpl    时间: 2009-02-23 11:32
原帖由 ballatong 于 2009-2-23 11:11 发表
按照楼上前辈的方法
cat -A detect.sh
后发现文件末行后多了^M$ 我猜是多了回车
去掉后就好了

#! /bin/sh
FILESRV= ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl
FILESRV_status=`grep -c ...


FILESRV变量没取到数值型的值

[ 本帖最后由 ywlscpl 于 2009-2-23 11:36 编辑 ]
作者: ballatong    时间: 2009-02-23 11:32
在if 前插入
echo $FILESRV

结果
0
detect2.sh: line 5: [: FILESRV: integer expression expected


$FILESRV的结果是对的,因为那个ip是不同的,需要返回0
作者: ywlscpl    时间: 2009-02-23 11:37
原帖由 ballatong 于 2009-2-23 11:32 发表
在if 前插入
echo $FILESRV

结果
0
detect2.sh: line 5: [: FILESRV: integer expression expected


$FILESRV的结果是对的,因为那个ip是不同的,需要返回0


别光看显示,判断前加这句看看
echo "##"$FILESRV"##"
作者: cooljean2008    时间: 2009-02-23 11:39

if [ "X"$FILESRV="X0" ]
看看
作者: guorui913    时间: 2009-02-23 11:39
把你的代码以及错误信息打全,以便大家指正及学习
作者: ywlscpl    时间: 2009-02-23 11:45
  1. [root@Mylinux ~]# v="aaa"
  2. [root@Mylinux ~]# [ $v -eq 0 ]&&echo xxx
  3. -bash: [: aaa: integer expression expected
复制代码

变量v不是数值型
作者: ballatong    时间: 2009-02-23 11:53

if [ "X"$FILESRV="X0" ]
代替原来的
if [ $FILESRV -eq 0 ]
可以了

现在整理一下:
#! /bin/sh
FILESRV= ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl
FILESRV_status=`grep -c "down" /pub/Filesrv-status`
if [ "X"$FILESRV="X0" ]
then
   mail -s "Fileserver down" may@126.com
fi

执行后发现还有个问题:
就是需要手工输入ctrl +D 再 输入 Enter 退出 mail命令 才会发送email到我的信箱

有没有办法能不用手工输入ctrl+D 和 Enter 就自动发送

我的想法是要把这代码放到 crontab去,每隔一定时间ping一下

谢谢这么多前辈热情帮忙。
作者: merlin852    时间: 2009-02-23 14:35
mail -s "Fileserver down" may@126.com  </dev/null
作者: 我是DBA    时间: 2009-02-23 14:40
mail -s "Fileserver down" may@126.com

因为你没有读入邮件正文,所以不能直接发送
作者: ballatong    时间: 2009-02-23 14:45
邮件正文内容可以不写吗?
我想受到标题就可以了
作者: ly5066113    时间: 2009-02-23 14:47
标题: 回复 #21 ballatong 的帖子
if [ "X"$FILESRV="X0" ]

这个判断永远为真
作者: 我是DBA    时间: 2009-02-23 14:50
标题: 回复 #24 ballatong 的帖子
不写可以啊,
你按上面的上面的上面的上面的下面
mail -s "Fileserver down" may@126.com  </dev/null
这样试一下看行不行?
作者: ballatong    时间: 2009-02-23 14:51
果然 if [ "X"$FILESRV="X0" ] 这个判断永远为真
不能这样用
作者: 我是DBA    时间: 2009-02-23 14:54
改成这样呢?
[ "X"$FILESRV == "X0" ]
作者: liaosnet    时间: 2009-02-23 14:59
标题: 回复 #28 我是DBA 的帖子
String1 = String2
            Returns a True exit value if the String1 and String2 variables are identical.

对于string是用=....
作者: liaosnet    时间: 2009-02-23 15:00
原帖由 我是DBA 于 2009-2-23 14:54 发表
改成这样呢?
[ "X"$FILESRV == "X0" ]


需是要注意 = 两边都要有空格..不能就是一体了...

[ "X"$FILESRV="X0" ] 这样的话就是测试 "X"$FILESRV="X0" 是否存在了.......
由于有"X"这样的,,所以,恒为真...
作者: 我是DBA    时间: 2009-02-23 15:03
标题: 回复 #30 liaosnet 的帖子
不错学习了,下个月开始学mysql了,老兄mysql学得如何?
作者: liaosnet    时间: 2009-02-23 15:07
标题: 回复 #31 我是DBA 的帖子
没玩过~~不懂~ 能力有限..学不了这么多~~
作者: 我是DBA    时间: 2009-02-23 15:17
标题: 回复 #32 liaosnet 的帖子
我也是被逼的。
没办法
最近公司开始搞游戏服务器
都用mysql
作者: ballatong    时间: 2009-02-23 15:19
用了
mail -s "Fileserver down" may@126.com  </dev/null

后 ctrl +D 和 enter是不用输入了
但是 mail收不到了好像没有发出来
作者: 我是DBA    时间: 2009-02-23 15:23
标题: 回复 #34 ballatong 的帖子
你手工操作的时候,可以收到吗?有没有收到过
作者: ballatong    时间: 2009-02-23 15:26
手工操作可以收到,但是现在我把它改回原来的也收不到了
但用前面的副本在另一目录下的没有加过 </dev/null 的 仍然可以收到
作者: ballatong    时间: 2009-02-23 15:29
如果一定要加邮件正文也可以加,其实只要能自动发送就可以了,但怎么能实现呢?
作者: liaosnet    时间: 2009-02-23 15:29
原帖由 我是DBA 于 2009-2-23 15:17 发表
我也是被逼的。
没办法
最近公司开始搞游戏服务器
都用mysql


游戏服务器?~~?你们公司很会赚钱嘛~~
作者: 我是DBA    时间: 2009-02-23 15:30
标题: 回复 #37 ballatong 的帖子
mail -s "Fileserver down" may@126.com  <  /root/install.log
/root/install.log改成你的文件路径+文件名就可以。
作者: 我是DBA    时间: 2009-02-23 15:31
标题: 回复 #38 liaosnet 的帖子
关键是把我搞得好乱,没办法专心做一项研究,一会这,一会那的。
作者: ballatong    时间: 2009-02-23 15:44
我是DBA    (我在学习,我要进步)
按照你上面的方法
我把代码改成如下的

#! /bin/sh
FILESRV= ping -c1 10.198.12.18 |grep 10.198.12.18 |grep -c ttl
FILESRV_status=`grep -c "down" /pub/Filesrv-status`
if [ "X"$FILESRV == "X0" ]
then
   mail -s "Fileserver down" may@126.com < /pub/test
echo 'Fileserver donw' > /pub/Filesrv-status
fi

现在可以了

现在我想把它放到crontab里 5分钟执行一次
用0-59/5 * * * * root  detect.sh
不知道可以不可以
作者: 我是DBA    时间: 2009-02-23 16:06
标题: 回复 #41 ballatong 的帖子
5分钟执行一次的话
*/5 * * * * 加上detect.sh的绝对路径
作者: ballatong    时间: 2009-02-23 16:12
重新测试了一下
发现  if [ "X"$FILESRV == "X0" ] 这句还是有问题
用了以后邮件发不出来
作者: cooljean2008    时间: 2009-02-23 16:36
应该是
if [ "X"$FILESRV = "X0" ]

对不起,前面的“=”两边忘记加空格了。
作者: ballatong    时间: 2009-02-23 16:57
谢谢大家,这个shell终于可以运行了

现在问题是如何把它放到crontab中 每5分钟一次

我是用
crontab -e
加入
*/5 * * * * root  /pub/detect.sh

但好像不起作用
作者: liaosnet    时间: 2009-02-23 17:07
原帖由 ballatong 于 2009-2-23 16:57 发表
谢谢大家,这个shell终于可以运行了

现在问题是如何把它放到crontab中 每5分钟一次

我是用
crontab -e
加入
*/5 * * * * root  /pub/detect.sh

但好像不起作用


有些系统不支持*/5这样的..你可能非得
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /pub/detect.sh >/dev/null 2>/dev/null
这样的格式.
作者: cooljean2008    时间: 2009-02-23 17:15
baidu 结果:
由于unix版本不一样,所以部分语法有差别,例如在hp unix aix 中设定间隔执行如果采用*/n 方式将出现语法错误,在这类unix中 ,间隔执行只能以列举方式,详请见例子。
在hp unix,中,每20分钟执行一次,表示为:0,20,40 * * * * 而不能采用*/n方式,否则出现语法错误

还有,有时候cron里要加上环境变量才可以正常运行
作者: haimming    时间: 2009-02-23 17:59
这贴这么火啊
作者: ballatong    时间: 2009-02-23 19:44
我的系统是debian 4.0,谢谢大家支持
明天用0,5,10,15,20,25,30,35,40,45,50,55 * * * * /pub/detect.sh >/dev/null 2>/dev/null这个试试看
作者: fedora_core_7    时间: 2009-02-24 10:18
FILESRV= ping -c1 10.198.12.18 |grep 10.198.12.18 |grep -c ttl


这样写的含义是带着 FILESRV=""这个环境变量来运行 ping 命令,你将永远得不到期望的结果,你应该这样写:
FILESRV=`ping -c1 10.198.12.18 |grep 10.198.12.18 |grep -c ttl`
作者: ballatong    时间: 2009-02-24 10:42
谢谢楼上,我把代码改成这样了
#! /bin/sh
FILESRV=`ping -c1 10.198.112.18 |grep 10.198.112.18 |grep -c ttl`
FILESRV_status=`grep -c "down" /pub/Filesrv-status`
if [ $FILESRV -eq 0 ]
then
   mail -s "Fileserver down" may@126.com < /pub/test
echo 'Fileserver down' > /pub/Filesrv-status
fi

单独运行已经可以了  `   到底是什么东西 ? 不是单引号啊, 我是把大家的这个符号复制过去才可以的。

现在问题是 放到crontab里还是不能直接运行 我的系统是debian 4.0

不管是
crontab -e
改成
0-59/5 * * * * root  /pub/detect.sh
*/5 * * * * root  /pub/detect.sh
还是这样
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /pub/detect.sh
都不可以,请大家看看改怎么改呢?
作者: liaosnet    时间: 2009-02-24 10:47
标题: 回复 #51 ballatong 的帖子
可能的环境问题吧~~
把crontab 放到root用户的任务里吧.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * su - root -c "/pub/detect.sh &">/dev/null 2>/dev/null

BTW: '与`是区别不是一般般的..一个是单引号...另一个是点引号(数字1的左边那个)
作者: ballatong    时间: 2009-02-24 10:56
如何把crontab 放到root用户的任务里呢?
放到/etc/crontab这个文件里面吗?
作者: ballatong    时间: 2009-02-24 12:39
问题解决了

不知道什么原因,重启之后就好了

谢谢大家的支持。
作者: dashuaisun    时间: 2009-02-25 15:11

作者: BeckW    时间: 2009-02-26 18:23
学习了~~:wink:
作者: liudew    时间: 2009-02-26 23:18
我也正在学习shell!
'` 发现两个不同.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2