-1
I am new to Shell Scripts and not aware of the syntax.

我有一个要推送到我的 android 设备的 shell 脚本,然后使用 'busybox dos2unix' 对其进行转换:

adb shell busybox dos2unix /data/local/myShellScript.sh

在脚本中,我试图检查一个字符串是否为空?但我收到错误:'unexpected operator/operand'。

下面是我的代码:

echo "Getting a PID (Process ID) from a file" ;
pid=`cat  /sdcard/PIDofTask.txt` ;
echo "PID of task is : " $pid ;
echo "Checking if the pid exists, to verify the process is running" ;    
pidString=$(ps | grep ${pid}) ;
echo "PID string : " $pidString ;

if [ $pidString ] ;
then
    echo "pid String is not empty" ;
else 
    echo "pid String is empty" ;
fi ;

输出 :

Getting a PID (Process ID) from a file
PID of task is : 11571
Checking if the pid exists, to verify the process is running
PID string : root 11571 8082 4180 1828 poll_sched 7faa42dcdc S sh
/data/local/myShellScript.sh[2]: [: 11571: unexpected operator/operand
pid String is empty

我也尝试过 [ -n $pidString ] 和 [ -z $pidString ] 选项。但两者都给出了错误。

我在哪里做错了?真的很感谢帮助...

4

2 回答 2

2

检查空字符串。

例子:

line="hello welcome"
if [ -z "$line" ] ; then
echo "String null"
fi
于 2016-02-05T06:36:23.910 回答
1

要检查空字符串,您需要使用 -z。

例子:

if [ -z "$str" ] ;
于 2016-02-05T06:34:58.010 回答