2012-06-09 63 views
2

任何人都可以解釋以下內容嗎?當我使用ssh遠程執行歷史記錄時,不會產生輸出,甚至其他遠程命令也會產生輸出,並且在該機器上有歷史記錄。ssh host'history |尾巴'產生沒有輸出

ssh host 'history | tail' # produces no output 
ssh host 'echo foo | tail' # produces 'foo' as output 
ssh host 
> history | tail # produces expected output 

回答

4

未加載非交互式shell的歷史記錄。您可以選擇tail歷史記錄文件(~/.bash_history)或打開歷史記錄並加載它。

set -o history 
history -r 

從遠程主機爲此所需要的完整的命令如下:

ssh host 'HISTFILE=~/.bash_history; history -r; history' | tail 
+0

輝煌。你能澄清一下「歷史-r」是什麼嗎?謝謝。 –

+1

@GirishRao:'help history'表示'history -r'讀取歷史文件並將其內容追加到歷史列表中。這就是爲什麼我在回答中說「加載它」。 –

+0

奇怪:我注意到'ssh -t localhost「HISTFILE =〜/ .bash_history; set -o history; export HISTTIMEFORMAT ='%F%T'; history -r; history」'沒有給我正確的時間戳;它只是填充所有行的當前時間戳 - 以任何方式獲得正確的時間? – rupert160