0

I have a script file (sub.sh) with the variable inside, for example:

cp -r "$dir1" "$dir2"
chmod 600 "$dir2"
# and many other commands...

I have another script (main.sh) that prepare the variables, export them to the environment, and run the script file above on another server via SSH. I tried the following command but it doesn't work because the variables are not pre-processed before passing to the server:

cat ./sub.sh | ssh $user@$ip

I know that I can use Heredoc like this:

ssh $user@ip << EOF
  cp -r "$dir1" "$dir2"
  chmod 600 "$dir2"
  # and many other commands...
EOF

but the script is long and I want to split the file to make the code cleaner.

How can I achieve this? Thanks.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Do any of the solutions in ["Pass variables to remote script through SSH"](https://stackoverflow.com/questions/6746168/pass-variables-to-remote-script-through-ssh) solve your problem? It looks like a variant of David Newcomb's answer would work (provided the local and remote shells are both bash): `{ set; cat ./sub.sh; } | ssh $user@$ip` – Gordon Davisson Aug 05 '23 at 07:08
  • You wrote: `main.sh` prepares and export the variables. Is this on the local or remote server? – Walter A Aug 05 '23 at 10:50
  • @WalterA It's on my local. I want to run the script in multiple servers. The number of servers may change and I may have to run it again and again, so I don't want to copy it to the remotes. – Giang Phạm Aug 07 '23 at 01:55
  • When you don't want to use all settings that `set` will show, you can try to change main.sh: Don't export the variables to the environment but echo them on the commandline like `var1=value1` and use `{ main.sh; cat ./sub.sh; } | ssh $user@$ip` – Walter A Aug 07 '23 at 10:49

2 Answers2

1

You can store your variables in a .sh file like config.sh

When executing you can use following command ./sub.sh config.sh

All the variables inside config.sh will be used as named variables (you can access them via name ) like $dir1

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
1

Use declare -p to pass variables :

{ declare -p dir1 dir2; cat ./sub.sh; } | ssh $user@$ip
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Note that `declare -p` will print the variable declaration(s) in bash-compatible syntax; if the remote shell is different *and* any of the variables contain sufficiently weird characters, the declarations might get misparsed on the remote system. (Hopefully this will not be a problem.) – Gordon Davisson Aug 06 '23 at 00:47
  • @GordonDavisson As OP tagged only `bash`, we would expect sub.sh is bash-compatible. – Philippe Aug 06 '23 at 07:58
  • @Philippe Thank you so much, this solves my problem perfectly. – Giang Phạm Aug 08 '23 at 01:17
  • @Philippe I've learned not to trust the bash tag (or even people explicitly saying they're using bash) -- too many people seem to think it's a generic term for unix shells, or are confused about what they're actually using. I've even seen people identify DOS-style batch scripts as bash! – Gordon Davisson Aug 09 '23 at 20:32