Rishikesh Rishikesh -4 years ago 75
Linux Question

bash script gives command not found error when using if statement

I am writing a bash script which will upload certain files on my AWS(Amazon web services) bucket when the script runs.I am trying to get a confirmation from the AWS by using "if" statement in the bash script and searching for the file name that i want to confirm has been copied to the bucket.This is the code for that-

if [`echo $(s3cmd ls s3://my-bucket/) | grep -c "file.txt" ` -gt 0 ]

echo "Success";

echo "fail";


When I run this script it gives me an error

./myBash.sh: line 21: [1: command not found

It says line 21 coz there is rest of my code too (irrelevant to this part of the code coz i am just running a few echos there)

Can anyone help with this? I searched around on the net but did not find any satisfactory results.

I am running debian linux (Jesse) on my Raspberry Pi

Answer Source

Add a space after the [:

if [ `echo $(s3cmd ls s3://my-bucket/) | grep -c "file.txt" ` -gt 0 ]

Your backticked command expand to 1, so right now you have

if [1 -gt 0 ]

which means the shell is trying to run [1 as a command, and no such command exists.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download