我有以下数组和变量:
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 0
和failed for non-zero
.
答案1
使用类似的东西:
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 参考手册 - 数组