On Tue, May 17, 2011 at 11:11:58AM -0400, Robert Jim Fulner wrote:
> Your script is a little beyond my understanding. Would you mind taking 
> some time to explain what's actually going on here?

Sorry for the delay; I wrote line by line explanations (maybe more than 
you wished), and since this allowed me to think again about it I wrote 
an improved version of the script.

> >     #!/bin/sh

This must be the first line (and there must be no space before #!) and means 
that when you execute the script (the file must be executable), the content 
will 
be interpreted by the shell /bin/sh (this is linked to dash for me).

> >     if elinks -remote ping\(\) 2>/dev/null; then

elinks -remote ping\(\) returns true if ELinks is running. It's necessary to 
escape the parentheses with \ (writing "ping()" with double quotes would also 
work) otherwise the shell interprets ping() as the start of a function 
definition.
2>/dev/null means that the error output of this command is discarded, so 
that if ELinks is not running you won't see the error message it 
displays:
ELinks: No running ELinks found.

> >         exec elinks -remote ${1:-about:blank}

If an ELinks instance was found, run the command
elinks -remote ${1:-about:blank}
${1:-about:blank} means:
- $1, i.e. the first parameter of the script if there was one;
- about:blank by default, you can replace it with whatever URL you like.
So if a URL was passed as a parameter it is opened in a new tab, 
otherwise a new tab with the URL about:blank is opened ('elinks -remote' alone 
returns an error).

> >     elif [ -n "$DISPLAY" ]; then

[ -n "$DISPLAY" ] is the same as test -n "$DISPLAY" and returns true if the 
DISPLAY environment variable is set, which means that an X server is available.

> >         exec roxterm -p ELinks -e elinks $1

In that case, execute
roxterm -p ELinks -e elinks $1
Which means "launch a new roxterm window with the profile ELinks, and 
execute the command 'elinks $1'" in this window."
As above $1 is the first parameter given to the script, I didn't set 
a default parameter in case $1 is empty because 'elinks' with no 
parameter doesn't return an error.
   You should replace "roxterm -p ELinks" with the terminal emulator and 
options you want to use. The option -e is standard for all terminal 
emulators and allows to run a command in the new terminal, but with some 
terminal emulators it's necessary to write
... -e "elinks $1"
because they interpret only the argument of the option as the command to 
run, while most emulators interpret everything until the end of the line 
as the command.

> >     else
> >         elinks $1
> >     fi

If neither ELinks nor X is running we are in a Linux console, just run elinks 
with the given URL. If you don't care about this case you can remove it 
and replace "elif ... then" with else.

> >     exit 0

Return 0 (no error) when finishing the script, but I think this line 
should be removed, because it hides the error code possibly returned by 
one of the previous commands (and anyway with the exec's this line is 
not even read).

The exec at the beginning of the lines mean that the command following 
replaces the shell. Actually I don't see any difference when I remove 
them, but since the aim of the script is to launch another program 
I think it's better to keep them (and add one in the third case).

A thing that is missing from the script is an "&" at the end of the line with 
the terminal emulator, in order to run the command in the background (this 
changes nothing for roxterm and some others, but is useful with e.g. xterm if 
you run the script from a terminal).

It's also possible to use variables to set your preferences, so here is 
an improved version, with xterm as a terminal emulator:

        #!/bin/sh

        DEFAULT_URL="about:blank"
        TERMINAL="xterm"

        if elinks -remote ping\(\) 2>/dev/null; then
                exec elinks -remote ${1:-"$DEFAULT_URL"}
        elif [ -n "$DISPLAY" ]; then
                exec $TERMINAL -e elinks $1 &
                # If you get an error or if the URL is ignored with your 
                # TERMINAL setting, comment the line above and use the 
following 
                # instead:
                #exec $TERMINAL -e "elinks $1" &
        else
                exec elinks $1
        fi

Or more simply, if you don't care about the "no X display" case:

        #!/bin/sh

        DEFAULT_URL="about:blank"
        TERMINAL="xterm"

        if elinks -remote ping\(\) 2>/dev/null; then
                exec elinks -remote ${1:-"$DEFAULT_URL"}
        else
                exec $TERMINAL -e elinks $1 &
                # If you get an error or if the URL is ignored with your 
                # TERMINAL setting, comment the line above and use the 
following 
                # instead:
                #exec $TERMINAL -e "elinks $1" &
        fi


When I wrote the first version of this script a few years ago I intended 
to send a patch with something like that to include it in the 
documentation of ELinks (there is already something similar in 
examples/TIPS-AND-TRICKS to launch ELinks in a new console, I took the 
name "relinks" of my script from there). I didn't do it because I wanted 
something that worked with any terminal emulator, but I think it would 
be necessary to add tests and it would make the script uselessly 
complicated (and anyone willing to use that should be able to comment or 
uncomment a line); I'd have also liked to make it use the homepage 
configured in ELinks instead of $DEFAULT_URL (but I'm not sure it is 
feasible), and to make it work with URLs containing spaces, or with 
several URLs when calling it from the command line. Since then I've 
never really felt the need for these features, so I never got around to 
modify it and submit it. In case one of the ELinks developers is reading 
this, feel free to put the script in the documentation if you want (or 
I can send a patch).
_______________________________________________
elinks-users mailing list
elinks-users@linuxfromscratch.org
http://linuxfromscratch.org/mailman/listinfo/elinks-users

Reply via email to