stderr重定向导致read命令无法打印输出

问题描述 投票:1回答:1

代码为

#!/bin/bash

exec 3>&1 4>&2
exec 1>/tmp/stdout.log
exec 2>/tmp/stderr.log
PS4='+ (Line $LINENO) '
set -x
echo "This goes to /tmp/stdout.log"
echo "This goes to /tmp/stderr.log" 1>&2
cmd1="$(uname -a)"
cmd2="$(uname +-a)"

exec 1>&3
read -n 1 -s -r -p "Please do manually Installation of package ,Press any key to continue"



exec 1>&3 2>&4
exec 3>&- 4>&-

我试图恢复exec 1>&3所以读取是回声,但它没有显示当我做正常的echo "hello"它显示但不与read

对于我希望用户干预的代码中的选择性位置,我恢复输出处理但是脚本等待命令进入然后执行。

bash scripting stderr
1个回答
2
投票

你期待read打印到stdout,但它打印到stderr,如此命令所示:

> read -p "prompt" 2>/dev/null # this command will print nothing

看看你的/tmp/stderr.log。丢失的提示将在那里。

要恢复read打印到屏幕的能力,而不是恢复stdout,你需要恢复stderr

exec 2>&4
read -n 1 -s -r -p "Please do manually Installation of package ,Press any key to continue"

或者,如注释中所述,您可以仅使用stderr命令恢复read,而不是使用单独的命令恢复read -n 1 -s -r -p "<shortened for clarity>" 2>&4

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.