cucucool
cucucool

Reputation: 3887

Bash string length check- [: : integer expression expected

I am trying to do the follow in a systemd unit file. I am facing 2 problems here

  1. publicIPAddress could be empty string and hence I thought ipLength should be zero and this should not cause [: : integer expression expected error but I am getting the error.

  2. ipLength seems to be empty every time, even for valid value of publicIPAddress. Am I missing something ?

 

/bin/bash -c '\
              ENV="/etc/environment"; \
              touch $ENV; \
              if [ $? -ne 0 ]; then  \
                 echo "****** Could not modify $ENV .. Exiting .."; \
                 exit 1; \
              fi; \
              while true; do \
                 publicIPAddress=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4); \
                 ipLength=${#publicIPAddress}; \
                 echo "************************************$ipLength..."; \
                 if [ "$ipLength" -gt 0 ]; then  \
                  echo "************************************ HURAHHHHHH  .."; \
                  break; \
                 fi; \
                 sleep 1; \
              done'

Output of /bin/bash -xc

Apr 22 19:20:27 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: ++ curl -s http://169.254.169.254/latest/meta-data/public-ipv4
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: + publicIPAddress=10.1.2.3
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: + ipLength=
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: + echo '************************************...'
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: ************************************...
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: + '[' '' -gt 0 ']'
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: /bin/bash: line 0: [: : integer expression expected
Apr 22 19:20:28 coreosextc-cluster-ws-machine-crgdulh4xle4.novalocal bash[3640]: + sleep 1

Upvotes: 0

Views: 532

Answers (2)

Etan Reisner
Etan Reisner

Reputation: 81022

So it turns out that the problem here is that this script is being embedded in a systemd unit file.

It seems that systemd itself understands ${var} expansions and is expanding ${#publicIPAddress} before the script even sees it.

Escaping the $ with another $ will protect it from systemd doing this.

So use

ipLength=$${#publicIPAddress};

instead of

ipLength=${#publicIPAddress};

in the script.

Upvotes: 1

AKS
AKS

Reputation: 17336

Remove " (double quotes) around ipLength variable within your if statement.

Then it'll work --OR if you want to use quotes, then instead of using -eq, use == "0".

I would also recommend to use 2 big brackets instead of single i.e.

if [[ $ipLength -eq 0 ]]; then echo giga; else echo koba; fi 

...... OR ....

if [[ "$ipLength" == "0" ]]; then echo Hurra -- ipLength=0 ; else echo ipLength=$ipLength; fi

For ex:

[c1000@s10 giga]$ a=; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi
-bash: [: : integer expression expected
koba
[c1000@s10 giga]$ a=''; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi
-bash: [: : integer expression expected
koba
[c1000@s10 giga]$ a=""; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi
-bash: [: : integer expression expected
koba
[c1000@s10 giga]$

[c4000@s10 giga]$ a="1"; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi
koba
[c1000@s10 giga]$ a="1.2.3.4"; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi
-bash: [: 1.2.3.4: integer expression expected
koba
[c1000@s10 giga]$ a="1.2.3.4"; if [ "$a" -eq "0" ]; then echo giga; else echo koba; fi
-bash: [: 1.2.3.4: integer expression expected
koba
[c1000@s10 giga]$ a="1.2.3.4"; if [ "$a" == "0" ]; then echo giga; else echo koba; fi
koba
[c1000@s10 giga]$

Upvotes: 0

Related Questions