1

This is what I am trying to do...

#!/bin/bash

array_local=(1 2 3 4 5)

ssh user@server << EOF
index_remote=1
echo \$index_remote
echo \${array_local[\$index_remote]}  
EOF

When I try to run the above script I get the O/P as 1 and a null value (blank space). I wanted ${array_local[$index_remote} value to be 2 instead of null, I need to access this local array using remote variable for my further work in the script..

6
  • What shell are you using? bash? Commented Oct 28, 2013 at 16:31
  • What makes you think that a variable (array_local) defined on your local system will be magically pre-defined for you in a different shell session in a completely separate process running on a remote server? That variable is local to the shell that runs ssh, and, even if you figure out the proper quoting to use, it is not set in the shell instance that runs echo... Commented Oct 28, 2013 at 17:16
  • @AdamSpiers Yes, it is a bash shell Commented Oct 29, 2013 at 2:28
  • @twalberg yes the variable array_local is defined locally, but I can access the variable within the remoter server, I have confirmed this by giving an echo ${array_local[*]} in the remote server process, it does print the array..the question is how do I now access the array with the remote variable, which is local to the remote server.. Commented Oct 29, 2013 at 2:31
  • @user2928822 I think you misunderstood the results of ${array_local[*]}. The way you are running that, it is expanded on the local end, before it is passed to the remote server. What the remote server sees is echo 1 2 3 4 5. The local variable is not available on the remote end. Commented Oct 29, 2013 at 4:47

1 Answer 1

1

<<EOF results variable expansion happening on the local machine, but you only defined the variable i on the remote machine. You need to think carefully about where you want to do the expansion. You haven't explained in your question whether the value of i is defined client-side or server-side, but I'm guessing from your subsequent comments that you want it done server-side. In that case you'll need to pass the array contents over ssh, which requires careful quoting:

ssh hostname@server <<EOF
i=1
eval `typeset -p array_local`
echo \${array_local[\$i]}
EOF

typeset -p array_local will output the string

declare -a array_local='([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")'

Since this is inside backticks, it will get expanded client-side within the EOF-delimited heredoc, and then evaluated server-side by the eval. In other words it's equivalent to:

ssh hostname@server <<'EOF'
i=1
declare -a array_local='([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")'
echo ${array_local[$i]}
EOF

Notice the difference in EOF quoting between the two examples. The first one allows parameter and shell expansion, and the second doesn't. That's why the echo line in the first one needs quoting, to ensure that parameter expansion happens server-side not client-side.

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

6 Comments

I understand that, but I can't figure out any way around, I have tried using 'EOF', that makes my array_local null as obvious, I am looking for a way in which I can access both my local as well as remoter variable within a command..is it just my fiction to think so or is it really possible??
It's absolutely possible (and not even hard) but you still haven't clearly described what you are trying to achieve.
I've guessed what you are asking for and expanded my answer.
I have edited my question, I have tried with \${array_local[\$i]}, it still gives me a null value...!!
Read my answer again. The typeset part is not optional. Also reread twalberg's first comment. It seems that you are still not thinking clearly about what gets expanded when.
|

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.