代码为
#!/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
。
对于我希望用户干预的代码中的选择性位置,我恢复输出处理但是脚本等待命令进入然后执行。
你期待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