1

In Windows when I double-click a Batch script, it will automatically open a terminal window and show me what's happening. If I were to double-click a bash script in Linux, a terminal window does not open to show me what is happening; it runs in the background. I have seen that one can use one script to launch another script in a new terminal window with x-terminal-emulator -e "./script.sh", but is there any bash command I can put into the same (one) script.sh so that it will open a terminal and show me what's happening (or if I need to answer y/n questions)?

9
  • 1
    I don't know if this would work, but maybe if ! [ -t 0 ]; then exec x-terminal-emulator -e "./script.sh"; fi? Commented Jun 20, 2019 at 12:37
  • @Edward : What do you mean by "double-click in Linux"? WHERE do you double click (i.e. which application is open to show you the script), and which Window Manager are you using? Commented Jun 20, 2019 at 12:41
  • 1
    Heh, or maybe #!/usr/bin/x-terminal-emulator -ebash script.sh as the first line of the script. This might be an awful hack. Commented Jun 20, 2019 at 12:41
  • @user1934428 Anywhere like the Desktop, Nautilus, or totalcommander type managers. Commented Jun 20, 2019 at 12:50
  • 1
    @Edward : With Window managers, I mean something like KDE or Gnome. If i understand you correctly, you need an operation which says: "Open this file WITH program XXX", where your XXX happens to be a Terminal program of your choice. Commented Jun 20, 2019 at 12:54

1 Answer 1

1

You can do something similar to what Slax developers do in their bootinst.sh:

#!/usr/bin/env sh
#
#     If you see this file in a text editor instead of getting it executed,
#     then it is missing executable permissions (chmod). You can try to set
#     exec permissions for this file by using:  chmod a+x bootinst.sh
#
#     Scrolling down will reveal the actual code of this script.
#



# if we're running this from X, re-run the script in konsole or xterm
if [ "$DISPLAY" != "" ]; then
   if [ "$1" != "--rex" -a "$2" != "--rex" ]; then
      konsole --nofork -e /bin/sh $0 --rex 2>/dev/null || xterm -e /bin/sh $0 --rex 2>/dev/null || /bin/sh $0 --rex 2>/dev/null
      exit
   fi
fi

# put contents of your script here
echo hi

# do not close the terminal immediately, let user look at the results
echo "Press Enter..."
read junk

This script would run correctly both when started in graphical environment and in tty. It tries to restart the script inside konsole and xterm and but if it doesn't find neither of them it will simply run in the background.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.