我想在systemd單元文件中執行下列操作。我在這裏面臨的兩個問題Bash字符串長度檢查 - [::整數表達式預計
publicIPAddress可能是空字符串,因此我認爲ipLength應該是零,這應該不會造成[:整數表達式預期的錯誤,但我得到的錯誤。
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
你從'echo'行看到'$ ipLength'包含什麼?或者是「ipLength似乎每次都是空的」,表示你看到'*** ...'? –
添加'-x'來執行這個操作,你會得到什麼? –
************************************ ...是我得到的「回聲」* *********************************** $ ipLength ...「; \」命令 – KarthikJ