加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 使用空白/空grep的Do语句返回?

发布时间:2020-12-15 22:50:00 所属栏目:安全 来源:网络整理
导读:这是我的foobar.sh的代码: !#/bin/bashwhile [ 1 ]do pid=`ps -ef | grep "mylittleprogram" | grep -v grep | awk ' {print $2}'` echo $pid if [ "$pid"="" ] then echo "Process has ended lets get this show on the road..." exit else echo "Process
这是我的foobar.sh的代码:

!#/bin/bash
while [ 1 ]
do
    pid=`ps -ef | grep "mylittleprogram" | grep -v grep | awk ' {print $2}'`
    echo $pid
    if [ "$pid"="" ]
    then
            echo "Process has ended lets get this show on the road..."
            exit
    else
            echo "Process has not ended yet"
    fi
    sleep 6
done

我基本上运行一个infinate循环,一旦受监视的进程结束就会执行命令X但是当我的脚本循环时我最终收到以下消息:

./foobar.sh: line 7: [: missing `]'
Process has not ended yet

有没有办法让脚本接受零反馈将触发我的’Then’语句并执行命令X,因为它不喜欢当前的方法.

解决方法

代替

if [ "$pid"="" ]

请试试

if [ "$pid" = "" ]

空白围绕=非常重要.

你也可以试试

if [ -z "$pid" ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读