为什么要采购具有“ set var = value”的脚本?打破$ @? [英] Why does sourcing a script with "set var = value" break $@?
问题描述
我正在尝试在Centos VM上配置自动注销。我已经注意到,如果我在/etc/profile.d/autologout.sh中创建一个文件,并且该文件中仅 set autologout = 30
,那么它将破坏任何源于/ etc / profile的脚本。
I am trying to configure auto logout on my Centos VM. I have noticed that if I create a file at /etc/profile.d/autologout.sh with only set autologout = 30
in the file, then it breaks passing arguments for any script that sources /etc/profile.
示例脚本显示如下:
#!/bin/bash
source /etc/profile
echo ${@}
当我运行它时,脚本只看到参数 autologout 30,并且没有任何参数,我试图在运行时传递它。
When I run it, the script only sees the arguments "autologout 30" and it doesn't get any parameter I try to pass it when running it.
无论自动注销脚本的名称,属性的名称如何,或者是否我设置了 set autologout 30
,都会发生这种情况。
This occurs regardless of the name of the autologout script, the name of the property, or if I have set autologout 30
instead.
有人可以解释发生了什么吗?好像autologout.sh劫持了参数。我对正在发生的事情不知所措,研究profile.d和set命令什么也没发现。
Can someone please explain what is happening? It is as if autologout.sh is hijacking the arguments. I am at a loss for what is happening, and researching profile.d and the set command has turned up nothing.
推荐答案
set
不能用于修改POSIX兼容shell中的shell变量。而是在给定位置参数时,它会修改命令行参数列表。
set
is not used to modify shell variables in POSIX-compliant shells. Rather, when given positional arguments, it modifies the command-line argument list.
如果只想为变量赋值,请不要使用 set
。相反,您的文件应该只包含:
If you just want to assign a value to a variable, don't use set
. Instead, your file should just contain:
autologout=30
...和 $ @
将保持其原始状态。
...and "$@"
will remain in its original state.
这篇关于为什么要采购具有“ set var = value”的脚本?打破$ @?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!