0

I am trying to read comma separated variable in shell script and splitting it like as below

while [ -z "$variable" ]
do      printf 'variable: '
        read -r variable
        [ -z "$variable" ] && echo 'Action number cannot be empty; try again.'
done

for i in $(echo ${variable} | sed "s/,/ /g")
do
    echo "$i"
done

It gives be output as below

abc
def

But if i am trying same thing with SSH its not working i am trying as below

while [ -z "$variable" ]
do      printf 'variable: '
        read -r variable
        [ -z "$variable" ] && echo 'Action number cannot be empty; try again.'
done

ssh -i my.pem -p 2022 ec2-user@ip-address 'bash -s' << EOF

sudo su - << SUEOF

echo "input $variable"

for i in $(echo ${variable} | sed "s/,/ /g")
do
    echo "$i"
done
SUEOF
EOF

But in SSH its not printing the values of input variable i am using echo to check the variable is passing into SSH session and i can see the variable is passing to SSH session

variable: abc,def
input abc,def


Please help me solving the issue

  • If the answer solves your problem, you should consider ^ (upvoting) and tick-mark (accepting) the answer. Judging from your questions history, you have not accepted answers to any of your previous questions – Inian Sep 6 at 3:45
  • Why don't you let the remote shell script read the values? Why do you need to first read them, then pass them over ssh, parse them and then use them? – Kusalananda Sep 6 at 6:27
  • @Kusalananda i tried that but when like below ssh -i my.pem -p 2022 ec2-user@ip-address 'bash -s' << EOF echo "input $variable" for i in $(echo ${variable} | sed "s/,/ /g") do echo "$i" done EOF – Rahul Sep 6 at 6:35
  • Is there any reason why you can't write a script that you execute completely on the remote side of the ssh connection, as in ssh -i ... ./myscript.sh? – Kusalananda Sep 6 at 6:42
  • yes i want only one script should perform this action without using any another script – Rahul Sep 6 at 6:43
2

It is because the $variable expansion inside the ssh heredoc, is expanded by the local shell on the local machine rather than in the remote shell. Generally, we escape the expansion sequences i.e. variable expansion $var as \$var and command substitutions as \$(..) instead of $(..), if we expect the expansion to happen in the remote shell.

So in your for loop, the split on , happens with your sed command but your "$i" expansion will again happen in the local shell which should have been happening in the remote shell. Due to lack of appropriate escape sequences, the echo "$i" will never see a value in the local shell.

You can get around by marking $i as \$i so that, its expansion happens remotely. Also the loop for i in $(echo $variable | sed sed "s/,/ /g") is an extremely fragile way to iterate over a list split on de-limiter ,. Use the shell built-ins, read in this case

ssh -i my.pem -p 2022 ec2-user@ip-address 'bash -s' <<EOF
echo "input $variable"
IFS="," read -ra split <<<"$variable"
for var in "\${split[@]}"; do
    printf '%s\n' "\$var"
done
EOF

Note the usage of escape sequences around the array expansion, "\${split[@]}" and the variable "\$var" which ensures the expansion of those variables happen remotely and not in the local machine.

  • Thanks for the reply @Inian this solution works perfectly...one last question i was trying to access the $var into variable but i am getting empty variable for example i am trying to create directories mkdir $var but $var value is coming empty – Rahul Sep 6 at 4:43
  • @Rahul: Did you read through the answer and understand it well enough? Inside the remote shell, the $var needs to be accessed as "\$var" – Inian Sep 6 at 4:55
  • 1
    That's very dangerous as the expansion of the $variable (by the local shell) ends up interpreted as code by the remote shell. Think of the case where $variable contains $(reboot) for instance. Here, you could pass the shell code as arguments instead of stdin and pass the content of $variable over stdin (or an env var with ssh -o SendEnv). – Stéphane Chazelas Sep 6 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.