2014-12-02 65 views
1

下面是一個解決方案,我從實測值: Create a pipe that writes to multiple files (tee)文件描述符:重定向輸入和輸出到兩個殼和日誌文件

因此下面的代碼重定向stdout和stderr到日誌文件,而在終端中顯示它們。我想知道是否有無論如何將stdin重定向到終端和日誌文件。

#!/bin/bash 
LOG=./file.log 
PIPE=./logPipe 
mkfifo ${PIPE} 
exec 3>&1 4>&2 
tee -a ${LOG} <${PIPE} >&3 & 
exec 1>${PIPE} & 
exec 2>&1 

printf "%s\n" " Enter Something " 
read something 

printf "%s\n" " Enter Something " 
read something2 
printf "\n" 

printf "%s\n" " Enter Something " 
read something3 

mkdir ./test 
mkdir ./test 
mkdir ./test here 

printf "$something $something2 $something3" >> something.out 
+0

終端不是一個殼。您不能在shell中顯示某些內容,但外殼可以將數據寫入到要顯示的終端。 – 2014-12-02 19:24:57

+0

@William;使用正確的術語進行更新。 – Stelios 2014-12-02 23:24:04

回答

1

您可以使用腳本命令

script -c infile.sh 
 
-c, --command command 
    Run the command rather than an interactive shell. This makes it easy for a 
    script to capture the output of a program that behaves differently when its 
    stdout is not a tty. 

saving terminal session

explainshell.com - script -c