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,142
  • 2
  • 14
  • 37
  • 1
    Don't call your file `test` as you're probably calling the system's `/usr/bin/test`. – slhck Apr 16 '13 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 '13 at 13:16
  • 1
    Is the test working? Replace `Terminal` with another command, is it run correctly? – terdon Apr 16 '13 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 '13 at 13:32
  • @terdon, yes `StyledEdit` opens fine rather than `Terminal`. @Dennis, 1. doesn't seem to make a difference (plus, all my other scripts start that way) 2. tried that, also no difference 3. it does the same as the script, opening a Terminal that never shows anything. `Terminal --help` doesn't mention an option for an executable, yet if you pass an argument it seems to look for a file...maybe not to execute it though? – Kev Apr 16 '13 at 13:48
  • 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 '13 at 15:16
  • @terdon, yep, confirmed via `echo $0`. So I tried `Terminal sh ~/Desktop/testterm`. Oddly, this works if I try it from a `Terminal` window, but not if I double-click the testterm icon (same result as all along.) – Kev Apr 16 '13 at 15:37

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
  • 52,568
  • 14
  • 124
  • 170
  • @Kev, see my updated answer for a hack you can try. – terdon Apr 16 '13 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 '13 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 '13 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 '13 at 17:01