2

How can I get a script to re-launch itself in a Terminal window if it wasn't started in one?

Based on this question, I've tried, in a file called testterm marked executable:

#! /bin/sh
if [ -t 0 ];  # stdin
then
    echo "yay! terminal!"
else
    Terminal sh ~/Desktop/testterm
fi

...but Haiku's Terminal just opens and never shows anything, or sometimes opens and goes away immediately.

From the Terminal if I type Terminal sh ~/Desktop/testterm it works once, opening a Terminal with "yay! terminal!" in it, but then subsequent attempts yield empty Terminals.

Kev
  • 1,184

1 Answers1

1

A hack you could try is the following:

  1. Create a special bash .rc file that sources your bashrc and runs your script. Lets call it ~/foo.rc

    $ cat ~/foo.rc
    #!/bin/sh
    ~/Desktop/testterm
    
  2. Create a new "shell" that calls bash with ~/foo.rc as its .rc file. Save this script as fake_shell somewhere in your $PATH (for example, ~/config/bin) and make it executable:

  3. Now, in your testterm script, launch Terminal using fake_shell as the shell.

The script becomes:

#!/bin/sh
if [ ! -t 0 ];  # stdin
then
    TIMESTAMP=`date +%Y%m%d%H%M`
    echo "#!/bin/sh
    source /boot/common/etc/profile
    $0" > ~/temp_term$TIMESTAMP.rc
    echo "#!/bin/sh
    bash --rcfile ~/temp_term$TIMESTAMP.rc" > ~/config/bin/temp_shell$TIMESTAMP
    chmod a+x ~/config/bin/temp_shell$TIMESTAMP
    Terminal temp_shell$TIMESTAMP
    rm -f ~/config/bin/temp_shell$TIMESTAMP
    rm -f ~/temp_term$TIMESTAMP.rc
fi

echo "yay! terminal!"
# your script here
exit
terdon
  • 54,564