2014-04-30 38 views
1

我正在編寫一個需要大量用戶輸入的腳本,然後通過該輸入創建一個文件。一些輸入是相當重複的,所以我想啓用向上箭頭功能來包含已經輸入的字符串。我很難找到如何做到這一點。如何使用用戶輸入作爲歷史記錄

我已經試過

set -o history 

,但只給了我實際運行的命令,而不是已收到的意見。我也知道

"\e[A" 

是向上的箭頭命令,但這是我得到的。測試腳本如下:

#!/bin/bash 

set -o history 

read -e date 
read -e date2 
read -e date3 

echo $date $date2 $date3 

輸入$date後,我希望能夠向上箭頭並獲得$date內容用於$date2。有任何想法嗎?

回答

3

你可以試試這個小片段播放:

#!/bin/bash 

HISTFILE=myhistoryfile 
history -r 
for i in {1..3}; do 
    read -ep "Enter date $i: " d 
    history -s "$d" 
done 
history -w 
echo "Thanks for using this script. The history is:" 
history 

我們定義的文件作爲歷史文件:myhistoryfile,並將其與history -r加載到內存中。然後,我們進入循環和可變d每個用戶輸入後,我們執行

history -s "$d" 

到輸入d追加到當前歷史記錄。在循環結束時,我們執行

history -w 

實際將其寫入歷史文件。

嘗試不同的組合:使用/不使用history -shistory -w以瞭解他們真的在做什麼。和read the manual

+1

這工作。我把那本手冊拉起來了,但是我沒有看到'''-s'''命令。 – NightHallow