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

bik*_*ben 5 bash shell if-statement while-loop

这是我的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
Run Code Online (Sandbox Code Playgroud)

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

./foobar.sh: line 7: [: missing `]'
Process has not ended yet
Run Code Online (Sandbox Code Playgroud)

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

A.H*_*.H. 11

代替

if [ "$pid"="" ]
Run Code Online (Sandbox Code Playgroud)

请试试

if [ "$pid" = "" ]
Run Code Online (Sandbox Code Playgroud)

空白是=很重要的.

你也可以试试

if [ -z "$pid" ]
Run Code Online (Sandbox Code Playgroud)