Linux 的 shell script 中,遇到 unary operator expected 的解決方法

許聖泉 Michael Hsu
1 min readFeb 27, 2019

--

在寫 shell script 如果遇到判斷式,例如

#!/bin/bash 
app-running=$(ps -ax | grep "[a]pp-server" | awk '{print $1}')
if [ $app-running == "" ];
then
cd $GOPATH/src/github.com/app
./run-script.sh backend
fi

在寫 linux shell 時,遇到 if 這種判斷句出現:

restart-app.sh: 3: [: ==: unary operator expected

那是因為 == 左邊沒有抓到值,或是值為 null

有兩種解決方法:

if [ "$app-running" == "" ];

或是

if [[ $app-running == "" ]];

--

--

許聖泉 Michael Hsu
許聖泉 Michael Hsu

No responses yet