简体   繁体   English

将字符串作为命令行参数传递时,转义如何工作?

[英]How escaping works when passing a string as command line argument?

I am passing date in this format 02/03/2013 to awk script, I need to get all of the records matching this date, If I do it directly without using the script I have to escape the forwards slash 我正在将这种格式的日期02/03/2013传递给awk脚本,我需要获取与该日期匹配的所有记录,如果我不使用脚本直接进行操作,则必须转义正斜线

awk '$4 ~ /02\/03\/2013/ {print $1}' 

However I dont have to escape it in case of it being sent as a command line argument,I ran the script using 02/03/2013 and awk command changed to accomodate it. 但是我不必逃避它作为命令行参数发送的情况,我使用02/03/2013运行了脚本,并更改​​了awk命令以适应它。

Could anyone explain to me How does the escaping of character work when we sending the string as command line argument? 有人可以向我解释当我们将字符串作为命令行参数发送时,转义字符如何工作吗? What are precaution to be taken for harmful input? 有害输入应采取哪些预防措施?

Any online resources related to this would be really appreciated. 任何与此相关的在线资源将不胜感激。

The forward slash is not a special character in the regular expression. 正斜杠不是正则表达式中的特殊字符。 The reason it needs to be escaped is because the regex constant uses forward slashes itself as demarcation. 之所以需要对其进行转义,是因为正则表达式常量将正斜杠本身用作标界。 With the use of a string that is interpreted as a regex, these are not used so that slashes do not need to be escaped.. 通过使用解释为正则表达式的字符串,不使用这些字符串,因此不需要转义斜线。

awk '$4~dd{print $1}' dd=02/03/2013

Instead of regex matching you could perhaps use exact matching 除了正则表达式匹配之外,您还可以使用完全匹配

awk '$4==dd{print $1}' dd=02/03/2013

You can use parameter expansion in bash: 您可以在bash中使用参数扩展:

date=02/03/2013
date=${date//\//\\/}            # Backslash all slashes.
awk "\$4 ~ /$date/ {print \$1}" # Prevent bash from expanding $1.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM