将 STDOUT 重定向到新的 window,将 STDERR 重定向到同一个新的 window 以及一个日志文件

Redirect STDOUT to new window, STDERR to same new window and also a log file

我在 bash 脚本(许多脚本之一)中有这个漫长的过程,所以我正在尝试将所有输出发送到单独的 window 进行监控,并将所有错误记录到 errorlog.txt。 例如:

rsync -vahPz foo@bar:/bigfolder/ ./ >> /dev/pts/4 2>> errorlog.txt

问题是上面没有在单独的 window.

上显示任何错误

有没有办法将错误重定向到我在 /dev/pts/4 errorlog.txt[= 的单独 window 27=],同时仍然将正常输出也重定向到 /dev/pts/4?

类似于:

rsyncblah >> /dev/pts/4 2>> errorlog.txt && /dev/pts/4

您可以将 teeprocess substitution 一起使用,如下所示:

your_cmd 1> >(tee -a /dev/pts/2 >> out.log) 2> >(tee -a /dev/pts/2 >> err.log)

或者,您可以仅对 stderr 使用进程替换 - 因为它是必需的 - 并像往常一样通过管道重定向 stdout:

your_cmd 2> >(tee -a /dev/pts/2 >> err.log) | tee -a /dev/pts/2 >> out.log

为 input/output

打开新的 window

1。创建一个新的 window 并捕获他的 pts id

exec {BACKFD}<> <(:)
xterm -e bash -c "exec {CLOSEFD}<> <(:);
                  echo >/proc/$$/fd/$BACKFD $$ $CLOSEFD $(tty);
                  read -u $CLOSEFD" &
read -u $BACKFD bpid bclose btty

与使用 gnome-terminal 相同:

exec {BACKFD}<> <(:)
gnome-terminal -- bash -c "exec {CLOSEFD}<> <(:);
                  echo >/proc/$$/fd/$BACKFD $$ $CLOSEFD $(tty);
                  read -u $CLOSEFD" &
read -u $BACKFD bpid bclose btty

xterm -e 将替换为:

gnome-terminal --  # for gnome
mate-terminal --   # for mate
konsole -e         # for KDE

等等...

注意 read -u $CLOSEFD 将确保新的 window 将保持打开状态,直到我们关闭它们。

2。玩新 window

printf >$btty '%s\n' "Hello world!"
sleep 2
dialog --gauge >$btty <$btty 'Fill the tank' 20 60 < <(
    for i in {0..100};do echo $i;sleep .02;done)
clear <$btty >$btty

if dialog --yesno >$btty <$btty 'Do this match?' 20 60
  then echo yes
  else echo no
fi
clear <$btty >$btty

answer=$(
  dialog --menu 'Choose on of:' 20 60 12 a foo b bar c baz 2>&1 >$btty <$btty
)
echo You choose: $answer
clear <$btty >$btty

3。根据要求,在日志文件和新 window:

中共享命令的输出
exec {SHAREDOUT}> >(tee $btty >>/path/to/logfile)
exec {SHAREDERR}> >(tee $btty >>/path/to/errfile)

ls -ld /t{mp,nt} 2>&${SHAREDERR} >&${SHAREDOUT}

rsync -vahPz --log-file=/proc/self/fd/$SHAREDOUT 2>&${SHAREDERR} \
    foo@bar:/bigfolder/. /destpath/.

4。关闭 window:

echo >/proc/$bpid/fd/$bclose