When to use environment variable or command line parameter?
何时使用环境变量或命令行参数?
I think there are two ways to pass parameters to another process in a script ,environment variable and command line parameters.
我认为有两种方法可以将参数传递给脚本,环境变量和命令行参数中的另一个进程。
Thus,in which condition we choose one instead of another?
那么,在哪种情况下我们选择一种而不是另一种?
3 个解决方案
#1
In most of the scripts I write, I allow both with the command line parameters taking preference.
在我编写的大多数脚本中,我允许使用命令行参数优先。
This is to allow 'lazy' users who want to set'n'forget the parameters to do so.
这是为了允许“懒惰”的用户想要设置'n'忘记这些参数。
It also allows over-riding of those parameters in special cases by the command line.
它还允许命令行在特殊情况下覆盖这些参数。
For those that don't want to take the chance that their parameters might be set up incorrectly, they can just use parameters.
对于那些不想错误地设置参数的人来说,他们可以只使用参数。
Sometimes I'll even have more levels in the hierarchy, in order of precedence:
有时我甚至会在层次结构中有更多级别,按优先顺序排列:
- Value set while program running.
- Command-line parameter.
- Environment variable.
- Local config file.
- Global config file.
- Default.
程序运行时的值。
本地配置文件。
全局配置文件。
That way, for each variable, you just work your way up that list, setting it to the relevant value, if it's there.
这样,对于每个变量,您只需沿着该列表工作,将其设置为相关值(如果存在)。
#2
I suggest you prefer command line parameters over environment vars unless
我建议您更喜欢命令行参数而不是环境变量
- you need values that persist across commands.
- you need system wide global values
你需要在命令中保持不变的值。
你需要全系统的全球价值观
Foredecker
#3
Always prefer parameters when you can.
总是喜欢参数。
But say you have Script1 that calls Script2. Both scripts are distributed to many users. Script2 may need different parameters depending on the user, and the users don't want or are unable to alter Script1 for some reason. This is a case where environment variables might be useful.
但是说你有Script1调用Script2。这两个脚本都分发给许多用户。 Script2可能需要不同的参数,具体取决于用户,并且用户不希望或由于某种原因无法更改Script1。这是环境变量可能有用的情况。
#1
In most of the scripts I write, I allow both with the command line parameters taking preference.
在我编写的大多数脚本中,我允许使用命令行参数优先。
This is to allow 'lazy' users who want to set'n'forget the parameters to do so.
这是为了允许“懒惰”的用户想要设置'n'忘记这些参数。
It also allows over-riding of those parameters in special cases by the command line.
它还允许命令行在特殊情况下覆盖这些参数。
For those that don't want to take the chance that their parameters might be set up incorrectly, they can just use parameters.
对于那些不想错误地设置参数的人来说,他们可以只使用参数。
Sometimes I'll even have more levels in the hierarchy, in order of precedence:
有时我甚至会在层次结构中有更多级别,按优先顺序排列:
- Value set while program running.
- Command-line parameter.
- Environment variable.
- Local config file.
- Global config file.
- Default.
程序运行时的值。
本地配置文件。
全局配置文件。
That way, for each variable, you just work your way up that list, setting it to the relevant value, if it's there.
这样,对于每个变量,您只需沿着该列表工作,将其设置为相关值(如果存在)。
#2
I suggest you prefer command line parameters over environment vars unless
我建议您更喜欢命令行参数而不是环境变量
- you need values that persist across commands.
- you need system wide global values
你需要在命令中保持不变的值。
你需要全系统的全球价值观
Foredecker
#3
Always prefer parameters when you can.
总是喜欢参数。
But say you have Script1 that calls Script2. Both scripts are distributed to many users. Script2 may need different parameters depending on the user, and the users don't want or are unable to alter Script1 for some reason. This is a case where environment variables might be useful.
但是说你有Script1调用Script2。这两个脚本都分发给许多用户。 Script2可能需要不同的参数,具体取决于用户,并且用户不希望或由于某种原因无法更改Script1。这是环境变量可能有用的情况。