如何传递带有特殊字符的参数以调用Shell脚本 [英] How to pass arguments with special characters to call shell script

查看:429
本文介绍了如何传递带有特殊字符的参数以调用Shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

推荐答案

更改行,

dbPassword=exam!ple#####

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

避免在bash

QUOTING子节下的man bash

在使用命令历史记录扩展工具时(请参见下面的"HISTORY EXPANSION"),必须用引号括起来,通常是!,以防止历史记录扩展.

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

此外,最好引用所有name-value对,以防止shell进行单词拆分.

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,

分词

shell扫描参数扩展,命令替换和算术扩展的结果,这些结果在双引号中没有出现用于分词的情况.外壳将IFS的每个字符视为定界符,并使用这些字符作为字段终止符将其他扩展结果拆分为单词

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

这篇关于如何传递带有特殊字符的参数以调用Shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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