Iterate array using loop over ssh


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-12-2013
Iterate array using loop over ssh

A simple script:

Code:
#!/bin/bash
test=test
test1=(test1 test2 test3)
echo ${test1[*]}
ssh server 'echo '$test'; echo '${test1[*]}' ; echo '${test1[1]}' ; for m in $(seq 1 $(echo '${test1[*]}' | tr " " "\n" | wc -l)); do echo $m ; echo '${test1[$m]}'; done'

Here is the result:

Code:
test1 test2 test3
testing
test
test1 test2 test3
test2
1
test1
2
test1 ***this should be "test2"
3
test1 ***this should be "test3"

Why is it not being iterated as expected?

I read somewhere that ssh doesn't know about the variables, etc etc. Then why can I see that "echo ${test1[*]}" iterates correctly?

BUMMER Smilie I really need this Smilie

Can you guys help me out?

**EDIT: Forgot the CODE tags, sorry Smilie
# 2  
Old 07-12-2013
I think, this is expected.

When your commands go to server, all the variables are replaced by its values.
The 'echo '${test1[$m]}' at the end of your command is evaluated by your local system (the first element of the array since it cannot make out what $m is) and would be replaced by 'test1' by the time it reaches the server . So it prints the same for all the iterations.
This User Gave Thanks to rajamadhavan For This Post:
mharald (07-12-2013)
# 3  
Old 07-12-2013
Quote:
Originally Posted by rajamadhavan
I think, this is expected.

When your commands go to server, all the variables are replaced by its values.
The 'echo '${test1[$m]}' at the end of your command is evaluated by your local system (the first element of the array since it cannot make out what $m is) and would be replaced by 'test1' by the time it reaches the server . So it prints the same for all the iterations.

Yes, makes sense (now that you mention it Smilie)

But that's the nice thing about BASH, there's no "No way out" problem:

Code:
#!/bin/bash
TEST=test
TEST1=(test1 test2 test3)
ssh server 'TEST2=('${TEST1[*]}') ; for m in $(seq 0 $(eval echo \${TEST2[*]} | tr " " "\n" | wc -l)); eval echo \${TEST2[$m]}; done'

Declaring a Variable, that is iterated before the loop, so that the array gets populated as expected and then iterated together with the loop variable $m.

Thank you for the hint. A nice day to you all Smilie
This User Gave Thanks to mharald For This Post:
rbatte1 (07-12-2013)
# 4  
Old 07-12-2013
Another way around the variable problem, a particularly robust one, is this:

Code:
ssh username@host /bin/sh -s arg1 arg2 arg3 <<"EOF"
literal script contents
that don't even need
extra quotes
or escaping
EOF

Also, "${ARRAY[@]}" is a safer way to expand an array, because it splits upon elements, not spaces. It works for commandline arguments too. Also, your loop can be simplified quite a lot. How about this:

Code:
ssh username@host /bin/sh -s "${TEST1[@]}" <<"EOF"
echo "$@"
echo "${1}"

N=0
for m in "$@" # Expands into $1 $2 $3 ...
do
        echo $m
        echo $N
        let N=N+1
done
EOF


Last edited by Corona688; 07-12-2013 at 12:10 PM..
# 5  
Old 07-13-2013
Quote:
Originally Posted by Corona688
Another way around the variable problem, a particularly robust one, is this:

Code:
ssh username@host /bin/sh -s arg1 arg2 arg3 <<"EOF"
literal script contents
that don't even need
extra quotes
or escaping
EOF

Never thought of using it for ssh. I wonder if it iterates all variables as expected. I will give it a try!
Quote:
Also, "${ARRAY[@]}" is a safer way to expand an array, because it splits upon elements, not spaces. It works for commandline arguments too. Also, your loop can be simplified quite a lot. How about this:
Thanks. Your improvement advice actually reflects my running script (more or less Smilie)

I kinda messed up in trying to keep the example script simple Smilie

A nice Weekend to all.
# 6  
Old 07-15-2013
Quote:
Originally Posted by mharald
Never thought of using it for ssh. I wonder if it iterates all variables as expected.
Depends what you expect. You get nothing but the $1 $2 $3 ... arguments you put into it.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Bash for loop array mwheeler12 Shell Programming and Scripting 5 03-27-2018 10:50 AM
Using nested for loop to iterate over file names zBernie Shell Programming and Scripting 4 01-22-2015 04:59 AM
awk loop using array:wish to store array values from loop for use outside loop klane Shell Programming and Scripting 2 12-19-2013 03:25 PM
Ssh to an array of servers in a for loop Sree10 Shell Programming and Scripting 9 09-12-2013 12:44 PM
Do loop doesn't iterate asandy1234 Shell Programming and Scripting 11 06-12-2013 06:08 PM
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 10:00 AM
Iterate/Loop Through XML Node List bradlecat UNIX for Dummies Questions & Answers 1 06-05-2012 03:17 AM
Array with do while and if loop nitin Shell Programming and Scripting 5 11-14-2011 10:09 PM
How to loop(Iterate) through List with foreach(csh) eawedat Shell Programming and Scripting 2 08-25-2011 06:12 PM
linux c, loop through 2d array? trey85stang Programming 2 06-03-2011 07:43 PM
loop in array in python grossgermany Shell Programming and Scripting 1 04-16-2010 08:04 PM
Reading from while loop into an array ssuresh1999 UNIX for Dummies Questions & Answers 2 08-12-2008 11:53 AM
How to use array values after the loop. Devesh5683 Shell Programming and Scripting 1 05-13-2008 07:38 PM
iterate over array IMTheNachoMan Shell Programming and Scripting 2 05-04-2008 12:46 PM
Array Declaration and For Loop 33junaid Shell Programming and Scripting 4 09-15-2007 04:16 PM