bash bash_aliases 和 awk 转义引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14560796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:22:38  来源:igfitidea点击:

bash_aliases and awk escaping of quotes

bashawkaliasquote

提问by Massagran

I'm trying to create an alias for a command to see the memory use,

我正在尝试为命令创建别名以查看内存使用情况,

ps -u user -o rss,command | grep -v peruser | awk '{sum+=} END {print sum/1024}'

but, the naive,

但是,天真,

#.bash_aliases
alias totalmem='ps -u user -o rss,command | grep -v peruser | awk '{sum+=} END {print sum/1024}''

gives errors:

给出错误:

-bash: alias: END: not found
-bash: alias: {print: not found
-bash: alias: sum/1024}: not found

I've tried with double quotes,

我试过用双引号,

totalmem ="ps ... |awk '{sum+=$1} END {print sum/1024}'", or

totalmem ="ps ... |awk '{sum+=$1} END {print sum/1024}'", 或者

totalmem ='ps ... |awk "{sum+=$1} END {print sum/1024}"', escaping,

totalmem ='ps ... |awk "{sum+=$1} END {print sum/1024}"', 逃脱,

totalmem ='ps ... |awk \'{sum+=$1} END {print sum/1024}\'', or escaping double quotes ... but I can't seem to make it work.

totalmem ='ps ... |awk \'{sum+=$1} END {print sum/1024}\'',或转义双引号......但我似乎无法让它工作。

totalmem ='ps ... |awk \"{sum+=$1} END {print sum/1024}\"',

totalmem ='ps ... |awk \"{sum+=$1} END {print sum/1024}\"',

gives the error

给出错误

awk: "{sum+=}
awk: ^ unterminated string

Any tips appreciated.

任何提示表示赞赏。

回答by chepner

You can avoid quoting issues by using a shell function instead of an alias:

您可以通过使用 shell 函数而不是别名来避免引用问题:

totalmem () {
  ps -u user -o rss,command | grep -v peruser | awk '{sum+=} END {print sum/1024}'
}

This is also more flexible, as you could allow totalmemto take arguments, such as a user name to pass to the -uoption of ps, as in this example:

这也更灵活,因为您可以允许totalmem采用参数,例如传递给-u选项的用户名ps,如下例所示:

totalmem () {
  ps -u "" -o rss,command | grep -v peruser | awk '{sum+=} END {print sum/1024}'
}

回答by Thor

You almost have it, the $will be expanded in double-quotes, so that needs extra escaping:

你几乎拥有它,$将用双引号扩展,所以需要额外的转义:

alias totalmem='ps -u user -o rss,command | grep -v peruser | awk "{sum+=$1} END {print sum/1024}"'

Or with the pattern inside awkas suggested by iiSeymour:

或者awk按照iiSeymour 的建议使用内部模式:

alias totalmem='ps -u user -o rss,command | awk "!/peruser/ {sum+=$1} END {print sum/1024}"'

回答by Anton Kovalenko

Like this:

像这样:

alias totalmem='ps -u user -o rss,command | grep -v peruser | awk '\''{sum+=} END {print sum/1024}'\'

Explanation: you may use different kind of quotes for the same argument, like "I'm double-quoted"'and I am $HOME-less'-and-i-am-not-quoted. Hence if you need a single quote inside single quotes, you can add '\''which (1) terminates single quoting, (2) adds literal single quote with \', (3) starts single quoting again.

说明:您可以对同一个参数使用不同类型的引号,例如"I'm double-quoted"'and I am $HOME-less'-and-i-am-not-quoted. 因此,如果您需要单引号内的单引号,您可以添加'\''which (1) 终止单引号,(2) 添加文字单引号\', (3) 再次开始单引号。

(Aliases of this size are something that's better done as functions).

(这种大小的别名最好作为函数来完成)。