0

I am passing variable from one shell script to another which is being executed on another remote server.

Script 1

echo "Identification No."
read id
export id
ssh atul@10.95.276.286 'bash -s' < data_file.sh

Script 2

echo "ID is ---- "$id
cd /abc/xyz/data/
cat data_abcxyz.txt|grep '|$id|'|wc -l

By this way I am not able to get any output even the id is also null in the second script.

I have also tried

ssh atul@10.95.276.286 'bash -s' < data_file.sh "$id"

But got no output.

Any help on this is greatly appreciated. I am using unix AIX.

atul329
  • 67
  • 1
  • 3
  • 9

2 Answers2

1

export on one host is absolutely not going to affect an entirely different host... it doesn't even affect another shell running on the current host.

Your second attempt is better and might even work if your script were checking for positional arguments but it isn't. (It might not even work in that case as I'm not at all sure that the command line argument would make it through to the script through ssh and bash -s.

You might be able to do something more like:

ssh atul@10.95.276.286 "bash -s $id" < data_file.sh

to pass the argument to the remote bash directly but your script would still need to use positional arguments and not expecting named variables to already exist.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
1

Exporting won't have any effects on the environment of remote scripts. You can set up a remote script's environment by specifying the env variables on the command line before the actual command, which you can btw use for local commands too.

 ssh atul@10.95.276.286 "id=$id bash -s" < data_file.sh

If you pass "$id" this way:

 ssh atul@10.95.276.286 'bash -s' < data_file.sh "$id"

It'll be your script's first parameter, AKA "$1" and you'll be able to access it from your script that way.

Note that '|$id|' in your "Script 2" will be interpreted as a literal string, since you're using single quotes.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142