2008-12-20 115 views
3

我試圖運行下面的命令:轉義雙引號tcsh的別名

replace -x "must " A2input.txt 
replace -x " a" -f -s ## A2input.txt 
replace -x to -s ## -a A2input.txt 
replace -x faith -f "unequivocal" A2input.txt 

而且這將會是好的,如果我可以別名它的東西短期和簡單的像「A」,「B 「,」c「,」d「等...

但是,其中一些參數有一個引號,這是混淆了別名。有誰知道如何避免雙引號?我嘗試了'\''和\「之類的東西,但似乎沒有任何效果。

我使用tcsh作爲我的shell。

+0

最後兩個不需要引號。用單引號代替雙引號怎麼樣? – 2008-12-20 03:19:39

回答

5

我得到它的工作原理是將雙引號字符串存儲在變量中,字符串用單引號括起來。當我在單引號內使用變量I時。
例子:

 
[11:~] phi% 
[11:~] phi% set text = 'a quote "' 
[11:~] phi% alias ec echo '$text' 
[11:~] phi% ec 
a quote " 
[11:~] phi% 
[11:~] phi% alias ec echo this has '$text' 
[11:~] phi% ec 
this has a quote " 
[11:~] phi% 

我在OSX

+0

非常好。我現在在我的別名安裝腳本中使用`set q ='「」`。我通常可以弄清楚如何結合``,',\,...`來正確引用,但使用`$ {q}`更具可讀性! – 2017-08-21 18:26:49

0

如果你不能得到一個別名工作,只寫一個短的shell腳本,使用chmod + x,並把它的地方在你的$ PATH(如$ HOME /箱):

#!/bin/tcsh 
replace -x "must" ... 

我沒有與任何tcsh的經驗,但在bash你不喜歡它的部分包括:

alias t='echo "hello world"'  # using single quotes to enclose entire string 
alias t=echo\ \"hello\ \ world\" # escape " and <space> 
alias t="echo \"hello world\"" # double-quote + escape inner double quotes 

也許類似的東西會在tcsh的工作嗎?

6

與tcsh的測試,這下在tcsh所有的工作來完成各種結果:

 
alias t echo hello world    # you may not actually need any quotes 
alias u 'echo "hello world"'   # nested quotes of different types 
alias v echo\ \"hello\ world\"   # escape everything 
alias w echo '\;'hello'";"' world  # quote/escape problem areas only 
alias x 'echo \"hello world\"'   # single quote and escape for literal " 
alias y "echo "\""hello world"\"  # unquote, escaped quote, quote ("\"") 
alias z 'echo '\''hello world'\'  # same goes for single quotes ('\'') 

,以瞭解這些由shell來解釋,無參數運行alias

 
% alias 
t  (echo hello world) 
u  echo "hello world" 
v  echo "hello world" 
w  (echo \;hello";" world) 
x  echo \"hello world\" 
y  echo "hello world" 
z  echo 'hello world' 

圓括號中的任何內容都在子外殼中運行。如果你試圖設置環境變量,這會很糟糕,但是大多數情況下是不相關的。

最後,這裏就是例子實際上做:

 
% t; u; v; w; x; y; z 
hello world 
hello world 
hello world 
;hello; world 
"hello world" 
hello world 
hello world 
2

tcsh有一個新的變量backslash_quote。不知道何時添加,但在6.18.01(OS X El Capitan上的版本)和6.19(本文撰寫時的最新穩定版本)中受支持。這使得可以在引號內使用',"`

set backslash_quote 

set sentence = 'I\'m a little teapot.' 
set sentence2 = "The man said \"hello\"" 

如果你不想使用此選項,您的選擇是有限的使用不同類型的報價在使用所有的報價符號

"The man said "'"'"hello"'"' 

或不在身邊和寬鬆backslashing事情。

The\ man\ said\ \"hello\" 
0

這樣看來,那\"\'像您期望的不工作的原因是,傳統csh不支持這樣的語法,所以,如果tcsh是無條件地支持它,那麼這樣的老腳本可能無法正常工作了。

作爲另一個答覆中提到,TCSH本身所具有的set backslash_quote功能;然而,與上述答案中所隱含的不同,這不是一個新功能,它只是一個特定於tcsh的功能,實際上至少在大約27年前被添加到tcsh - 該功能首次記錄在手冊頁tcsh.man,v 3.8 1991/07/25 04:50:55,其中幾乎沒有資格成爲「新」。

http://mdoc.su/n,f,d/tcsh.1

backslash_quote (+) 
     If set, backslashes (`\') always quote `\', `'', and `"'. This 
     may make complex quoting tasks easier, but it can cause syntax 
     errors in csh(1) scripts.