简体   繁体   English

为什么我必须输入enter才能退出bash脚本?

[英]Why do I have to type enter to exit my bash script?

When I run my script, I am forced to type enter key, in order exit the script after it completes. 运行脚本时,我被迫键入Enter键,以在完成脚本后退出脚本。 Here's my script: 这是我的脚本:

log_file=/tmp/log.out

rm -f $log_file

setup()
{
  exec > >(tee -a ${log_file} )
  exec 2> >(tee -a ${log_file} >&2)
}

setup

echo "hello world"
echo "hello again"

I'm testing forcing all echo's to go to stdout and a log file simultaneously. 我正在测试强制所有回显同时进入标准输出和日志文件。 I've seen this behavior before in scripts and have always wondered: 我以前在脚本中已经看到过这种行为,并且一直想知道:

  • Why does this happen? 为什么会这样?
  • How to make it so that I don't have to type return to exit the script? 如何使它不必键入return即可退出脚本?

You don't have to hit enter. 您不必按回车键。 You are already at the next prompt. 您已经在下一个提示符下。 Try typing a command and hitting enter. 尝试键入命令并按Enter。

The problem is that you've raced the output and the next prompt being printed to the screen. 问题在于,您已经竞相输出,并且下一个提示被打印到屏幕上。

So your prompt gets printed before the standard error output gets printed so you "lose" your cursor positioning. 因此,在打印标准错误输出之前先打印提示,这样您就可以“丢失”光标的位置。

Clearing the screen, with Ctrl-L say, will also get you "back" to your prompt. 使用Ctrl-L清除屏幕,也会使您“返回”提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的“exit”命令不退出我的 Bash 脚本? - Why does my “exit” command not exit my Bash script? 为什么我的 bash 脚本中不断出现语法错误? 它是否与“:”有关? - Why do I keep getting syntax error in my bash script? Does it have something related to the “:”? 如何从主脚本中的下标获取退出代码-bash - How do I get the exit code from my subscript in main script - bash 为什么我的bash脚本中出现语法错误? - Why do I get a syntax error in my bash script? 为什么即使我使用“&”,我的 bash 脚本也不会在服务器上并行化? - Why do my bash script is not prallelizing on server even if I use '&'? Bash shell脚本:如何退出并重启脚本? - Bash shell script: how do I exit and restart the script? 以root用户身份运行bash脚本后,如何让脚本自动强制我的用户退出root用户? - After running a bash script as root user, how can I have the script automatically force my user to exit root? 为什么这个bash脚本不会退出? - Why does this bash script not exit? 当我将 pipe 设置为“tee”时,为什么“exit”不退出我的脚本? - Why does "exit" not exit my script when I pipe it to "tee"? 在bash中如何从tee管道的函数中退出脚本? - In bash how do I exit a script from a function that is piped by tee?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM