是否可以在像 C 这样的 shell bash 函数中将变量定义为静态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20883981/
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
Is it possible to define a variable as static in shell bash function like C?
提问by MOHAMED
In C, I can define a static variable in a function like this
在 C 中,我可以在这样的函数中定义一个静态变量
int func() {
static int var=0
.....
}
Is there something equivalent to that in shell bash linux?
是否有与 shell bash linux 中相同的东西?
Is it possible to define a local variable of the bash shell function as static?
是否可以将 bash shell 函数的局部变量定义为静态?
回答by Basile Starynkevitch
With bash
you cannot really get that (I imagine you want some variable shared between several instances of your shell...). However, if you switch to the fishshell (use chsh
to change your login shell), you get so called universalvariables which kind-of fits the bill. See also this answerto a related question.
随着bash
你不能真正得到那个(我想你想你的shell的多个实例之间共享一些变量...)。但是,如果您切换到fishshell(用于chsh
更改您的登录shell),您会得到所谓的通用变量,这有点符合要求。另请参阅相关问题的答案。
BTW, you should read advanced bash scripting guideand consider using bash
functions (instead of a script).
顺便说一句,您应该阅读高级 bash 脚本指南并考虑使用bash
函数(而不是脚本)。
If you just want to share a variable between several shell functions inside the sameshell process, just don't declare it local
to functions!
如果您只想在同一个shell 进程内的多个 shell 函数之间共享一个变量,请不要将其声明local
为函数!
回答by Akhilesh Singh
What are you planning to actually achieve? If it is something like a variable which can store values between shell script executions, you can export the variable using export command. This will make it a globally accessible environment variable.
您打算实际实现什么目标?如果它类似于可以在 shell 脚本执行之间存储值的变量,则可以使用 export 命令导出该变量。这将使它成为一个全局可访问的环境变量。
As is the case with statics, you will have to make sure nobody else is updating it except your code.
与静态的情况一样,您必须确保除了您的代码之外没有其他人正在更新它。
回答by Devolus
If you want to use a variable in several scripts being executed, you must either export it in the shell, or you must source it.
如果要在多个正在执行的脚本中使用一个变量,则必须在 shell 中将其导出,或者必须将其作为源。
. myscript
Where myscript
defines some variables which will exist afterwards in the shell as well.
Wheremyscript
定义了一些变量,这些变量随后也将存在于 shell 中。
回答by Kevin Ruffus
The only reason to have a static variable in bash would be to protect it from "bad coding", and if that becomes a problem, you're not testing at each step of the way like you should be.
在 bash 中有一个静态变量的唯一原因是保护它免受“错误编码”的影响,如果这成为一个问题,你就不会像你应该的那样在每一步进行测试。
Just use a pre-defined global variable, and keep two copies of the script, ie: TestScript and Script. When you've tested the changes to TestScript, copy and paste the changes into Script.
只需使用预定义的全局变量,并保留脚本的两个副本,即:TestScript 和 Script。当您测试了对 TestScript 的更改后,将更改复制并粘贴到 Script 中。
You should also be doing all of your testing when possible in a VM.
如果可能,您还应该在 VM 中进行所有测试。
I've done this and I've now got a script that customizes a barebones Ubuntu 16.04 LTS server to include a clients new hostname, fqdn, email forwarding for root and webmaster, and installs and configures ownCloud, MariaDB 10, PHP 7, Apache 2, Webmin, mod_security, mod_evasive, tripwire, rkhunter, chkrootkit, PSAD, ClamAV, Logwatch, sets the firewall and file permissions, sets logfile rotation, resets the root password, generates Expect scripts to be called on the fly, truncates the logs and removes generated script files, then reboots the server for use.
我已经完成了这项工作,现在我有了一个脚本,可以自定义准系统 Ubuntu 16.04 LTS 服务器以包含客户端的新主机名、fqdn、root 和网站管理员的电子邮件转发,并安装和配置 ownCloud、MariaDB 10、PHP 7、Apache 2、Webmin、mod_security、mod_evasive、tripwire、rkhunter、chkrootkit、PSAD、ClamAV、Logwatch、设置防火墙和文件权限、设置日志文件轮换、重置root密码、生成要即时调用的Expect脚本、截断日志和删除生成的脚本文件,然后重新启动服务器以供使用。
I'll be adding additional functions to allow selecting Samba and LDAP as additional features during install, and modifying the install as needed.
我将添加附加功能以允许在安装期间选择 Samba 和 LDAP 作为附加功能,并根据需要修改安装。
Just takes a lot of time and a lot of testing.
只是需要大量的时间和大量的测试。