Blog Post

Bash: “command not found” in IF statement

,

This blog post is about a situation where writing an IF THEN ELSE statement with variables fails with something like

./scalePREPROD.sh: line 26: [[1: command not found

This was in a script called scalePREPROD that scaled app services if they were not a certain value – so I am checking to see IF they are a value of 15 and if they are not then scale up to 15.

TL;DR

You need a space in the IF statement when variables are used.

The code looked something like this:

for i in "${appServiceArray[@]}"; do
appcapacity=<bash query to check app services>
if
[[$appcapacity -ne $scaleAPPWorkersUp]];
then
<stuff to do>
else
<echo something back that we're all good>
fi
done;

So in the example above – I wanted to see if for my array of 6 app services ${appServiceArray[@]} any were not equal to a value of $scaleAPPWorkersUp.

To fix it I just needed to put a space so:

[[$appcapacity -ne $scaleAPPWorkersUp]];

becomes

[[ $appcapacity -ne $scaleAPPWorkersUp ]];

Pretty easy fix.

Yip.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating