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.

7
  • 1
    Don't call your file test as you're probably calling the system's /usr/bin/test.
    – slhck
    Apr 16, 2013 at 13:12
  • Thanks, I hadn't thought of that. Edited the question. (It still doesn't work, but at least I know it's not because of a naming conflict this time.)
    – Kev
    Apr 16, 2013 at 13:16
  • 1
    Is the test working? Replace Terminal with another command, is it run correctly?
    – terdon
    Apr 16, 2013 at 13:24
  • 1
    I don't really know much about Haiku, but here's what I'd try: 1. Remove the space between the shebang and /bin/sh. 2. Use a relative or absolute path for testterm. 3. Try executing Terminal testterm & from a terminal. All terminal emulators I've worked with require a switch before the executable (e.g., xterm -e ./testterm).
    – Dennis
    Apr 16, 2013 at 13:32
  • 1
    Assuming Terminal works like xterm, the file it is looking for is the shell it should run. If so, I can think of a workaround. Try running Terminal sh, does it use sh instead of bash?
    – terdon
    Apr 16, 2013 at 15:16

1 Answer 1

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
4
  • @Kev, see my updated answer for a hack you can try.
    – terdon
    Apr 16, 2013 at 17:50
  • Awesome! 99% of the way there, which is all I need. A couple things, the prompt in the new window is bash-4.0# instead of /> (doesn't matter) and when you go to close the window, it says, The process "bash" is still running. If you close the Terminal, the process will be killed. (Terminals normally just close. Also not a big deal.)
    – Kev
    Apr 17, 2013 at 16:49
  • @Kev, the prompt can be changed by setting the PS1 variable in foo.rc, the reason it is different is because /etc/bash.bashrc is not read, so you could also fix this by sourcing it in foo.rc. As for the error, it is because you are explicitly launching /bin/bash. Try launching in background (&, though I doubt that will work).
    – terdon
    Apr 17, 2013 at 16:53
  • The & makes the Terminal appear blank again, although that is the correct syntax for asyncronous commands in Haiku. Thanks for the PS1 hint, I'll look that up. BTW not sure why my edit came out looking funny...
    – Kev
    Apr 17, 2013 at 17:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .