Fidi Naj
Fidi Naj

Reputation: 113

if statement returning [[: not found

I'm writing a dinky little script and I need to do a check on values.

size is going to be a float, or number with decimal point for example like 28.0.

For some reason whenever I run, I get this error:

   while read -r line;
   do
           sample=$(echo $line | awk 'BEGIN{FS="/"}; {print $5}')
           size=$(aws s3 ls --summarize --human-readable $line | grep "Total Size:" | awk 'BEGIN{FS=" "}; {print $3}')
   #       aws s3 cp $line ./crams/
           if [[ $( echo "${size} < 20" | bc ) -eq 1 ]]; then
                   echo "${sample}\t${size}"
           fi
   done < $1

download-aws.sh: 6: download-aws.sh: [[: not found 

Any thoughts???

Also if you know of a better way to do the comparison of a float to a int... that'd be great!

Thanks

EDIT:

So I found a solution, I changed the if statement to:

if [ $( echo "${size} < 20" | bc ) -eq 1 ]; then

If anyone could point me in the direction of some awesome explanations of the logic behind the brackets etc... that'd be amazing!

Upvotes: 1

Views: 602

Answers (1)

paulsm4
paulsm4

Reputation: 121669

From "Bourne Shell built-ins" documentation:

https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html

test
[
test expr
Evaluate a conditional expression expr and return a status of 0 (true) or 1 (false). Each operator and operand must be a separate

argument. Expressions are composed of the primaries described below in Bash Conditional Expressions. test does not accept any options, nor does it accept and ignore an argument of -- as signifying the end of options.

When the [ form is used, the last argument to the command must be a ].

In other words,

  • [ is an alias for "test"

Q: Is there any chance you could assign an integer to "size"? So perhaps your test condition could simplify to something like:

if [ $size -lt 20 ]; then
 ...
fi

Upvotes: 1

Related Questions