
我的終端有一個奇怪的行為。
以下命令不會在我的終端機上列印任何內容:
comment=$(
cat /usr/share/applications/brasero.desktop |
grep '^Comment=' |
grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'
)
echo $comment
完全相同的指令、相同的語法在 bash 腳本中完美運作。
通常,我看到人們抱怨相反的行為。
我知道上面的操作可以透過數百種方式完成,但我不明白為什麼上面的命令沒有在我的終端中運行。
另一方面,這些命令在終端機和腳本中都可以正常運作:
comment=$(
cat /usr/share/applications/brasero.desktop |
grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'
)
echo $comment
grep -Po '(?<=Exec=)[ --0-9A-Za-z/:space:]*' > /usr/share/applications/brasero.desktop
我在 Debian 8 Sid 下使用 GNU bash,版本 4.4.0(1)-release (x86_64-pc-linux-gnu),並使用 grep (GNU grep) 2.26。我已經在 xfce 和 mate 終端中嘗試過,但行為相同。
謝謝
答案1
當你遇到這樣的問題時,你應該開始從測驗中刪除一些東西,直到你得到有用的東西。
例如,如果您的起點是:
comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*');echo $comment
首先去掉變數的東西:
cat /usr/share/applications/brasero.desktop |grep '^Comment=' |grep -Po '(?<=^Comment=)[ --0-9A-Za-z/.]*'
然後剝掉第二個grep
:
cat /usr/share/applications/brasero.desktop |grep '^Comment='
然後是另一個 grep:
cat /usr/share/applications/brasero.desktop
最終你會找到它開始工作的點。在您的特定情況下,它將開始使用以下內容:
cat /usr/share/applications/brasero.desktop |grep '^Comment='
所以這就是grep
問題所在。添加其他東西回來看看它是否仍然有效:
comment=$(cat /usr/share/applications/brasero.desktop |grep '^Comment=');echo $comment
確實如此。所以有什麼問題?好吧,請注意最後一個輸出是彩色的嗎?輸出中包含 ANSI 顏色轉義代碼。這會阻止第二個grep
匹配。
解決方法是將grep
別名更改為--color=auto
、 或 do unalias grep
,或手動新增--color=never
到命令中。
還有另一種方法可以解決這個問題。既然您知道它是在腳本內工作的,那麼唯一的解釋就是它是環境性的。
因此,您可以從一個乾淨的環境開始,bash --noprofile --norc
然後開始一點一點地引入您的配置文件,直到它崩潰。
但這種技術有個問題。即使使用--noprofile --norc
,CLI 和腳本之間也存在一些差異,例如歷史擴充和作業控制。您也可以將它們關閉:set +H
和set -m
。