简体   繁体   English

别名中的双引号转义

[英]Double quotation mark escaping in alias

I am trying to set the following alias in Debian Stretch 我正在尝试在Debian Stretch中设置以下别名

alias myalias='watch -d -n 0.1 '\''find /path -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c'\'''

I have tried to escape the first set of quotation marks with '\\'' but it doesn't work with the deeper marks around 我试图用'\\'来转义第一组引号,但它不适用于较深的引号

%TY-%Tm-%Td\n

When I run the command, I end up with the following output. 运行命令时,最终得到以下输出。 The quotation marks around the %TY-%Tm-%Td\\n being not there anymore, the output does not interpret the \\n and the result is on one line. %TY-%Tm-%Td \\ n周围的引号不再存在,输出不解释\\ n并且结果在一行上。

Every 0.1s: find /root/bolero/bolero/pkl/stocks -type f -printf %TY-%Tm-%Td\n | sort | uniq -c

Any idea to make this work? 有什么想法可以使这项工作吗?

The output you're looking for is this: 您正在寻找的输出是这样的:

alias myalias='watch -d -n 0.1 '\''find /path -type f -printf '\''\'\'''\''%TY-%Tm-%Td\n'\''\'\'''\'' | sort | uniq -c'\'''

This is, of course, enormously complicated. 当然,这非常复杂。

Because nobody wants to count quotation marks, let me introduce you to a feature of Git you may not have known about: git rev-parse --sq-quote . 因为没有人想计算引号,所以让我向您介绍您可能不了解的Git功能: git rev-parse --sq-quote If you want to know how text would be properly single-quoted, instead double-quote that portion and pass it to git rev-parse --sq-quote . 如果您想知道如何将文本正确地单引号,请将该部分双引号,然后将其传递给git rev-parse --sq-quote So incrementally, it looks like this: 因此,它看起来像这样:

$ git rev-parse --sq-quote "find /path -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c"
 'find /path -type f -printf '\''%TY-%Tm-%Td\n'\'' | sort | uniq -c'
$ git rev-parse --sq-quote "watch -d -n 0.1 'find /path -type f -printf '\''%TY-%Tm-%Td\n'\'' | sort | uniq -c'"
 'watch -d -n 0.1 '\''find /path -type f -printf '\''\'\'''\''%TY-%Tm-%Td\n'\''\'\'''\'' | sort | uniq -c'\'''

And that's how you get your result. 这就是您获得结果的方式。 Note that Git will insert a leading space on the line, which you may want to remove for tidiness. 请注意,Git会在行上插入一个前导空格,您可能需要将其删除以保持整洁。

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

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