0

I've following array and variable :

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"

below is the code I use :-

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

how do I grouped the query&variable? (the variable $status stored the error code for curl command). How do i make the code read each of the following query&variable and print the output (someone suggest I use for) and move to next query&variable. the purpose of this script to check the return code from the following url and print the output Success for 0 and failed for non-zero.

1 Answer 1

0

Use some thing like:

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

Reference: Bash Reference Manual - Arrays

3
  • when i run the script...I only get 1 Success,it supposed to get 3 Success right? (bcos of the 3 curl cmd)
    – Asif
    Commented Jun 2, 2015 at 3:33
  • @Asif, It depend how you want to handle failures. Better to call exit after the loop.
    – user.dz
    Commented Jun 2, 2015 at 7:08
  • i've succeed...thnks btw..really helped a lot..
    – Asif
    Commented Jun 2, 2015 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .