2

I'm calling Uncle. I'm attempting to manipulate variables that have hard coded values in a second bash script I am calling. I have no control over the script and am building a wrapper around it to adjust some build behavior before it finally kicks off a yocto build. I'm not sure what else to try after reading and trying numerous examples.

Examples of the situation:

build.sh calls build2.sh

IS_DEV=1 ./build2.sh #trying to override value

build2.sh

IS_DEV=0 # hardcoded value
echo $IS_DEV
# always results in 0.

I have also tried export IS_DEV=1 before calling build2.sh.

I'm sure this is pretty simple, but I cannot seem to get this to work. I appreciate any assistance. Is this possible? I'm using GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu) on Ubuntu 16.04.4 LTS.

Oh, I have also tried the sourcing technique with no luck.

IS_DEV=1 . ./build2.sh
IS_DEV=1 source ./build2.sh

Where am I getting this wrong?

Much appreciated.

  • This question, and the answers, would indicate you may be out of luck unless you can get support for this in the second script https://stackoverflow.com/questions/4609668/override-variable-in-bash-script-from-command-line – Brandon Miller Jun 15 '18 at 19:43
  • Possible duplicate of [override variable in bash script from command line](https://stackoverflow.com/questions/4609668/override-variable-in-bash-script-from-command-line) – Brandon Miller Jun 15 '18 at 19:44
  • @BrandonMiller yes I have looked at that post and tried the examples, no go. I'll see if I can gain the ability to tweak the second script. thx. – Frank Beard Jun 15 '18 at 19:48
  • 1
    I'd always have written your build2 to have `: "${IS_DEV:=0}"`, making 0 explicitly an overridable default. (BTW, all-caps variables are used by the shell and POSIX utilities for their own use; the namespace of variables with at least one lower-case letter are reserved for application use and guaranteed not to modify the shell's behavior; see relevant spec @ http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph) – Charles Duffy Jun 15 '18 at 20:07
  • @CharlesDuffy I wish I could directly edit the called script and clean it up, but I cannot. :( – Frank Beard Jun 15 '18 at 20:09
  • Now I will work on adding multiple `sed` replaces before executing the second script. Learning some new awesome stuff every day. – Frank Beard Jun 15 '18 at 20:12
  • Tangentially, remember you can perform multiple replacements in a single `sed` script. See https://stackoverflow.com/questions/7657647/combining-two-sed-commands – tripleee Jun 15 '18 at 20:36

1 Answers1

4

If you can't modify the script, execute a modified version of it.

sed 's/^IS_DEV=0 /IS_DEV=1 /' build2.sh | sh

Obviously, pipe to bash if you need Bash semantics instead of POSIX sh semantics.

If the script really hard-codes a value with no means to override it from the command line, modifying that script is the only possible workaround. But the modification can be ephemeral; the above performs a simple substitution on the script, then passes the modified temporary copy through a pipe to a new shell instance for execution. The modification only exists in the pipeline, and doesn't affect the on-disk version of build2.sh.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • See also tangentially [Difference between `sh` and `bash`](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Jun 15 '18 at 19:48
  • thank you! this saved me. I've never used `sed`, now I will. thx again. – Frank Beard Jun 15 '18 at 20:07
  • hey guys, the second part of this is that after using `sed` to rewrite an in-memory copy of the second script, I also need to "forward along" a big command line to the same script. what is the approach for that? example: `sed 's/^IS_DEV=0 /IS_DEV=1 /' build2.sh | bash -d -a 1 --logs` I would want all the "additional" cmd line args to pass to `build2.sh` and not as args to the bash shell. – Frank Beard Jun 15 '18 at 20:17