Iterate array using loop over ssh | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Iterate array using loop over ssh

Shell Programming and Scripting


Tags
array, iterate expression, ssh

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2013
mharald mharald is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 13 July 2013, 5:30 AM EDT
Posts: 5
Thanks: 1
Thanked 1 Time in 1 Post
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 I really need this

Can you guys help me out?

**EDIT: Forgot the CODE tags, sorry
Sponsored Links
    #2  
Old 07-12-2013
rajamadhavan rajamadhavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 11 August 2014, 2:25 AM EDT
Location: Bangalore, India
Posts: 279
Thanks: 4
Thanked 58 Times in 57 Posts
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.
The Following User Says Thank You to rajamadhavan For This Useful Post:
mharald (07-12-2013)
Sponsored Links
    #3  
Old 07-12-2013
mharald mharald is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 13 July 2013, 5:30 AM EDT
Posts: 5
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by rajamadhavan View Post
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 )

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
The Following User Says Thank You to mharald For This Useful Post:
rbatte1 (07-12-2013)
    #4  
Old 07-12-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 November 2014, 5:17 PM EST
Location: Saskatchewan
Posts: 19,925
Thanks: 846
Thanked 3,417 Times in 3,202 Posts
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 01:10 PM..
Sponsored Links
    #5  
Old 07-13-2013
mharald mharald is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 13 July 2013, 5:30 AM EDT
Posts: 5
Thanks: 1
Thanked 1 Time in 1 Post
Quote:
Originally Posted by Corona688 View Post
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 )

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

A nice Weekend to all.
Sponsored Links
    #6  
Old 07-15-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 November 2014, 5:17 PM EST
Location: Saskatchewan
Posts: 19,925
Thanks: 846
Thanked 3,417 Times in 3,202 Posts
Quote:
Originally Posted by mharald View Post
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.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Do loop doesn't iterate asandy1234 Shell Programming and Scripting 11 06-12-2013 07:08 PM
Array Variable being Assigned Values in Loop, But Gone when Loop Completes??? mrm5102 Shell Programming and Scripting 5 10-19-2012 11:00 AM
Iterate/Loop Through XML Node List bradlecat UNIX for Dummies Questions & Answers 1 06-05-2012 04:17 AM
How to loop(Iterate) through List with foreach(csh) eawedat Shell Programming and Scripting 2 08-25-2011 07:12 PM
iterate over array IMTheNachoMan Shell Programming and Scripting 2 05-04-2008 01:46 PM



All times are GMT -4. The time now is 09:38 PM.