0

The script basically has output that the user should read. I was thinking of this:

#!/bin/bash

#cd to the script dir if executed from outside
SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_PATH"
if [ ! -t 1 ]; then #not from terminal
    SCRIPT=$(basename "$(readlink -f "$0")")
    x-terminal-emulator --profile "$USER" --working-directory "$SCRIPT_PATH" -e "./$SCRIPT" &
    exit 0
fi

echo "output"

#keep terminal open
bash

but this has the problem that if you try to close the window of the new terminal (when executed from nautilus, pgrep says only one bash process) or the 'old' terminal (when executed from a already open terminal, pgrep says two bash processes), that "There is still a process running in this terminal. Closing the terminal will kill it."

I'm looking for a complete solution: can run from a open terminal, can run from nautilus or another file manager, will always allow the user to read output, won't leave processes hanging when exits and won't show that warning on the user innocently closing the window.

I'm pretty sure that the 'process' making the visible console show that warning is the bash at the last line, but that bash is the only thing allowing the echo to be seen if the script is run from nautilus.

  • bash i snot the only way, you could add a read command at the end and it will wait for input from user before closing. If you want the output of your script start your script from a terminal do not use nautillus to start it, it would be the best way to do what you want to do. – Kiwy Apr 25 '18 at 7:36
  • I see. I think i'll do something like if[ ! -t 1 ]; then SUBPROC=1 x-terminal-emulator ... and at the end if [ "$SUBPROC" -eq 1 ]; then read or similar. You'd think this would be a easy and catered for usecase sigh. – i30817 Apr 25 '18 at 7:40
1

Slight improvement: use trap to prompt at exit:

# some scripts like are meant to be run by clicking on
# them in the file manager.  This function runs the script in an X
# terminal if it isn't in a terminal already.
run_in_terminal() {
    # run in a new terminal if not already
    if [ ! "${RUNNING_IN_TERMINAL:-}" -a ! -t 1 -a "${DISPLAY:-}" ]; then
        RUNNING_IN_TERMINAL=1 exec x-terminal-emulator -e "$@"
    fi

    # wait for prompt to close the terminal
    if [ "${RUNNING_IN_TERMINAL:-}" ]; then
        exit_prompt() {
            echo
            read -p "Finished.  Press any key to close..." -n 1
        }
        trap exit_prompt EXIT 
    fi
}

run_in_terminal "$0" "$@"
0

it's possible to solve this by doing:

SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_PATH"
if [ ! -t 1 ]; then #not from terminal
    SCRIPT=$(basename "$(readlink -f "$0")")
    SUBPROC=1 x-terminal-emulator --profile "$USER" --working-directory "$SCRIPT_PATH" -e "./$SCRIPT" &
    exit 0    
fi
echo "output"
...
[[ -v SUBPROC ]] && read -p "Press any key to exit" -n1 junk

a bit annoying but sure.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.