shell - Bash string length check- [: : integer expression expected -


i trying follow in systemd unit file. facing 2 problems here

  1. publicipaddress empty string , hence thought iplength should 0 , should not cause [: : integer expression expected error getting error.

  2. iplength seems empty every time, valid value of publicipaddress. missing ?

 

/bin/bash -c '\               env="/etc/environment"; \               touch $env; \               if [ $? -ne 0 ];  \                  echo "****** not modify $env .. exiting .."; \                  exit 1; \               fi; \               while true; \                  publicipaddress=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4); \                  iplength=${#publicipaddress}; \                  echo "************************************$iplength..."; \                  if [ "$iplength" -gt 0 ];  \                   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 

so turns out problem here script being embedded in systemd unit file.

it seems systemd understands ${var} expansions , expanding ${#publicipaddress} before script sees it.

escaping $ $ protect systemd doing this.

so use

iplength=$${#publicipaddress}; 

instead of

iplength=${#publicipaddress}; 

in script.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -