Chinaunix

标题: shell中的判断语句得到错误结果 [打印本页]

作者: lemoncoral    时间: 2014-06-30 12:52
标题: shell中的判断语句得到错误结果
本帖最后由 lemoncoral 于 2014-06-30 12:57 编辑

最近在写shell脚本,感觉基本的都会,但是一些细节就是处理不到位,可能还没对一些东西很好地完全理解,有一个问题是,写了一个脚本,监控rsync是否在正常运行

#!/bin/bash

[[ "" != "`pgrep rsync`" ]] && echo $?;exit

这个脚本在rsync不运行的时候,输出结果为0

但是我如果在命令行下直接运行语句:
> [[ "" != "`pgrep rsync`" ]]
>echo $?
> 1

会输出结果为1

因此,命令行下的才应该是正确的结果,因为rsync没有运行,但是脚本文件中的结果就不正确。百思不得其解!
作者: lemoncoral    时间: 2014-06-30 13:32
我SB了。。。。。。问题出在,这个脚本文件的名字中包含rsync这个字符串,然后启动这个脚本的时候,preg rsync当然怎么都不会为空。。。。
作者: 关阴月飞    时间: 2014-06-30 14:01
回复 1# lemoncoral


     [[ "" != "`pgrep rsync`" ]]  #这种写法好忧桑呀

建议这样写:

[ -z "`pgrep rsync`" ]  或者直接  [ "`pgrep rsync`" ]
作者: zl624867243    时间: 2014-06-30 15:03
我也在学习shell楼主学习shell是看的什么书?
作者: seesea2517    时间: 2014-06-30 15:34
感谢楼主分享,不说真是不知道原因~
作者: 用户名注册后不能更改    时间: 2014-06-30 17:47
回复 3# 关阴月飞

或者直接pgrep rsync && other command...
作者: lemoncoral    时间: 2014-07-03 17:03
回复 3# 关阴月飞

是的,感谢指正!

   
作者: lemoncoral    时间: 2014-07-03 17:05
回复 4# zl624867243

论坛里面的bash shell script那本教程就很好!


   




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