在 shell 脚本中传递时参数被更改

Parameter gets changed when passed in shell script

我正在使用 ksh shell。我必须从定义的别名中获取信息,我使用下面的代码

#!/usr/bin/ksh
source ~/.profile
echo "parameter : "
alias | grep hello

我已经将上面的脚本保存为test.sh 或 运行 test.sh,我的输出低于 :

./test 324 
parameter : autologout
hello='Hello, How are you'

我的问题是,当我将 324 作为参数传递时,为什么自动注销会显示为 ? 我该如何解决这个问题?

您可以在执行 source 之前保存参数 </code>,这可能会改变您的 <strong> 位置参数 </strong></p> <pre><code>#!/usr/bin/ksh arg1="" source ~/.profile echo "parameter : $arg1" alias | grep hello

确保您的 ~/.profile 中没有 set 命令。

#!/usr/bin/ksh
source ~/.profile
set "new"
echo "parameter : "
alias | grep hello

>./script.ksh foo    
parameter : new

现在您的参数将始终为 "new"。 请尝试注释掉 source 命令并查看错误是否仍然存在。如果是,则在“~/.profile”

中搜索 set 命令