Can I make a script wait for a key press when it has opened itself in a new terminal window?

I came across this line

if [ ! -t 0 ]; then x-terminal-emulator -e "$0"; exit 0; fi

which I can write at the top of a script and when clicking on the script file in nautilus, the script will open itself in a terminal window so that I can see the output.

Now there is the problem that this command

read -n1 -r -p "Press any key to continue..." key

in this case will not wait for a key press.

My guess is that x-terminal-emulator may be the reason, but I haven't found any other solution to double click a script file in Nautilus and get a new terminal window where the script will run.

7

2 Answers

Cannot reproduce. This code

#!/bin/bash
if [ ! -t 0 ]; then x-terminal-emulator -e "$0"; exit 0; fi
echo "new window"
read -n1 -r -p "Press any key to continue..." key
echo bye

when run with ./foo.sh </dev/null opens in a new terminal and waits for a key press.

You'll need to be more explicit about your code.

1
read -r -p "Press any key to continue..." key

waits for the Return key, no matter if #!/bin/bash has been used or not.

When

#!/bin/bash

is at the top of the script, -n1 will work and will wait for any key.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like