“ Bash 脚本缺失”]’

我在 test.sh 文件中得到一个错误./test.sh: 第13行: [ : 丢失‘]’ 我尝试使用括号和其他选项,比如-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

我是个新手,所以如果有什么愚蠢的错误,请原谅我。

146561 次浏览

add a space before the close bracket

Change

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

into

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

note the space.

You're missing a space after "p1":

if [ -s "p1" ];

I got this error while trying to use the && operator inside single brackets like [ ... && ... ]. I had to switch to [[ ... && ... ]].

If you created your script on windows and want to run it on linux machine, and you're sure there is no mistake in your code, install dos2unix on linux machine and run dos2unix yourscript.sh. Then, run the script.