系统相关
首页 > 系统相关> > linux – 如何使用特殊字符传递参数来调用shell脚本

linux – 如何使用特殊字符传递参数来调用shell脚本

作者:互联网

使用如下所需的参数调用.sh(shell脚本): –

sh home/example.sh --context_param dbUserName=username --context_param dbPassword=exam!ple##### --context_param resultDate=2017-01-13

使用参数dbUsername和password调用example.sh但是出现以下错误: –

-bash: !ple#####: event not found

我认为特殊字符限制命令执行.然后我如何传递特殊字符.任何帮助都会很明显.

解决方法:

改变线,

dbPassword=exam!ple#####

至,

dbPassword='exam!ple#####'

避免 ! (历史扩张)在bash中被特别对待

从QUOTING子节下的man bash,

When the command history expansion facilities are being used (see HISTORY EXPANSION below), the history expansion character, usually !, must be quoted to prevent history expansion.

在历史扩张下更多

History expansions are introduced by
the appearance of the history expansion character, which is ! by
default. Only backslash (\) and single quotes can quote the history
expansion character.

此外,最好引用所有名称 – 值对以防止shell完成分词.

sh home/example.sh --context_param dbUserName="username" --context_param dbPassword='exam!ple#####' --context_param resultDate="2017-01-13"

关于分词,从手册页,

Word Splitting

The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting. The shell treats each character of IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators

标签:quoting,bash,linux,shell
来源: https://codeday.me/bug/20190926/1819538.html