开发者

Bash - why doesn't this condition do what I think it should do?

This is basically my first script in bash so I'm probably missing something really simple.

I don't understand why "$(pgrep firefox)" appears to return something even if firefox is not running.

The script should always keep a firefox instance running.

#!/bin/bash
while true;
do
        if [ -z "$(pgrep 开发者_开发知识库firefox)" ]
                then
                        echo "firefox not running. Starting now..."
                        firefox
        fi
done

The really weird thing is if I type this at the bash command prompt it works as expected

if [ -z "$(pgrep firefox)" ];  then echo "not running"; fi


Have you tried printing out the result of $(pgrep firefox) to see what is being returned?

By the way, you don't need to compare strings here. pgrep returns true if a process is found and false otherwise, so you can do this:

if ! pgrep firefox
then
    echo firefox not running
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜