2016-02-05 61 views
0
I am new to Shell Scripts and not aware of the syntax. 

我有我推到我的Android设备,然后用“busybox的DOS2UNIX的”转换成为一个shell脚本:在shell脚本中检查字符串错误是否为空?

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

在剧本我想检查一个字符串是否为空或不 ? 但我得到错误:'意外的操作符/操作数'。

下面是我的代码:

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]选项。但是两者都给出了错误。

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

回答

1

要检查一个空字符串,你需要使用-z。

实施例:

if [ -z "$str" ] ; 
+0

也是我认为声明 “pidString = $(PS | grep的$ {} PID)” 是不正确。你是否试图检查pid是否激活? –

1

要检查空字符串。

例子:

line="hello welcome" 
if [ -z "$line" ] ; then 
echo "String null" 
fi 
+0

工作。如此愚蠢的我,缺少双引号!谢谢您的帮助 – gambit

相关问题