tty >/dev/null || { urxvt -hold -e "$0" "$@" & exit; }
This can be the first line of a script that will be clicked from a graphical user interface in X to make it open up a virtual terminal to display output. If a terminal is already open it will run in the current terminal. It assumes urxvt
and uses the hold option to keep from closing, both of which could be substituted for such as rxvt
or add read
at the end of the script.
if
statement that checks the exit code of tty
which prints the current terminal name usually nothing under X.{
and a semicolon is required before the closing brace }
.&
forks the terminal command to a second process and the launching script exits right away.-e
feeds to the terminal application the expression of $0
which holds the path of the script itself and $@
, the entire set of quoted arguments.If the script is large, say several gigabytes and the system tries to make two copies of the script, twice the size of RAM or memory will be needed for loading it.
rxvt -e
will kill any subprocesses at the endtty >/dev/null || { urxvt -e /bin/sh -c "tty >/tmp/proc$$; while test x; do sleep 1; done" & while test ! -f /tmp/proc$$; do sleep .1; done; FN=$(cat /tmp/proc$$); rm /tmp/proc$$; exec >$FN 2>$FN <$FN; }