2015-04-22 59 views
0

我想在systemd單元文件中執行下列操作。我在這裏面臨的兩個問題Bash字符串長度檢查 - [::整數表達式預計

  1. publicIPAddress可能是空字符串,因此我認爲ipLength應該是零,這應該不會造成[:整數表達式預期的錯誤,但我得到的錯誤。

  2. ipLength似乎每次都是空的,即使對於publicIPAddress的有效值。我錯過了什麼嗎?

 

/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' 

/斌/慶典的輸出-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 
+2

你從'echo'行看到'$ ipLength'包含什麼?或者是「ipLength似乎每次都是空的」,表示你看到'*** ...'? –

+0

添加'-x'來執行這個操作,你會得到什麼? –

+0

************************************ ...是我得到的「回聲」* *********************************** $ ipLength ...「; \」命令 – KarthikJ

回答

1

所以事實證明,這裏的問題是這個腳本被嵌入到systemd單元文件中。

看來,系統本身可以理解${var}擴展,並在腳本甚至看到它之前擴展${#publicIPAddress}

轉義$與另一個$將保護它免受systemd這樣做。

的腳本,以便使用

ipLength=$${#publicIPAddress}; 

代替

ipLength=${#publicIPAddress}; 

0

刪除(雙引號)圍繞ipLength變量的if語句。

T母雞它會工作 - 或者如果你想使用報價,那麼而不是使用-eq,使用==「0」

我也建議使用2個大括號,而不是單一的,即

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 

對於前:

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

[[email protected] giga]$ a="1"; if [ "$a" -eq 0 ]; then echo giga; else echo koba; fi 
koba 
[[email protected] 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 
[[email protected] 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 
[[email protected] giga]$ a="1.2.3.4"; if [ "$a" == "0" ]; then echo giga; else echo koba; fi 
koba 
[[email protected] giga]$ 
+0

這裏的問題不是「爲什麼'''給出那個錯誤」,而是「爲什麼是$ ipLength空字符串「。 –