<<< Date Index >>>     <<< Thread Index >>>

Re: text/html; mutt_bgrun mozilla.sh %s; test=InXTerm



On Thursday, November  3 at 11:22 AM, quoth Gary Johnson:
In my .mailcap, I have the following:

image/*; mutt_bgrun /usr/bin/display %s; test=InXTerm
image/*; /usr/bin/zgv --zoom-reduce-only --zoom -m '1024 768 24' %s; 
test=InPlainTerm

Where InXTerm looks like this:

#!/bin/sh
if [ "$TERM" == "xterm" ]
then
    exit 0;
else
    exit 1;
fi

Wow - talk about an amusing script.

So, the point of the above script is to have a return value of either 0 or 1 depending on whether $TERM is "xterm" or not. To find out whether $TERM is "xterm" or not, it uses a program called "test" (which is also known as [ -- check it out, it's /bin/[ on your system). The funny part is that test has a return value of 0 or 1 depending on whether what you ask it to test for is true or false (respectively). So, that script can be simplified to:

   #!/bin/sh
   exit [ "$TERM" == "xterm" ]

Or, if you prefer, it could be further simplified to:

   #!/bin/sh
   test "$TERM" == "xterm"

...because the return value of a shell script is the same as the return value of the last command it ran (unless otherwise specified).

Now, since mailcap allows you to specify a command to run, why run "IsXterm" the shell script when you can just run the "test" program directly? Like so:

  image/*; mutt_bgrun /usr/bin/display %s; test=test "$TERM" == "xterm"

Because it's just silly to encapsulate something that small in it's own shell script -- I mean, you may as well make your own shell script called "showFiles" that runs ls for you, that's how silly that is.

Now, let's think about something else: why test to see if you're in an xterm or not? I mean, if you decide one day that you want to try out some of the other terminals out there, say, rxvt or GnomeTerminal or if you buy a Mac and use AppleTerminal or something like that... why should your test break?

What you're really looking for is you're looking to see if you're running in an environment where mozilla (or in the example case above, "display") can be run: i.e. you don't want it to run if you're in a terminal where an X11 server isn't available. I mean, if you're running an xterm, then X11 is available, but that's not the only time that it's available. So how do we find out if X11 is available more directly? There's another environment variable, like TERM, that *must* be defined if things like mozilla or xterm or any other X11 program can run. That environment variable is DISPLAY. But, for most purposes, it doesn't really matter what the contents of DISPLAY are, it matters more that DISPLAY has been defined. The "test" program can detect that, too. In fact, that's its default feature -- like so:

   test "$DISPLAY"

In other words, the above example can be simplified down to:

   image/*; mutt_bgrun /usr/bin/display %s; test=test "$DISPLAY"

And InPlainTerm like this:

#!/bin/sh
if [ "$TERM" == "linux" ]
then
    exit 0;
elif [ "$TERM" == "screen" ]
then
    exit 0;

This is similar to the previous script -- "linux" and "screen" are two examples of terminals where X11 is rarely available. If your TERM is "linux", you're at a non-X11 console. If your TERM is "screen", you're running inside a screen session (complicated -- read the man page of "screen" if you really want to know). But this list is incomplete -- there are many more terminals where X11 is not available. For example, many WYSE terminals: vt100, vt220, vt200, and so on. The DISPLAY variable is more powerful and more accurate: if it exists, you should be using a terminal where an X11 server is available. If it doesn't exist, you are using a terminal where an X11 server is not available.

~Kyle
--
Never think that war, no matter how necessary, no matter how justified, is not a crime.
                                                    -- Ernest Hemingway

Attachment: pgpBl4MjsB31i.pgp
Description: PGP signature