.cshrc 以复杂别名引用

问题描述 投票:0回答:1

直接输入时,以下行在 tcsh shell 中有效:

df -P . | awk -F: 'NR==2 {print "//"$1}' | awk '{printf "%s/%s\n", $0, ENVIRON["PWD"]}' | awk '{gsub("/home/", ""); gsub("/", "\\"); print}'

但是当我尝试在 .cshrc 中使用 in 来创建别名时:

alias foo " df -P . | awk -F: 'NR==2 {print "//"$1}' | awk '{printf "%s/%s\n", $0, ENVIRON["PWD"]}' | awk '{gsub("/home/", ""); gsub("/", "\\"); print}' "

最外面的双引号似乎破坏了东西:

> foo
awk: cmd. line:1: {printf %s/%sn, /usr/bin/tcsh, ENVIRON[PWD]}
awk: cmd. line:1:         ^ syntax error
awk: cmd. line:1: {gsub(/home/, ); gsub(/, \); print}
awk: cmd. line:1:               ^ syntax error
awk: cmd. line:1: {gsub(/home/, ); gsub(/, \); print}
awk: cmd. line:1:               ^ 0 is invalid as number of arguments for gsub

我尝试了多种引号组合(此处有以下信息),但没有成功。我错过了什么?

linux tcsh
1个回答
0
投票

弄清楚了这个令人厌恶的引用:

alias foo "df -P . | awk -F: 'NR==2 {print "\$"1}'| awk '{printf "\""//%s/%s\n"\"", "\$"0, ENVIRON["\""PWD"\""]}' | awk '{gsub("\""/home/"\"", "\"""\""); gsub("\""/"\"", "\""\\"\""); print}' "

关键是替换:

$ -> "\$" 
within single-quoted awk: " -> "\""
© www.soinside.com 2019 - 2024. All rights reserved.