2

In this GitHub repository I have a directory named nwsm. This directory contains the file nwsm.sh that contains a master script (a script that runs other scripts).

The directory also contains a few other files which contain sub-scripts that the master script executes, each one at a time.

In nwsm.sh I declare a few variables and these variables should eventually be used inside all aforementioned subscripts. Variable expansion in both nwsm.sh and subscripts should take place the same way, only in their execution.


Note that the directory doesn't contain any other files besides nwsm.sh and its sub script files, nor that it should contain other files, at any time in the future.

Variable expansions inside the subscripts should occur with the relevant values defined in nwsm.sh, after nwsm.sh started to run.

This is the master script in nwsm.sh (first the variable declarations with the read utility, then the execution of the adjacent files):

#!/bin/bash
domain="$1" && test -z "$domain" && exit 2

read -sp "Please enter DB root password:      " dbrootp_1 && echo
read -sp "Please enter DB root password again:" dbrootp_2 && echo
if [ "$dbrootp_1" != "$dbrootp_2" ]; then echo "Values unmatched" && exit 1 fi

read -sp "Please enter DB user password:      " dbuserp_1 && echo
read -sp "Please enter DB user password again:" dbuserp_2 && echo
if [ "$dbuserp_1" != "$dbuserp_2" ]; then echo "Values unmatched" && exit 1 fi

"$PWD"/tests.sh
"$PWD"/wp-cli.sh
"$PWD"/nginx.sh
"$PWD"/dbstack.sh
"$PWD"/certbot.sh

How could I ensure that values defined in nwsm.sh will be available to all its counterparts while .nwsm, and them, are running?

2

If you mean you just want to have the variables visible when the main script runs the other scripts, then you just export them:

$ cat main.sh 
#!/bin/sh
read foo
export foo
./foo.sh
$ cat foo.sh 
#!/bin/sh
echo "foo is $foo"

$ ./main.sh 
blah
foo is blah
$ 

The other scripts run as subprocesses of the main script, and exported variables get passed to them through the environment. None of this limits the variables to scripts in a particular directory, exported variables are visible to all programs started by the main script. If you want to run some program without passing the variables to them, you'll have to unexport them with export -n first. You could also unexport at the start of the other scripts, to avoid them passing the variables on.

Also note that there's no need to clear the variables or unexport them at the end of the main script (or the others). The variables only exist in the memory of the running shell processes, and when the process ends, the variables disappear.

(Passing variables to independent processes, on the other hand, would require saving them to a file or some such.)


Of course, another way to achieve almost the same kind of modularization would be to split the program into functions, store them in separate files and source those files from the main script. That way, all the variables in the program would be visible to all functions. (Which may or may not be preferable.)

  • So much thanks! How do you properly export read variables? (I have something in mind but ask just in case). – user9303970 Feb 14 '18 at 21:25
  • 1
    @user9303970, just read var; export var; there's the sample script in the answer. (I don't think there's a flag in read to make it export the variables it writes) Or, you could use set -a in the beginning to make all modified variables automatically exported but that has a wider effect and is much less explicit. – ilkkachu Feb 14 '18 at 21:46
0

If you export your variables, then at end of your master script, "unset" the variables.

unset domain
unset dbrootp_1
unset dbrootp_2
unset dbuserp_1
unset dbuserp_2

unset: unset [-f] [-v] [name ...] For each NAME, remove the corresponding variable or function. Given the -v', unset will only act on variables. Given the-f' flag, unset will only act on functions. With neither flag, unset first tries to unset a variable, and if that fails, then tries to unset a function. Some variables cannot be unset; also see readonly.

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.