Hello,
Somebody (Jeff Smith?) and I were talking last week, and I happened to
mention a short shell script I had written to take care of running Netscape
for me. It handles various combinations of arguments and states, such as
opening a local file in a new window when Netscape is already running, or
removing stale lock files. He asked me to post it to the list. Well, here it
is, attached to this message. I hope somebody finds it useful.
Share and enjoy.
--
Ben Scott <[EMAIL PROTECTED]>
| Brooks's Law: "Adding manpower to a late software project makes it later." |
#!/bin/bash2
# This is a shell script which handles opening the Netscape Navigator
# browser window in a varietry of circumstances. It requires GNU bash
# version 2 or later (or another Bourne-compatible shell which supports
# the "[[ ... ]]" and "${foo:start:length}" constructs).
#
# Recommend you install as "netscape" in a directory early in your path.
#
# It will start Netscape if needed, or open a new window if it is already
# running. Local files, URLs, and "default new windows" (no arguments)
# are all handled in both cases.
#
# If this script is invoked with multiple arguments, they are simply
# passed to Netscape directly, so it is compatible with programs that
# know Netscape and do special tricks with it.
#
# It also starts JunkBuster, if configured and needed, and closes it when
# Netscape exits.
#
# There are almost certainly race conditions in all this process grepping
# and the like, so don't spaz and click your "Netscape" icon sixty times
# a second, or you'll probably get an error.
#
# Contact Ben Scott <[EMAIL PROTECTED]> with questions, comments, and
# or suggestions.
# ---------- you may need to tweak these for your installation -----------
# if MOZILLA_HOME isn't already defined, define it now
# you can set it in your login script, or set the default here
# I have my own installed; Red Hat uses /usr/lib/netscape
if [[ ! $MOZILLA_HOME ]] ; then
export MOZILLA_HOME=/usr/local/opt/netscape
fi
# name of the Netscape binary
# on Red Hat, it will be netscape-navigator or netscape-communicator
NETSCAPE="$MOZILLA_HOME/netscape"
# if you've got JunkBuster (http://www.junkbusters.com) running on a
# per user basis, point $JBCONFIG to the confile file, and this will start
# it for you. Or, use or set the default here.
if [[ ! $JBCONFIG ]] ; then
JBCONFIG="$HOME/.junkbuster/config"
fi
# ---------- you shouldn't have to change anything past this point -----------
LOCKFILE=$HOME/.netscape/lock
# if Junkbuster isn't running -- start it
unset STARTED_JUNKBUSTER
if [[ $JBCONFIG && -f $JBCONFIG ]] && ! ps x | grep -q [j]unkbuster ; then
junkbuster $JBCONFIG &
STARTED_JUNKBUSTER=1
fi
# if Netscape is not in the process list, but the lock file exists,
# then it must have crashed, so erase the lock file
if [[ -h $LOCKFILE ]] && ! ps x | grep -v grep | grep -q $NETSCAPE ; then
rm -f $LOCKFILE
fi
# now the fun begins!
if [[ ! -h $LOCKFILE ]]; then
# if Netscape is not already running, we just invoke it normally
$NETSCAPE "$@"
# okay, Netscape is already running....
elif [[ $# -gt 1 ]]; then
# if multiple arguments were passed, we just pass them to Netscape
# as is (this allows direct use of "-remote" and the like...)
$NETSCAPE "$@"
elif [[ $# = 1 ]]; then
# a single argument was passed
if [[ -e $1 ]]; then
# argument exists in filesystem
# we assume it is a local file to view
if [[ ${1:0:1} = / ]] ; then
# if first character of filename was a slash (/)
# it is an absolute path and we just prepend file://
URL="file://$1"
else
# not an absolute path, we need to insert $PWD also
URL="file://$PWD/$1"
fi
else
# argument must be a plain old URL
URL="$1"
fi
$NETSCAPE -remote openURL\("$URL"\,new_window\) &
else
# if no arguments were passed, we simply open a new window
$NETSCAPE -remote 'openBrowser()' &
fi
# if we started JunkBuster prior to getting into this mess, clean up
# after ourselves and shut it down, now.
if [[ $STARTED_JUNKBUSTER ]] ; then
# FYI, bash seems to feel a need to print the fact that we killed one
# of its jobs here, despite the fact that this should be a
# non-interactive shell. even doing a "set +m" doesn't help.
killall junkbuster
fi
# END OF FILE