bash 在 shell 脚本中使用只读变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13404665/
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-18 03:48:15  来源:igfitidea点击:

Use of read-only variables in shell scripts

linuxbashshellunix

提问by user1812379

Is it good shell programming practice to use read-only variables whenever possible or does it have any drawbacks? E.g. if I wanted to write some script that consists of multiple script files that make use of immutable file paths, would it make sense to declare the paths like that:

尽可能使用只读变量是好的 shell 编程习惯还是它有任何缺点?例如,如果我想编写一些由多个脚本文件组成的脚本,这些脚本文件使用不可变的文件路径,那么声明这样的路径是否有意义:

readonly LOGS
export LOGS 
LOGS="/some/path"

Another question: Is it a good idea to split monolithic and tedious too read shell script code into separate files? Many thanks for your answers.

另一个问题:将单片和冗长乏味的 shell 脚本代码拆分成单独的文件是个好主意吗?非常感谢您的回答。

回答by Mark Reed

It sounds like you might think that readonlydoes more than it really does. For one thing, readonly status is not exported into the environment or inherited by child processes:

听起来您可能认为这readonly比实际做的要多。一方面,只读状态不会导出到环境中,也不会被子进程继承:

$ declare -rx LOGS=hello
$ LOGS=goodbye
bash: LOGS: readonly variable
$ bash -c 'echo "$LOGS"'
hello
$ bash -c 'LOGS=goodbye; echo "$LOGS"'
goodbye
$ 

回答by RonaldBarzell

Generally speaking, using read only variables (in any language) and modularizing your program (in any language) is a good thing.

一般来说,使用只读变量(任何语言)和模块化程序(任何语言)是一件好事。

Read only variables protect against a common source of bugs, and helps improve readability and maintainability. Knowing that you can rely on a value of a variable enables you to reason better about your program, and to make assumptions about that variable later on -- things you couldn't do if the variable were mutable.

只读变量可防止常见错误来源,并有助于提高可读性和可维护性。知道您可以依赖变量的值使您能够更好地推理您的程序,并在以后对该变量进行假设——如果变量是可变的,您就无法做到这一点。

Modularization improves maintainability and re-usability. More modules generally means more fine-grained units that can find re-use in different circumstances, shorter code that's easier to read, and if your modules are independent, less interactions among parts that could wreck a modification.

模块化提高了可维护性和可重用性。更多的模块通常意味着更细粒度的单元可以在不同的情况下重用,更短的代码更容易阅读,如果你的模块是独立的,那么可能破坏修改的部分之间的交互就会减少。

回答by JRFerguson

A classic use of read-only variables is with TMOUT. Setting this variable to a non-zero value will logout an interactive terminal session after TMOUTseconds of inactivity (i.e. no keyboard input). To defeat a smart user from overriding the setting, use readonly:

只读变量的经典用法是使用TMOUT. 将此变量设置为非零值将在TMOUT几秒钟不活动(即没有键盘输入)后注销交互式终端会话。要阻止聪明的用户覆盖设置,请使用readonly

readonly TMOUT=60

Having done this, there is no way to do:

这样做了,就没有办法了:

export TMOUT=0

回答by cdleonard

I don't think readonly variables in bash are useful. I can't think of any issue I've seen that could have been prevented by making a variable readonly. Such limitations goes against the dynamic nature of bash. There are other more common causes of issues (like mispellings or forgetting to declare variables as local) which are impossible to prevent anyway.

我认为 bash 中的只读变量没有用。我想不出我见过的任何可以通过将变量设置为只读来防止的问题。这种限制与 bash 的动态特性背道而驰。还有其他更常见的问题原因(如拼写错误或忘记将变量声明为本地变量)无论如何都无法预防。

If you want to split things off try to only split "functions", not just chunks of code. It's easier to reuse a small thing if you know "source ~/myscript.sh" doesn't actually do anything.

如果您想拆分事物,请尝试仅拆分“功能”,而不仅仅是代码块。如果您知道“source ~/myscript.sh”实际上没有做任何事情,那么重用小东西会更容易。