2

I'm fairly good with windows .bat scripts and normally use the pause command while developing them so I can see things step by step as the script runs in the command prompt. I am trying to do something similar with a Linux Bash file. I have a very simple bash, which can be seen below...

#!/bin/bash
echo Hello World!
read -p "Press [Enter] key to continue..."

I configured Nautilus (a Linux file management program) to "Run executables when they are double clicked" but I don't see anything when I double click the script. Do I need to re-route output to the console or something?

Edit

Here is how I achieved what I wanted...

  1. Create a .sh file (I am using one from above)
  2. Open Nautilus
  3. Click Edit -> Preferences -> Behavior tab
  4. Under "Executable Text Files" choose "Ask Each Time" Radio button

Now when you click on .sh files, you will be prompted with a few options. Option "Run in Terminal" does exactly what I want. Thanks everyone for the contributions!

Jason
  • 153
  • 2
  • 7

2 Answers2

2

You need to launch a terminal, which in turn launches the script.

Try this:

#!/bin/sh
xterm -e "echo Hello World; read -p 'Press [Enter] key to exit ..."

This is of course impractical for a longer script. But then you can simply do:

#!/bin/sh
xterm -e /home/user/scripts/thescript.sh

and thescript.sh contains all the commands you want to use. Make sure it is executable (chmod +x thescript.sh)

Sebastian
  • 8,677
  • 4
  • 39
  • 49
  • I figured out how to do exactly what I wanted, I added the edit above. What exactly does xterm do? – Jason Sep 08 '14 at 20:23
  • Congratulations. Xterm is just a terminal emulator. See `man xterm`. You might prefer another one. E.g. `gnome-terminal`. – Sebastian Sep 09 '14 at 04:49
1

Your script needs a terminal for you to interact with it. One way of doing that is what @Sebastian suggested. However, note that bash scripts are not really designed to be run this way. A cleaner way would be to write a .desktop file that launches your script and then double click that. Something like:

[Desktop Entry]
Exec=/home/user/yourscript.sh
Terminal=true
Type=Application

Save that file as foo.desktop in your ~/Desktop folder. That will now appear as an icon there and double clicking it will cause your script to be run in a terminal. Obviously, you need to change /home/user/yourscript.sh to the actual path of your script.

terdon
  • 234,489
  • 66
  • 447
  • 667
  • With your terminal emulator, how can we redirect stdin , stdout and stderr to /dev/null? Thank you. – Frank May 28 '16 at 04:50
  • @Frank I don't know. I would do it by editing the script itself or writing a wrapper script. – terdon Jun 01 '16 at 10:02
  • You can consider [my script here](https://unix.stackexchange.com/a/674052/318461) to generate these files - just switch `Terminal=` from `false` ([there](https://unix.stackexchange.com/a/674052/318461)) to `true` ([here](https://unix.stackexchange.com/a/154410/318461)) – Cadoiz Nov 05 '21 at 16:27