@anverhousseini If the issue is what I mentioned above, the trick is you need to include an additional for
loop within the one you have that handles all the items in the scripts array. But you have to specify a different character as a placeholder for the array indices.
I copied/pasted your script into TextWrangler and added this as shown below. Give this a try. Note that I'm using f
for the script indexes, and left the i
for the context indexes. This separates them and allows the script to go through the first loop on one server context and stay there until it has completed going through the script arrays to completion, before moving on to the next server context.
#!/bin/bash
jss=https://jss.example.com:8443
user=example
password=example
filepath=~/Scripts/
jssresource=JSSResource/scripts/id/0
xml=(
'script1.xml'
'script2.xml'
'script3.xml'
'script4.xml'
'script5.xml'
)
context=(
'test1'
'test2'
'test3'
'test4'
'test5'
)
/bin/echo "`date`: Loop through context and XML"
for ((i = 0; i < "${#context[@]}"; i++))
do
for ((f = 0; f < "${#xml[@]}"; f++))
do
/bin/echo "`date`: POST ${filepath}/${xml[$f]} to ${jss}/${context[$i]}"
/usr/bin/curl -k -v -u "$user":"$password" "$jss/${context[$i]}/$jssresource" -T "$filepath"/"${xml[$f]}" -X POST
done
done
exit 0
I only tested this by commenting out the curl line so I would get the echoes back and it showed up like this in TextWrangler:
Wed Nov 25 14:28:32 EST 2015: Loop through context and XML
Wed Nov 25 14:28:32 EST 2015: POST /Users/mike/Scripts//script1.xml to https://jss.example.com:8443/test1
Wed Nov 25 14:28:32 EST 2015: POST /Users/mike/Scripts//script2.xml to https://jss.example.com:8443/test1
Wed Nov 25 14:28:32 EST 2015: POST /Users/mike/Scripts//script3.xml to https://jss.example.com:8443/test1
Wed Nov 25 14:28:32 EST 2015: POST /Users/mike/Scripts//script4.xml to https://jss.example.com:8443/test1
Wed Nov 25 14:28:32 EST 2015: POST /Users/mike/Scripts//script5.xml to https://jss.example.com:8443/test1
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script1.xml to https://jss.example.com:8443/test2
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script2.xml to https://jss.example.com:8443/test2
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script3.xml to https://jss.example.com:8443/test2
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script4.xml to https://jss.example.com:8443/test2
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script5.xml to https://jss.example.com:8443/test2
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script1.xml to https://jss.example.com:8443/test3
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script2.xml to https://jss.example.com:8443/test3
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script3.xml to https://jss.example.com:8443/test3
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script4.xml to https://jss.example.com:8443/test3
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script5.xml to https://jss.example.com:8443/test3
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script1.xml to https://jss.example.com:8443/test4
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script2.xml to https://jss.example.com:8443/test4
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script3.xml to https://jss.example.com:8443/test4
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script4.xml to https://jss.example.com:8443/test4
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script5.xml to https://jss.example.com:8443/test4
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script1.xml to https://jss.example.com:8443/test5
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script2.xml to https://jss.example.com:8443/test5
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script3.xml to https://jss.example.com:8443/test5
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script4.xml to https://jss.example.com:8443/test5
Wed Nov 25 14:28:33 EST 2015: POST /Users/mike/Scripts//script5.xml to https://jss.example.com:8443/test5
Edit: minor thing, but your filepath variable has a trailing slash in it, so you don't need to include it in the echo line, as its giving you double slashes in the paths as seen in the script output above.