代码为
#!/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
时不会显示。
对于代码中我要用户干预的特定位置,我恢复了输出处理,但脚本等待命令输入然后执行。
答案 0 :(得分: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