【问题标题】:File descriptors: Redirect both input and output to both shell and logfile文件描述符:将输入和输出都重定向到 shell 和日志文件
【发布时间】:2014-12-02 17:44:37
【问题描述】:

以下是我找到的解决方案: Create a pipe that writes to multiple files (tee)

因此,下面的代码将 stdout 和 stderr 重定向到日志文件,同时也在终端中显示它们。我想知道是否还有将标准输入重定向到终端和日志文件。

#!/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

【问题讨论】:

  • 终端不是shell。您不能在 shell 中“显示”某些内容,但 shell 可以将数据写入要显示的终端。
  • @William;使用正确的术语更新。

标签: bash shell pipe io-redirection


【解决方案1】:

可以使用脚本命令

script -c infile.sh
-c, --command 命令 运行命令而不是交互式 shell。这使得很容易 脚本来捕获程序的输出,该程序的行为不同时 标准输出不是 tty。

saving terminal session

explainshell.com - script -c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2016-10-07
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多