bash - 带有空白/空 grep 的 Do 语句返回时?

标签 bash shell while-loop if-statement

这是我的 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

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

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

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

最佳答案

代替

if [ "$pid"="" ]

请试试

if [ "$pid" = "" ]

= 周围的空格很重要。

你也可以试试

if [ -z "$pid" ]

关于bash - 带有空白/空 grep 的 Do 语句返回时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7922854/

相关文章:

linux - 删除“ID : string in bash

php - 检索Mysql数据并按特定顺序打印

linux - 编写多个 sed 命令的更简洁的方法?

bash - 如果命令失败,为什么 `if` 语句的退出代码不是失败?

python - 运行我的 watch 应用程序之外的应用程序

javascript - 如何访问shell脚本文件中的 Node JS(javascript)变量

linux - linux 下 make 上的 tab 补全错误

macos - AppleScript Runner 退出状态传回 shell 脚本

c - C 中的 for/while 循环内存清除

继续迭代/重复 while 循环,直到按下 enter(或任意键)