Bash:脚本中的退出不会关闭终端

时间:2017-10-23 18:36:28

标签: 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循环停止,终端将不会以命令“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 $ 

它永远不会退出......我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

  

终端不会以命令“exit”

关闭

那是因为你在脚本中运行exit命令。 kill $pidOfTheTerminalWindow表示“退出脚本”,而不是“退出终端”。 如果要从脚本关闭终端窗口,则可能必须使用source sxdd 等命令。

作为替代方案,您可以 source 脚本,即执行脚本,就像您直接在命令行中键入脚本一样。使用

. sxdd

body {
  font-family: sans-serif;
}

h2 {
  color: #3CAD5D;
  padding-bottom: 0px;
  margin-bottom: 0px;
}

ul {
  margin: 0 auto;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  max-width: 1500px;
  width: 100%;
}

li {
  display: flex;
  width: calc(33.333333% - 10px);
}

a {
  flex-grow: 1;
  box-sizing: border-box;
  text-decoration: none;
  color: #000;
  box-shadow: 3px 3px 5px #888888;
  padding: 20px;
}

a:hover {
  color: #fff;
}


/*** MEDIA QUERIES ***/

@media screen and (max-width: 800px) {
  ul {
    flex-direction: column;
    /* change direction of flex for mobile */
    margin: 0 auto;
  }
  li {
    width: 97%;
  }
}


/*** BG WITH ZOOM ***/

.bg1 {
  background: url('https://cmeimg-a.akamaihd.net/640/clsd/getty/c64f76dc20c246ca88ee180fe4b4b781');
  margin: 5px;
}

.bg2 {
  background: url('http://www.petmd.com/sites/default/files/hypoallergenic-cat-breeds.jpg');
  margin: 5px;
}

.bg3 {
  background: url('https://ichef.bbci.co.uk/images/ic/560x315/p0517py6.jpg');
  margin: 5px;
}

.bg1,
.bg2,
.bg3 {
  background-size: 100% 100%;
  background-repeat: no-repeat;
  -webkit-transition: all .5s;
  -moz-transition: all .5s;
  -o-transition: all .5s;
  transition: all .5s;
}

.bg1:hover,
.bg2:hover,
.bg3:hover {
  background-size: 130% 130%;
}

答案 1 :(得分:1)

尝试像这样运行脚本: exec ./sxdd

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