-2

I am wondering about some use case with bash script. Imagine that user type:

./my_script.sh INSTALL=FALSE

and then package is installed without any dependencies or type INSTALL=TRUE and then package is installed with all dependencies.

Now the script should know that INSTALL=FALSE is default way so it means that is okay to type just

./my_script.sh

but user needs to specify

./my_script.sh INSTALL=TRUE

if he wants to install package with all dependencies.

Is it possible? How to do it?

Barmar
  • 741,623
  • 53
  • 500
  • 612
sqr
  • 35
  • 5
  • 2
    Start by showing us what you're trying that _doesn't_ work. – Charles Duffy Jan 09 '23 at 21:34
  • 2
    BTW, it'd be easier for `INSTALL=TRUE ./my_script.sh` to be how it's overridden, instead of `./my_script.sh INSTALL=true`. The first way the variable gets passed through the environment, the second way you have to parse it out from command-line arguments. – Charles Duffy Jan 09 '23 at 21:35
  • [bash command line arguments replacing defaults for variables](https://stackoverflow.com/questions/16319720/bash-command-line-arguments-replacing-defaults-for-variables) – Charles Duffy Jan 09 '23 at 21:36
  • [override a variable in a bash script from the command line](https://stackoverflow.com/questions/4609668/override-a-variable-in-a-bash-script-from-the-command-line) – Charles Duffy Jan 09 '23 at 21:36
  • 1
    Give the variable a default value in the script before you check arguments. – Barmar Jan 09 '23 at 21:39
  • BTW, it would be better if you used standard argument formatting like `--install true/false`. Then you can process arguments with `getopts`. – Barmar Jan 09 '23 at 21:40
  • For users who have the `-k` option enabled, `./my_script.sh INSTALL=TRUE` is equivalent to `INSTALL=TRUE ./my_script.sh`; a variable named `INSTALL` with a value `TRUE` is added to the environment in which `my_script.sh` executes. – chepner Jan 09 '23 at 21:43

1 Answers1

0

If you want your script to remember the config, you should create a config file with correct setting.

  • If script ran without any argument, read config file.
  • If config file doesn't exist, create it with INSTALL=FALSE
  • If script was given an argument, overwrite new setting to config file.

If script ran without any argument, and config file doesn't exist, create a config file with INSTALL=FALSE

Calvin Kim
  • 359
  • 2
  • 6
  • Having said "config file" what do you mean? I thought that I can put the variables inside of my_script.sh file which corresponds to FALSE or TRUE and then I call out them in CLI like in my previous post. – sqr Jan 12 '23 at 11:36
  • @sqr You can pass an argument(false/true) to your script, but does your script remember this setting next time? You can default your script to 'false', meaning you have to tell your script every time if you want 'true'. Or my suggestion is to write your current setting (either 'true' or 'false') to a config file. And have your script to read that config file every time. Then your script will default to whatever config file says. – Calvin Kim Jan 12 '23 at 18:35