2013-04-13 76 views
50

我在文件test.sh中收到錯誤./test.sh:第13行:[:missing`]' 我嘗試使用括號和其他選項,如-a或通過檢查文件p1的大小,但是錯誤總是存在的,並且else語句總是被執行,而不管給定的輸入如何。在第13行,但它沒有幫助。Bash腳本缺失']'

test.sh

#!/bin/bash 
echo "Enter app name" 
read y 
$y & 
top -b -n 1 > topLog.log 
#-w checks for the whole word not and sub string from that word 
grep -w "$y" topLog.log > p1 
#-s option checks if the file p1 is present or not 
if [ -s "p1"]; #line 13 
then 
    echo "Successful " 
else 
    echo "Unsuccessful" 
fi 
rm p1 

我是新來砸向scripting.So如果有任何愚蠢的錯誤,請原諒我。

回答

130

變化

if [ -s "p1"]; #line 13 

前添加一個空格到

if [ -s "p1" ]; #line 13 

注意空格。

+2

+ 1你們真的很快!!! – Kent

+0

因爲有一次我打你:-) –

+0

我剛剛發現OP的ID很快! :D – Kent

5

閉括號

7

"p1"後缺少空間:

if [ -s "p1" ]; 
7

我試圖使用&&操作單括號內像[ ... && ... ]得到這個錯誤。我不得不切換到[[ ... && ... ]]

+0

謝謝,這是我的問題,這解決了它 –

+1

' [&] [']也會工作 –

+0

是的[[]]是一種bashism,所以在其他地方不起作用。 – BTRUE