View Full Version : How to make shell script pause?
fatsheep
October 1st, 2006, 10:25 PM
Whenever I run a bash script in the terminal (double click on it, "Run in Terminal") it will open up a terminal window, complete the script and exit RIGHT after it finishes giving me no time to review the output. I would like the script to leave the terminal window open after it finishes and allow further commands if possible. How would I do this? I know the sleep command can pause the script for a designated ammount of time and then close the terminal but this is an imperfect solution.
po0f
October 1st, 2006, 10:47 PM
fatsheep,
I assume you are using Gnome Terminal. In this case, open up a new terminal window. Go to the "Edit" menu and click "Current Profile". Click on the "Title and Command" tab. In there, there is a setting called "When command exits". Change it to "hold the terminal open".
fatsheep
October 1st, 2006, 10:54 PM
fatsheep,
I assume you are using Gnome Terminal. In this case, open up a new terminal window. Go to the "Edit" menu and click "Current Profile". Click on the "Title and Command" tab. In there, there is a setting called "When command exits". Change it to "hold the terminal open".
Cool. Thanks a bunch. However, is there any way to do this inside a script itself?
po0f
October 1st, 2006, 11:00 PM
fatsheep,
I think you would manually have to open up a terminal and run the script if you wanted to do it that way. AFAIK, there is no way to do this from a script. When the script exits, whatever program called it (Nautilus in this case, I'm guessing), gets the exit signal and closes the terminal. That's why Gnome Terminal has that setting.
ghostdog74
October 2nd, 2006, 01:23 AM
Whenever I run a bash script in the terminal (double click on it, "Run in Terminal") it will open up a terminal window, complete the script and exit RIGHT after it finishes giving me no time to review the output. I would like the script to leave the terminal window open after it finishes and allow further commands if possible. How would I do this? I know the sleep command can pause the script for a designated ammount of time and then close the terminal but this is an imperfect solution.
use the read command ?
fatsheep
October 2nd, 2006, 02:04 AM
use the read command ?
Works like a charm. Thanks a bunch! :p
amo-ej1
October 2nd, 2006, 07:53 AM
And if you simply want a pause, use sleep
sleep 10
will sleep 10 seconds (10 can be any number ;-) ) (a long as it's positive)
hackeron
January 31st, 2007, 02:51 PM
If you don't want to change the code of the script, you can simply hit ctrl+z on the keyboard to suspend any process running in foreground and run fg to resume it in foreground (or bg to resume it in background).
Tritonio
October 10th, 2007, 11:26 PM
Works like a charm. Thanks a bunch! :p
For some weird reason the read command doesn't keep the terminal open for me. But if I run the script through a terminal manualy then it gives the desired effect. I am using something like this:
#/bin/bash
'./LUA Binaries/LUA Binaries for Linux/lua5.1' './source/AskWise.lua'
read
Should it work? :confused:
dwhitney67
October 11th, 2007, 05:36 AM
If you merely want access to the shell after your script is complete, try this:
xterm -e 'bash myscript.bash && bash'
With this concept you would *not* require the 'read' statement in your script. Btw, for some reason I could not get the same results when I substituted gnome-terminal for xterm. Maybe a gnome guru can explain this.
americancaesar2
June 18th, 2008, 04:33 AM
read -p "Press enter to continue"
to make the script pause
or
read -n 1 -p "Press any key to continue"
to mimic the DOS pause more exactly
the prompt message (what you put in quotes) is up to you
Skip Da Shu
April 27th, 2009, 09:44 PM
READ and SLEEP both worked for me at bottom of my 1st .sh :-)
fi
done
echo " "
echo "Done, Press ENTER to end"
read
#echo "Done"
#sleep 6
Works now as a 'launcher' on my gnome desktop also.
Endolith
May 6th, 2009, 12:53 AM
read -p "Press enter to continue"
to make the script pause
This does not work in Ubuntu.
Press enter to continueread: 19: arg count
Endolith
May 6th, 2009, 12:59 AM
This works, though:
#!/bin/sh
read -p "Press enter to continue" nothing
the version of read used by /bin/sh doesn't like it if you don't give it a variable
Skip Da Shu
June 2nd, 2009, 07:36 PM
This does not work in Ubuntu.
Press enter to continueread: 19: arg count
But using "echo" and then read w/o any parms does (as shown in the post above yours).
In larger scripts I did later where I needed to do this a couple times I just stick both of these as a function at the top of the .sh
function wait_enter {
echo ""
read -p "Please press enter to continue..." nothing
}
function cont_y_n {
printf "Do you want to continue (Y/n): "
read INPT
if [ "$INPT" = "n" ]; then
echo "Ending..."
echo ""
exit
fi
}Then invoke as:
echo "Terminating this process..."
wait_enter or
cont_y_n
newtekken
July 31st, 2009, 08:57 PM
I think, the "sleep" command is the perfect suggestion for the pausing purpose. Thanks.
hyperAura
August 1st, 2009, 12:33 PM
thnx for this 1 i needed it too.. read command suits my needs..
ihitf13anddied
August 3rd, 2009, 12:40 AM
This works, though:
#!/bin/sh
read -p "Press enter to continue" nothing
the version of read used by /bin/sh doesn't like it if you don't give it a variable
Thank you SO MUCH!
I was going crazy wondering why read -p "Text here" was not doing the job when running the .sh from a launcher.
Other sites say to just use the read command without mentioning the variable. That works great if you run the .sh from terminal, but since I am running from a launcher your variable method solved my issue.
simon_w
August 19th, 2009, 07:02 AM
Thank you SO MUCH!
I was going crazy wondering why read -p "Text here" was not doing the job when running the .sh from a launcher.
Other sites say to just use the read command without mentioning the variable. That works great if you run the .sh from terminal, but since I am running from a launcher your variable method solved my issue.
For what it's worth, the issue here is that your terminal is running bash, and you have the shebang at the top of your launcher script invoking sh instead. If you change the top line to "#!/bin/bash" then commands will behave as you're used to in the Terminal.
JBA2337
February 20th, 2010, 09:26 AM
An easy way to pause the terminal after running a script command is to use read -p followed by the text of your choice in quotation marks, as in the following example.
#!/bin/bash
df -h
read -p "-End-"
The above command should be copied to a text editor, and saved as a *.sh file. In the example above, I saved the above code in a file named "Mounted File Systems.sh"
Double clicking on that file will offer you the choice of running the fle in a terminal, or displaying its contents.
JBA2337
Byb
April 26th, 2010, 12:58 PM
The above command should be copied to a text editor, and saved as a *.sh file. In the example above, I saved the above code in a file named "Mounted File Systems.sh"
Double clicking on that file will offer you the choice of running the fle in a terminal, or displaying its contents.
JBA2337
For me, I just not even have to save as .sh. Even without any extension I'm prompted to display or run in terminal (maybe because I set the x attribute).
Thank you very much all you for this very useful and detailled topic.
jimbabwean
November 20th, 2012, 02:31 PM
echo Press enter to exit this script
read xyz
nothingspecial
November 20th, 2012, 02:48 PM
Old thread.
Closed.
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.