Bash脚本缺少']'

qui*_*ck- 62 bash scripting

我在文件test.sh中收到错误./test.sh:line 13:[:missing`]'我尝试使用括号和其他选项(如-a)或通过检查文件p1的大小但错误是总是在那里,而且无论给出什么输入,都会执行else语句.我甚至尝试删除; 在第13行,但它没有帮助.

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
Run Code Online (Sandbox Code Playgroud)

我是bash脚本的新手.如果有任何愚蠢的错误,请原谅.

Fre*_*ihl 170

更改

if [ -s "p1"];  #line 13
Run Code Online (Sandbox Code Playgroud)

if [ -s "p1" ];  #line 13
Run Code Online (Sandbox Code Playgroud)

注意空间.


Car*_*l G 22

尝试&&在单个括号内使用运算符时出现此错误[ ... && ... ].我不得不切换到[[ ... && ... ]].

  • `[...] && [...]`也可以 (3认同)

Elm*_*ise 8

你之后错过了一个空格"p1":

if [ -s "p1" ];
Run Code Online (Sandbox Code Playgroud)


Sam*_*uby 6

在close括号前添加一个空格