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

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
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
awk script: loop through array ENG_MOHD Shell Programming and Scripting 8 11-09-2013 05:34 AM
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
Help with awk in array in while loop vincyoxy Shell Programming and Scripting 2 03-19-2009 12:36 PM
iterate over array IMTheNachoMan Shell Programming and Scripting 2 05-04-2008 12:46 PM


All times are GMT -4. The time now is 05:36 AM.

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