如何传递带有特殊字符的参数来调用shell脚本

发布于 2025-01-15 22:34:53 字数 359 浏览 1 评论 0原文

使用所需参数调用 .sh(shell 脚本),如下所示:-

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

使用参数 dbUsername 和密码调用 example.sh,但出现以下错误:-

-bash: !ple#####: 未找到事件

我认为特殊字符限制了要执行的命令。那么我必须如何传递特殊字符。任何帮助都将非常重要。

Calling .sh(shell script) with the required parameters as below :-

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

calling example.sh with paramters dbUsername and password but getting following error:-

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

I think special characters restrict the command to execute. Then how i have to pass the special characters. Any help will be appreciable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2025-01-22 22:34:53

将行更改

dbPassword=exam!ple#####

为,

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

以避免在 bash 中对 ! (历史扩展)进行特殊处理,

来自 QUOTINGman bash代码>小节,

当使用命令历史扩展功能时(请参阅下面的历史扩展),必须引用历史扩展字符(通常是 !)以防止历史扩展。

历史扩展下有更多内容

历史扩展由
历史资料片角色登场!经过
默认。只有反斜杠(\)和单引号可以引用历史记录
扩展字符。

另外,最好引用所有名称-值对,以防止 shell 进行分词。

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

关于分词,来自 man 页面,

分词

shell 扫描未出现在双引号内的参数扩展、命令替换和算术扩展的结果以进行分词。 shell 将 IFS 的每个字符视为分隔符,并使用这些字符作为字段终止符将其他扩展的结果拆分为单词

Change the line,

dbPassword=exam!ple#####

to,

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

to avoid ! (history-expansion) being treated specially in bash

From man bash under QUOTING sub-section,

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.

more under 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.

Also, it is a good practice to quote all your name-value pairs to prevent Word-splitting done by shell.

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

About word-splitting, from the man page,

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

天暗了我发光 2025-01-22 22:34:53

像这样通过:

exam\!ple\#\#\#\#\#

测试:

echo exam\!ple\#\#\#\#\#

Pass it like this:

exam\!ple\#\#\#\#\#

Test:

echo exam\!ple\#\#\#\#\#
你是暖光i 2025-01-22 22:34:53

您可以执行以下两件事:

  1. 用反斜杠转义每个特殊符号

    sh home/example.sh --context_param dbUserName=用户名 --context_param dbPassword=exam\!ple\#\#\#\#\# --context_param resultDate=2017-01-13
    
  2. 单引号整个参数

    sh home/example.sh --context_param dbUserName=用户名 --context_param dbPassword='考试!ple#####' --context_param resultDate=2017-01-13
    

来自man bash

引用

引用用于删除 shell 中某些字符或单词的特殊含义。引用可用于禁用
对特殊字符进行特殊处理,以防止保留字被识别为此类,并防止参数扩展。

当使用命令历史扩展功能时,必须引用历史扩展字符,通常是!,以防止历史扩展。

共有三种引用机制:转义符单引号双引号

不带引号的反斜杠 (\) 是转义字符。它保留后面的下一个字符的字面值,但例外
<换行>。如果出现 \ 对,并且反斜杠本身未加引号,则 \ 被视为行延续
(也就是说,它被从输入流中删除并被有效地忽略)。

将字符括在单引号中会保留引号内每个字符的字面值。单引号可能不会出现
在单引号之间,即使前面有反斜杠。

将字符括在双引号中会保留引号内所有字符的字面值,但 $,`,\ 除外并且,当启用历史扩展时,!。字符 $ 和 ` 在双引号内保留其特殊含义。反斜杠
仅当后跟以下字符之一时才保留其特殊含义:$、`、"\;。双引号内可以加反斜杠,如果启用,则将执行历史扩展,除非 ! 出现在双引号中。
引号使用反斜杠转义。 ! 前面的反斜杠不会被删除。

特殊参数*@在双引号中具有特殊含义。

$'string' 形式的单词会被特殊处理。该单词扩展为字符串,并按照 ANSI C 标准指定的方式替换反斜杠转义字符。

反斜杠转义序列(如果存在)按如下方式解码:

 \警报(响铃)
          \b 退格键
          \e
          \E 转义字符
          \f 换页
          \n 新行
          \r 回车
          \t 水平制表符
          \v 垂直制表符
          \\ 反斜杠
          \' 单引号
          \" 双引号
          \nnn 八位字符,其值为八进制值 nnn(一到三位数)
          \xHH 八位字符,其值为十六进制值 HH(一位或两位十六进制数字)
          \uHHHH Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHH(一到四个十六进制数字)
          \呃呃呃呃呃
                 Unicode (ISO/IEC 10646) 字符,其值为十六进制值 HHHHHHHH(一到八个十六进制数字)
          \cx 一个 control-x 字符

扩展结果是单引号的,就好像美元符号不存在一样。

前面带有美元符号 ($"string") 的双引号字符串将导致根据当前区域设置翻译该字符串。如果当前语言环境是 C 或 POSIX,则忽略美元符号。如果字符串被翻译并替换,则替换内容将用双引号引起来。

You can do the following two things:

  1. Escape every single special symbol with a backslash

    sh home/example.sh --context_param dbUserName=username --context_param dbPassword=exam\!ple\#\#\#\#\# --context_param resultDate=2017-01-13
    
  2. Singlequote the entire argument.

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

From man bash

QUOTING

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable
special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

When the command history expansion facilities are being used, the history expansion character,usually !,must be quoted to prevent history expansion.

There are three quoting mechanisms: the escape character, single quotes, and double quotes.

A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception
of <newline>. If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation
(that is, it is removed from the input stream and effectively ignored).

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $,`,\, and, when history expansion is enabled, !. The characters $ and ` retain their special meaning within double quotes. The backslash
retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double
quotes is escaped using a backslash. The backslash preceding the ! is not removed.

The special parameters * and @ have special meaning when in double quotes.

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

Backslash escape sequences, if present, are decoded as follows:

          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double quote
          \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
          \uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)
          \UHHHHHHHH
                 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)
          \cx    a control-x character

The expanded result is single-quoted, as if the dollar sign had not been present.

A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文