1

In the below bash I am trying to pass the ${array[$i]} to ssh after changing to a specific directory, but ${array[$i]} it is not recognized? The goal is to use the id in ${array[$i]} (there may be more than 1) to further go into that directory. The bash seems to work as expected except for the ${array[$i]} not be passed.

bash

readarray -t array <<< "$(printf '%s\n' $id)"
for ((i=0; i<${#array[@]}; i++ )) ; do
   echo "${array[$i]}"
done

 sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx "${array[$i]}" 'cd path/to/folder/"$array[$i]" && exec bash -l'
 echo ${array[$i]}

maybe?

readarray -t array <<< "$(printf '%s\n' $id)"
for ((i=0; i<${#array[@]}; i++ )) ; do
   echo "${array[$i]}"
done

 for i in "${array[$i]} ; do
 sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx "${array[$i]}" 'cd path/to/folder && exec bash -l'
done

contents of array[$i] ---- array[$i] will be different in number each time but the format will always be the same ----

00-0000-xxx-xxx-xxx
00-0001-yyy-yyy-yyy

desired ssh

cd path/to/folder/00-0000-xxx-xxx-xxx && cp *.txt* /home/location
cd path/to/folder/00-0000-yyy-yyy-yyy && cp *.txt* /home/location
10
  • 1
    As a future note, printf '%s\n' "${array[@]}" is a much easier way to print all array contents one-line-to-an-item. Commented Oct 11, 2019 at 14:58
  • 1
    That said, because array contents can contain literal newlines, the above isn't a safe serialization format; only NULs aren't allowed to be present inside elements (which is why they're what you should use). Commented Oct 11, 2019 at 14:58
  • 1
    Anyhow, I don't know how/why you expect the above code to work, or even what "working" means in its context -- the sshpass command isn't in a loop at all, f/e; and when you pass multiple arguments to ssh, they're just concatenated together into a single shell command. Commented Oct 11, 2019 at 14:59
  • 1
    ...so, when you pass "${array[$i]}" as an extra argument: (1) what kind of thing do you expect it to contain? (2) how, and why, do you expect the remote shell to parse that opaque thing you aren't showing us in any particular way? Commented Oct 11, 2019 at 15:00
  • 1
    I'm not saying that, because I don't understand what you're actually trying to do, so I can't say anything useful and specific (and afaict, neither can anyone else). Do you want only one ssh connection to happen passing all the array elements across at the same time? Do you want one connection per element? ...? Commented Oct 11, 2019 at 15:42

1 Answer 1

1

Here, we generate a single remote that runs (cd ... && exec cp) for each array element, with each in a subshell to prevent the cd from having side effects on subsequent elements' commands:

printf -v cmd_q '(cd /path/to/folder/%q && exec cp -- *.txt* /home/location)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx "$cmd_q"

The exec is a minor performance enhancement, consuming the subshell started with the parenthesis by replacing it with the cp process.

The %q placeholder in the printf format string gets substituted with the individual array values.

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

1 Comment

Thank you very much for your help and explanation :)

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.