为什么使用“set var = value"获取脚本?打破$@? [英] Why does sourcing a script with "set var = value" break $@?

查看:26
本文介绍了为什么使用“set var = value"获取脚本?打破$@?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Centos VM 上配置自动注销.我注意到,如果我在/etc/profile.d/autologout.sh 创建一个文件,文件中只有 set autologout = 30 ,那么它会破坏任何来源/etc/的脚本的传递参数个人资料.

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.

显示此内容的示例脚本是:

A sample script that shows this is:

#!/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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆