3

I want to symbolically link two arrays' elements. For example, array1 = (AAA BBB CCC DDD), array2 = (001 002 003 004), 001->AAA, 002->BBB, 003->CCC and 004->DDD.

Here is the shell script I wrote, but it doesn't work, and I couldn't figure out where is wrong.

declare -a array1=(AAA BBB CCC DDD)
declare -a array2=(001 002 003 004)
num = ${#array1[@]}
ssh username@hostmachine 'for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done' 

Can anyone give me some hints/advice? Thank you in advance.

1
  • 1
    Post also the error messages you get. Commented Nov 22, 2012 at 23:41

3 Answers 3

2

You should include all your bash code inside the parameter to ssh, like this:

ssh username@hostmachine 'declare -a array1=(AAA BBB CCC DDD); declare -a array2=(001 002 003 004); num = ${#array1[@]}; for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done'

because otherwise the ssh bash code won't get access to your previously defined arrays, because they were defined in your computer not in the ssh one.

Sign up to request clarification or add additional context in comments.

1 Comment

@Nelson This seems to work: myArray="onething anotherthing"; ssh myhost "for myValue in ${myArray[@]}; do echo \$myValue; done;" but this does not: myArray="onething anotherthing"; ssh thufirg-0105 "for ((i=0; i<2; i++ )); do do echo ${myArray[\$i]}; done;" (syntax error: operand expected (error token is "$i")) - can you explain the difference?
1

At a first look, I would say you're missing a final done in your loop:

ssh username@hostmachine 'for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done'

4 Comments

Oops, missed typing it. However, I had the 'done' in that line for my script, and it still doesn't work properly.
No obvious error, and it just doesn't get anything linked on the remote machine.
@user1846268 That's odd. I tried your snippet without ssh and it works as expected. Doess ssh ask for your password? Any output at all?
yes, without ssh, it works fine. I did enter the password correctly, but it doesn't work as expected on the 'hostmachine'. BTW, Only the first element from each array gets linked on 'hostmachine', i.e.: 001->AAA. No others.
1

The variable substitution isn't happening insside single quotes. Try double quotes instead:

declare -a array1=(AAA BBB CCC DDD)
declare -a array2=(001 002 003 004)
num=${#array1[@]}
ssh username@hostmachine "for((i = 0 ; i < $num ; i++ )); do ln -sf ${array1[$i]} ${array2[$i]}; done"

3 Comments

Yes, I tried it as well, and Only the first element from each array gets linked on 'hostmachine', i.e.: 001->AAA. No others. It seems like that '$i' didn't get iterated properly somehow.
Yes, the more I think about it, the more I believe you need to rewrite your solution. The hostmachine doesn't have access to your environment so you need to provide it as well in the ssh command (it doesn't know what array1, array2 or num are).
Speak of the devil, look at Nelson's answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.