[bash] Bash scripting missing ']'

I am getting an error ./test.sh: line 13: [: missing `]' in the file test.sh I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by removing the ; in line 13 but it didn't help.

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

I am new to bash scripting.So if there is any silly mistake please excuse me.

This question is related to bash scripting

The answer is


You're missing a space after "p1":

if [ -s "p1" ];

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.


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


add a space before the close bracket