简体   繁体   English

在bash别名文件中转义带引号的字符串

[英]Escaping a string with quotes in bash alias file

I'd like to put the following command in my ~/.bash_aliases file: 我想在我的~/.bash_aliases文件中输入以下命令:

grep screen /var/log/auth.log|grep "$(date|awk '{print $2" "$3}')"

I've tried putting the following in the alias file: 我尝试将以下内容放入别名文件中:

alias unlocks=grep screen /var/log/auth.log|grep "$(date|awk '{print $2" "$3}')"

but I get the following: 但我得到以下信息:

bash: alias: screen: not found bash:别名:屏幕:找不到

bash: alias: /var/log/auth.log: not found bash:别名:/var/log/auth.log:找不到

Presumably, this is because of the spaces in the command string, which would normally be solved by quoting the entire string, but I'm already using two types of quotes and I can't work out how to escape them correctly. 大概是因为命令字符串中有空格,通常可以通过引用整个字符串来解决空格,但是我已经使用了两种类型的引号,而且我无法弄清楚如何正确地对它们进行转义。

Please can someone help? 请有人帮忙吗?

Don't try to make something this complex an alias. 不要试图将复杂的东西作为别名。 Define a function in .bashrc instead. 而是在.bashrc定义一个函数。

unlocks () {
    grep screen /var/log/auth.log | grep "$(date +"%b %d")"
}

Also, don't use awk when you can just adjust the output of date directly. 另外,当您可以直接调整date的输出时,请不要使用awk However, you can use awk to replace both instances of grep : 但是,您可以使用awk替换两个grep实例:

unlocks () {
     awk -v date=$(date +"%b %d") '/screen/ && $0 ~ date' /var/log/auth.log
}

alias foo="my command with\\"quotes\\"" should work most of the time alias foo="my command with\\"quotes\\""大多数情况下, alias foo="my command with\\"quotes\\""应正常工作

EDIT: As bash will evaluate the string, you also need to escape other special characters like $ 编辑:由于bash将评估字符串,因此您还需要转义其他特殊字符,例如$

In your case: alias unlock="grep screen /var/log/auth.log|grep \\"\\$(date|awk '{print \\$2\\" \\"\\$3}')\\"" should do the trick. 在您的情况下: alias unlock="grep screen /var/log/auth.log|grep \\"\\$(date|awk '{print \\$2\\" \\"\\$3}')\\""应该可以解决问题。

There is a simple way and a hard way. 有一个简单的方法和一个困难的方法。

The simple way: Put the code into a script file called unlocks inside the folder $HOME/bin . 最简单的方法:把代码放到称为脚本文件unlocks里面的文件夹$HOME/bin In most systems, that folder gets added to the path. 在大多数系统中,该文件夹被添加到路径中。 Use chmod +x $HOME/bin/unlocks to make the script executable. 使用chmod +x $HOME/bin/unlocks使脚本可执行。

The hard way: You can use nested quotes but it's ugly, brittle and ... I don't recommend it. 困难的方法:您可以使用嵌套的引号,但是它很难看,易碎,而且...我不建议这样做。 But if you must, it would look like this: 但是,如果必须的话,它将如下所示:

alias unlocks='grep screen /var/log/auth.log|grep "$(date|awk '"'"'{print $2" "$3}'"'"')"'

To get a single nested single quote in a single quoted string, you need '"'"' The first tick ends the current string. 要在单个带引号的字符串中获得单个嵌套的单引号,您需要'"'"'第一个对号结束当前字符串。 The "'" gives you a single quote which the shell will append the current string. "'"为您提供单引号,shell将在其后附加当前字符串。 The last tick then continues the string. 然后最后一个滴答声继续该字符串。

alias unlocks="..."

won't work in this case because it will immediately run date|awk '{print $2" "$3}' and put the result into the alias. 在这种情况下将不起作用,因为它将立即运行date|awk '{print $2" "$3}'并将结果放入别名中。

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

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