1

I'm working on a script to run on terminal like this one

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

exit

But once the counter reaches 9 and the while cycle stops, the terminal wont close with the command "exit".

Here's the output

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 $ 

And it never quits... how do I fix that?

8
  • 1
    What terminal? How are you running this script? Commented Oct 23, 2017 at 18:40
  • @melpomene I'm using linux. I used che chmod command to run the script on the terminal by doing ./script Commented Oct 23, 2017 at 18:44
  • 1
    There's nothing to fix, then. exit exits your script, as designed. Commented Oct 23, 2017 at 18:45
  • @anubhava yes it is on a text file. I forgot to mention that there is "#!/bin/bash" at the beginning of the script Commented Oct 23, 2017 at 18:45
  • @melpomene look at the updated description so you can see the output I get... Commented Oct 23, 2017 at 18:51

3 Answers 3

3

the terminal wont close with the command "exit"

That's because you run the exit command inside a script. There exit means "exit the script", not "exit the terminal". If you want to close the terminal window from a script, you probably have to use commands like kill $pidOfTheTerminalWindow.

As an alternative, you can source the script, that is execute the script as if you typed it directely into the command line. Use either

source sxdd

or

. sxdd
Sign up to request clarification or add additional context in comments.

2 Comments

Alternatively, run the script commands directly in the current shell with . ./sxdd.
@melpomene thank you your solution works fine ;) if you write it as an answer I'll accept that
2

Try to run your script like this: exec ./sxdd

It replaces the shell process with your script and when the script exits your window will close.

Comments

1

A similar question was asked on Ask Ubuntu. It was suggested that you could put kill $PPID at the end of the script which will send a SIGTERM to the parent process. You could also do this:

echo "You can close this window now"
sleep 100
kill $PPID

Which would prompt the user to close the window and then forcibly close the window using the other method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.