Home
Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Iterate array using loop over ssh

Tags
array, iterate expression, shell scripts, ssh

Login 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 I really need this

Can you guys help me out?

**EDIT: Forgot the CODE tags, sorry
# 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.
The Following User Says Thank You to rajamadhavan For This Useful 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 )

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
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..
# 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 )

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

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 to Reply

« Previous Thread | Next 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
Bash for loop array mwheeler12 Shell Programming and Scripting 5 03-27-2018 11:50 AM
Using nested for loop to iterate over file names zBernie Shell Programming and Scripting 4 01-22-2015 05: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 04:25 PM
Ssh to an array of servers in a for loop Sree10 Shell Programming and Scripting 9 09-12-2013 01:44 PM
Do loop doesn't iterate asandy1234 Shell Programming and Scripting 11 06-12-2013 07:08 PM
Loop over array in batches dietmar13 Shell Programming and Scripting 2 05-13-2013 02:14 AM
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
Array with do while and if loop nitin Shell Programming and Scripting 5 11-14-2011 11:09 PM
How to loop(Iterate) through List with foreach(csh) eawedat Shell Programming and Scripting 2 08-25-2011 07:12 PM
Help with awk in array in while loop vincyoxy Shell Programming and Scripting 2 03-19-2009 01:36 PM
Reading from while loop into an array ssuresh1999 UNIX for Dummies Questions & Answers 2 08-12-2008 12:53 PM
How to use array values after the loop. Devesh5683 Shell Programming and Scripting 1 05-13-2008 08:38 PM
iterate over array IMTheNachoMan Shell Programming and Scripting 2 05-04-2008 01:46 PM
Array Declaration and For Loop 33junaid Shell Programming and Scripting 4 09-15-2007 05:16 PM


All times are GMT -4. The time now is 06:53 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password