首页 > 解决方案 > 检查服务是否通过 bash 脚本运行

问题描述

我有一个脚本sh startAWS.sh。最重要的是,我检查了这个:

if [[ `ps -acx|grep postgres|wc -l` < 1 ]]; then
    echo "You need to start your Postgres service to run this script."

    ps -acx|grep postgres|wc -l

    exit 0
fi

当我跑步时,我得到了

⚡️  laravel  sh startAWS.sh                                                            
You need to start your Postgres service to run this script.                           
       6  

嗯……什么……‍♂️</p>

我的 Postgres 正在运行,不应执行该回显。

它表明6,并且 6 不小于 1,该代码不应该被执行。

有谁知道为什么我的 echo 打印出来了?

标签: linuxbashshellubuntuunix

解决方案


您需要执行数字比较,因此请在检查中使用 -lt 或 -gt。

比较 Bash 中的数字

if [[ `ps -acx|grep postgres|wc -l` -lt 1 ]];

推荐阅读