0

I have one config and one main bash script files. Config file has variables assigned with values whereas, the main file wants to access them. Below are my files,

config.sh

#!/bin/bash

username=abc
password=abc
hosts=(192.168.110.164);
walletname=varsh

main.sh

#!/bin/bash
source ~/shell/config.sh

echo "$username"
echo "$walletname"

start_block(){
  echo "$username"
  echo "$walletname"
}

SCRIPT="$(declare -f); start_block"
for i in ${!hosts[*]} ; do
        printf "\t=========== Logging in ${hosts[i]} ===========\n\n"
        SCR=${SCRIPT/PASSWORD/${password}}
        sshpass -p ${password} ssh -l ${username} ${hosts[i]} "${SCR}"
done

$username and $walletname can be accessed from outside of the start_block function but not from inside the function. The echo from the function start_block gives empty results. I tried using export before variable names in config. How can I access these variables?

Edit:

I want to login to another host using the username password, create some files, run installation and other commands using config file variables. And there could be many variables in the config.

7
  • From taking a quick look at your code, it does not look like start_block is being executed anywhere in the script Commented Nov 16, 2020 at 7:59
  • I can't tell what you mean by SCRIPT="$(declare -f); start_block" Commented Nov 16, 2020 at 7:59
  • ok, I see: SCRIPT gets a list of ALL functions defined (i.e, ftn_1() {...}; ftn_2() {...}; ...) and then you try to search/replace all instances of PASSWORD with the variables in the config. Note: currently, SCR will only replace the first instance, to replace all, you'll need SCR=${SCRIPT//PASSWORD/${password}} (two slashes). But I don't know if that resolves your issue. Commented Nov 16, 2020 at 8:09
  • Second, I don't see how any of the config variables are going to be used in $SCR (or $SCRIPT for that matter). (As already mentioned.) There might be a way to accomplish what you're trying to do here, but it seems like a really fragile way to implement variables in a shell script (i.e, global search & replace based on pattern). (rest of comment deleted, doesn't work). Commented Nov 16, 2020 at 8:25
  • This is the working code for ssh and script. Logging in ${!hosts[*]} is not the issue. start_block function is also getting executed (tested with printing some string inside function). Commented Nov 16, 2020 at 8:33

2 Answers 2

2

Your variables are not sent to the remote host. You can forcibly inline them, like you do the functions:

SCRIPT="$(env) ; $(declare -f) ; start_block"

I'd, however, recommend just inlining the config directly:

SCRIPT="$(cat ~/shell/config.sh) ; $(declare -f); start_block"
Sign up to request clarification or add additional context in comments.

2 Comments

I want to send the functions also. Can I inline the file itself. eg. SCRIPT="$(cat ~/shell/config.sh) ; $(cat ~/shell/main.sh) ; $(typeset -f) ; first"? This pass the functions but give error first bash: line 28: /home/remotehost/shell/config.sh: No such file or directory cat: /home/remotehost/shell/config.sh: No such file or directory cat: /home/remoteost/shell/main.sh: No such file or directory
The error is saying that your cat commands are running remotely (they should run locally). I can't say more without seeing the complete code.
1

The function is executed, when you do a ssh -l ${username} ${hosts[i]} "${SCR}". Hence it is executed in a separate process, most likely even on a different host. Of course you don't see your shell variables over there. Actually it is surprising that on the remote side, start_block does not give raise to a command not found error, since the function also exists in your local process only.

As for the variables, you could turn them into environment variables (by using the export command) and use the SendEnv and AcceptEnv options in your ssh configuration file, as explained for instance here.

1 Comment

The declare-f inlines all the local function definitions so they are also declared remotely.

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.