Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash:脚本中的退出不';关闭终端_Linux_Bash - Fatal编程技术网

Linux Bash:脚本中的退出不';关闭终端

Linux Bash:脚本中的退出不';关闭终端,linux,bash,Linux,Bash,我正在编写一个脚本,运行在像这样的终端上 #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER yoooooooo sleep 2 let COUNTER=COUNTER+1 done exit 但是,一旦计数器达到9并且while循环停止,终

我正在编写一个脚本,运行在像这样的终端上

#!/bin/bash
COUNTER=0
         while [  $COUNTER -lt 10 ]; do
             echo The counter is $COUNTER
             yoooooooo
             sleep 2
             let COUNTER=COUNTER+1 
         done

exit
但是,一旦计数器达到9并且while循环停止,终端将不会使用命令“exit”关闭

这是输出

pi@raspberrypi:~/Desktop $ ./sxdd
The counter is 0
./sxdd: line 7: yoooooooo: command not found
The counter is 1
./sxdd: line 7: yoooooooo: command not found
The counter is 2
./sxdd: line 7: yoooooooo: command not found
The counter is 3
./sxdd: line 7: yoooooooo: command not found
The counter is 4
./sxdd: line 7: yoooooooo: command not found
The counter is 5
./sxdd: line 7: yoooooooo: command not found
The counter is 6
./sxdd: line 7: yoooooooo: command not found
The counter is 7
./sxdd: line 7: yoooooooo: command not found
The counter is 8
./sxdd: line 7: yoooooooo: command not found
The counter is 9
./sxdd: line 7: yoooooooo: command not found
pi@raspberrypi:~/Desktop $ 
它永远不会停止。。。我该如何解决这个问题

终端不会用“退出”命令关闭

这是因为您在脚本中运行exit命令。退出
exit
表示“退出脚本”,而不是“退出终端”。 如果要从脚本关闭终端窗口,可能必须使用类似于
kill$pidofterminalwindow
的命令

另一种方法是,您可以为脚本提供源代码,也就是像直接在命令行中键入脚本一样执行脚本。使用其中一个

source sxdd

终端不会用“退出”命令关闭

这是因为您在脚本中运行exit命令。退出
exit
表示“退出脚本”,而不是“退出终端”。 如果要从脚本关闭终端窗口,可能必须使用类似于
kill$pidofterminalwindow
的命令

另一种方法是,您可以为脚本提供源代码,也就是像直接在命令行中键入脚本一样执行脚本。使用其中一个

source sxdd


尝试按以下方式运行脚本:
exec./sxdd


它将用脚本替换shell进程,当脚本退出时,窗口将关闭。

尝试如下方式运行脚本:
exec./sxdd


它用脚本替换shell进程,当脚本退出时,窗口将关闭。

哪个终端?你是如何运行这个脚本的?@melpomene我正在使用linux。我使用che chmod命令在终端上运行脚本,方法是执行./script无需修复<代码>退出按设计退出脚本。@anubhava是的,它位于文本文件中。我忘了在开头提到“#!/bin/bash”script@melpomene看看更新后的描述,这样你就可以看到我得到的输出…什么终端?你是如何运行这个脚本的?@melpomene我正在使用linux。我使用che chmod命令在终端上运行脚本,方法是执行./script无需修复<代码>退出按设计退出脚本。@anubhava是的,它位于文本文件中。我忘了在开头提到“#!/bin/bash”script@melpomene查看更新后的描述,以便查看我得到的输出……或者,使用
直接在当前shell中运行脚本命令/sxdd
@melpomene谢谢您的解决方案运行良好;)如果您将其作为答案编写,我也会接受。或者,使用
在当前shell中直接运行脚本命令/sxdd
@melpomene谢谢您的解决方案运行良好;)如果你写下来作为回答,我会接受的