从命令行覆盖 Bash 脚本中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4609668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:02:37  来源:igfitidea点击:

Override a variable in a Bash script from the command line

bashshellscripting

提问by Derek

How do you override a variable in your Bash script from the command line?

如何从命令行覆盖 Bash 脚本中的变量?

I know how to pass variables in, but I just want something like ./myscript.sh -Dvar=val.

我知道如何传入变量,但我只想要像./myscript.sh -Dvar=val.

回答by SiegeX

You need to use parameter expansion for the variable(s) you want to override:

您需要对要覆盖的变量使用参数扩展:

$ cat override.sh
#!/bin/bash

: ${var1:=foo} # var1 will take on the value "foo" if not overridden
var2=${var2:-foo} # same thing but more typing

echo "var1 is $var1 | var2 is $var2"

Without Override Values

没有覆盖值

$ ./override.sh
var1 is foo | var2 is foo

With Override Values

使用覆盖值

$ var1=bar var2=baz ./override.sh
var1 is bar | var2 is baz

回答by David W.

Bash isn't like Make or Ant. Those two programs won't allow you to reset the value of a macro/property once it is set on the command line. Instead in Bash, you'll have to write your scripts in such a way that allows you to set these values from the command line and not override them inside your scripts.

Bash 不像 Make 或 Ant。一旦在命令行上设置,这两个程序将不允许您重置宏/属性的值。相反,在 Bash 中,您必须以允许您从命令行设置这些值而不是在脚本中覆盖它们的方式编写脚本。

You might want to look at the getoptscommand which is a Bash builtin. That gives you an easy, flexible way to parse command line arguments and set values from the command line. For example, I have four variables OPT_A, OPT_B, OPT_C, and OPT_D. If I don't pass the parameter, they get their default value. However, I can override that default value on the command line:

您可能想查看Bash 内置命令getopts命令。这为您提供了一种简单、灵活的方法来解析命令行参数并从命令行设置值。例如,我有四个变量OPT_AOPT_BOPT_C,和OPT_D。如果我不传递参数,它们将获得默认值。但是,我可以在命令行上覆盖该默认值:

 USAGE="
$ export COMMANDLINE_FOO="FUBAR"
[-a <a> -b <b> -c <c> -d <d>]" OPT_A="Default Value of A" OPT_B="Default Value of B" OPT_C="Default Value of C" OPT_D="Default Value of D" while getopts ':a:b:c:d:' opt do case $opt in a) OPT_A=$OPTARG;; b) OPT_B=$OPTARG;; c) OPT_C=$OPTARG;; d) OPT_D=$OPTARG;; \?) echo "ERROR: Invalid option: $USAGE" exit 1;; esac done


You can also export your environment variables to allow your Bash scripts access to them. That way, you can set a variable and use that value.

您还可以导出环境变量以允许 Bash 脚本访问它们。这样,您可以设置一个变量并使用该值。

In Bash, the ${parameter:=word}construct says that if $parameteris set, use the value of $parameter. However, if $parameteris null or unset, use wordinstead.

在 Bash 中,该${parameter:=word}构造表示如果$parameter已设置,则使用 的值$parameter。但是,如果$parameter为 null 或未设置,请word改用。

Now, imagine if you did this:

现在,想象一下如果你这样做:

FOO=BARFU

[...]    #Somewhere later on in the program...

echo "I'm using '${COMMANDLINE_FOO:=$FOO}' as the value"

Now the variable $COMMANDLINE_FOO is set and readable for your shell scripts.

现在,变量 $COMMANDLINE_FOO 已为您的 shell 脚本设置并可读取。

Then, in your shell script, you can do this:

然后,在您的 shell 脚本中,您可以执行以下操作:

I'm using 'FUBAR' as the value

This will now print

这现在将打印

I'm using 'BARFU' as the value

instead of

代替

MYVAR=74 ./myscript.sh

回答by Michael Lowman

You should specify the variable with the following syntax:

您应该使用以下语法指定变量:

if [ ! -z $MYVAR ]; then
    #do something
fi

Within the script, check if it is already set before setting it:

在脚本中,在设置之前检查它是否已经设置:

# if JAVA_HOME not set, then set with default value
if [ "x$JAVA_HOME" = "x" ]; then
    JAVA_HOME=/opt/java
fi

回答by lyuboe

I would do it like this:

我会这样做:

##代码##