-1

I'm new to bash scripting and am having trouble trying to get this to work

local attempt=1
local success=false
while [[ "$attempt" -le "$retryAttempt" && "$success" -eq "false"]]; do
    if ! [[ some condition here]]; 
    then
        echo true
        return            
    fi
done

I'm getting an error on the while condition saying line 10: [: missing ]

I cannot figure out what is wrong here, suggestions anyone?

tmp dev
  • 8,043
  • 16
  • 53
  • 108

1 Answers1

1

Short: insert spaces around braces

Long: I wouldn't consider myself a bash pro (no idea what local is), but in a minimal syntax example (which is actually an infinity loop).

while [ $b -le $a] ; do 
  b=$a
done

gives me the same error. Changing the first line to

while [ $b -le $a ] ; do 

works.

Depending on the complexity of the script, you might want to consider python or perl. In my opinion bash syntax can be a real pain in the a**. Especially when passing arguments with spaces through more than one level.

tarbos
  • 241
  • 1
  • 2
  • 6
  • Basically you are right, but your example is misleading: While the `]` is simply supposed to be an argument to the command `[` (`[` checks that its last argument is a lone `]`). From the viewpoint of grammar, `[` is not different than, say, `ls` or `echo`., `[[` .. `]]` however is a _syntactic_ shell construct, and hence bash designers could have decided that a space is not necessary. For instance, they made the space optional before a `))`, which is also just shell syntax. Alas, they decided that `]]`, although delimiting the context introduced by `[[`, must be prepended by a space. – user1934428 Sep 01 '21 at 10:08