Shell-Script

使用 tee 將使用者輸入寫入文件

  • November 18, 2018

我有一個腳本可以指導使用者安裝我的軟體,我想寫一個日誌文件,以防發生不好的事情並且使用者需要支持。

腳本如下所示:

while true; do
 echo "This script will help you with the installation. Do you wish to proceed?"
 echo "(1) Yes"
 echo "(2) No"
 read proceed
 case $proceed in
   1 ) break;;
   * ) echo "Aborting."; exit 8;;
 esac
done
unset proceed

然後我通過 using 執行它./install.ksh | tee /var/log/myinstall.log,一切正常,但未記錄使用者對問題的輸入。當我echo $proceedread命令後添加時,它被寫入日誌但顯示兩次,如下所示:

This script will help you with the installation. Do you wish to proceed?
(1) Yes
(2) No
1 #this is the input which is not written to the log
1 # this is the echo which is written to the log

我現在的問題是如何抑制read命令的輸出,或者如何將echo唯一寫入文件而不寫入 STDOUT?

您應該改用script它,它正是為此目的而設計的:

script /var/log/myinstall.log -c ./install.ksh

它將記錄輸入read以及任何輸出。

引用自:https://unix.stackexchange.com/questions/429982