Bash

如何對以下變數和命令進行分組並逐一讀取

  • June 30, 2016

我關注數組和變數:

httpurl="http://www.nnin.com"
firstquery=$(curl -s -X POST -d "UID=user1&PWD=1111" $httpurl)
name="firstcust"
ip="105.105.0.1"

httpurl="http://www.mmim.com"
secondquery=$(curl -s -X POST -d "UID=user2&PWD=2222" $httpurl)
name="secondcust"
ip="106.106.0.1"

httpurl="http://www.ooio.com"
thirdquery=$(curl -s -X POST -d "UID=user3&PWD=3333" $httpurl)
name="thirdcust"
ip="107.107.0.1"

下面是我使用的程式碼:-

httpurl="http://www.nnin.com"
firstquery=$(curl -s -X POST -d "UID=user1&PWD=1111" $httpurl)
name="firstcust"
ip="105.105.0.1"

status=$?

if [ $? -eq 0 ]; then
   echo "$name : $ip (success)"
   exit $status
else
   echo "$name : $ip (failed)"
   exit $status
fi

如何對查詢和變數進行分組?(變數$status儲存了curl命令的錯誤程式碼)。如何使程式碼讀取以下每個查詢和變數並列印輸出(有人建議我使用for)並移至下一個查詢和變數。此腳本的目的是檢查來自以下 url 的返回程式碼並列印輸出Success for 0failed for non-zero.

使用類似的東西:

httpurl[0]="http://www.nnin.com"
query[0]="curl -s -X POST -d 'UID=user1&PWD=1111'"
name[0]="firstcust"
ip[0]="105.105.0.1"

httpurl[1]="http://www.mmim.com"
query[1]="curl -s -X POST -d 'UID=user2&PWD=2222'"
name[1]="secondcust"
ip[1]="106.106.0.1"

httpurl[1]="http://www.ooio.com"
query[1]="curl -s -X POST -d 'UID=user3&PWD=3333'"
name[1]="thirdcust"
ip[1]="107.107.0.1"

for i in 0 1 2
 do

   ${query[$i]) ${httpurl[$i]}

   status=$?

   if [ $? -eq 0 ]; then
       echo "${name[$i]} : ${ip[$i]} (success)"
       #exit $status
   else
       echo "${name[$i]} : ${ip[$i]} (failed)"
       #exit $status
   fi
 done

參考: Bash 參考手冊 - 數組

引用自:https://unix.stackexchange.com/questions/206270