文件描述符:将输入和输出重定向到shell和logfile

时间:2014-12-02 17:44:38

标签: bash shell pipe io-redirection

以下是我发现的解决方案: 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

1 个答案:

答案 0 :(得分: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