直接输入时,以下行在 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
我尝试了多种引号组合(此处有以下信息),但没有成功。我错过了什么?
弄清楚了这个令人厌恶的引用:
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: " -> "\""