![带有换行符、逗号和引号的 zsh 别名](https://linux22.com/image/64611/%E5%B8%A6%E6%9C%89%E6%8D%A2%E8%A1%8C%E7%AC%A6%E3%80%81%E9%80%97%E5%8F%B7%E5%92%8C%E5%BC%95%E5%8F%B7%E7%9A%84%20zsh%20%E5%88%AB%E5%90%8D.png)
在我的 zshrc 中,我有一个这样的别名:
alias sl='screen -list'
它符合我的需要(查看我正在运行的屏幕),但输出相当难看:
[pts/7]~% sl
There are screens on:
32765.quotes-api (04/26/2015 11:09:18 AM) (Detached)
5055.gitsync-test (04/07/2015 09:24:04 PM) (Detached)
15074.gitsync-interceptor (03/31/2015 10:39:45 AM) (Detached)
4662.eloquent-api (03/29/2015 11:37:26 AM) (Detached)
16177.Dropbox (03/17/2015 03:53:44 PM) (Detached)
18803.gitsync-todo-api-py (03/06/2015 08:21:24 AM) (Detached)
796.website (01/31/2015 01:56:02 PM) (Detached)
7874.gitsync-optionals (01/29/2015 02:27:24 PM) (Detached)
28474.linkbag (12/16/2014 09:56:39 AM) (Detached)
10839.datapump (10/13/2014 02:16:26 PM) (Detached)
5118.resr-api-python (09/13/2014 12:28:33 PM) (Detached)
7619.dataglobbing (09/03/2014 08:34:13 PM) (Detached)
10583.rest-api-dataglobbing (09/03/2014 01:06:21 AM) (Detached)
11705.save-functions (08/12/2014 01:00:58 PM) (Detached)
14 Sockets in /var/run/screen/S-tuvokki.
所以我开始格式化它并得到以下内容:
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'
但是我如何将其放入别名中,就像我之前使用的简单命令一样?
仅将所有内容放在一行上是行不通的。我试图转义引号,使用双引号而不是单引号给出解析错误。我还尝试将其包装在一个函数中,但 awk 命令似乎依赖于换行符,并且不喜欢所有指令都在一行上。
答案1
您可以通过三种方法解决这个问题。
一:只使用一个函数。别名用于简单的文本宏,而您的第二个示例则不是。
sl() {
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'
}
二:使用quote-line
widget正确转义整个命令
# type the entire command out like you would interactively.
% screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'
# use quote-line which transform the line into:
% 'screen -list|grep -v There|grep -v Sockets|awk '\''BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'\'''
# prepend alias sl= to the newly escaped line:
% alias sl='screen -list|grep -v There|grep -v Sockets|awk '\''BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'\'''
第三:只使用一个函数。别名用于简单的文本宏。
sl() {
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s\n"
printf format, "Name", "Active", "Status"
printf format, "----", "------", "------" }
{ printf format, $1, $2, $5 }'
}
awk 示例也不依赖换行符,但您需要;
在同一行上分隔多个语句。
screen -list|grep -v There|grep -v Sockets|awk 'BEGIN { format = " %-35s %-10s %s
"; printf format, "Name", "Active", "Status"; printf format, "----", "------", "------" } { printf format, $1, $2, $5 }'
将工作。