nmh-workers
[Top] [All Lists]

Re: [Nmh-workers] Getting the location of the of the nmh executables from a script

2015-01-27 13:15:29
ralph wrote:
Hi Norm,

Neither 'command -v scan' nor 'which scan' works for me. I want to
name the script, from which I want to know where nmh's scan is
located. "scan". I want to know its location so I can invoke it.

OK, so you've your own `scan' script that you want to be found earlier
in PATH and have it call the next one that's found if PATH searching
continued?  The way I normally do that is to hardcode the path to the
next one in line, I must admit.

in bash, i do:
 mhbin=$(type -p install-mh)  # find the install-mh executable
 mhbin=${mhbin%/*}            # strip the last '/' and anything that follows

the first line could be replaced by
        mhbin=`which install-mh`

the second could be replaced by something clever in expr or sed.

later, my script says:
 $mhbin/comp   # run the real 'comp' command

paul



PATH could be manipulated to remove the directory that found this scan,
so the next one is found.  But that's just as error prone as parsing
PATH and searching oneself, remembering that `.' can be an empty
element.  The problem is often what is "this" script given it might have
been found by searching PATH, or as `../bin/scan'.  bash sets $_ on
entry to the script to the absolute path to it, but only if PATH was
searched, else it's `../bin/scan', for example.

If you don't want to hardcode then how about a script that lists all the
places foo is found in PATH;  then you could just pick the second one,
assuming the first is the one to skip.  This script lists all places
it's found in PATH, if I've done it right, e.g. `searchpath scan'.
Piping into `sed -n 2p' would print the second, if there is one.

    #! /bin/bash

    : ${1?specify command to find}

    p="$PATH"
    # Make current working directory explicit.
    p="${p/#:/.:}"
    p="${p/%:/:.}"
    p="${p//::/:.:}"

    IFS=: read -ra p <<<"$p"
    for d in "${p[@]}"; do
        f="$d/$1"
        [[ -r $f && -x $f ]] && printf '%s\n' "$f"
    done

Cheers, Ralph.

_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
https://lists.nongnu.org/mailman/listinfo/nmh-workers

=----------------------
 paul fox, pgf(_at_)foxharp(_dot_)boston(_dot_)ma(_dot_)us (arlington, ma, 
where it's 11.8 degrees)

_______________________________________________
Nmh-workers mailing list
Nmh-workers(_at_)nongnu(_dot_)org
https://lists.nongnu.org/mailman/listinfo/nmh-workers

<Prev in Thread] Current Thread [Next in Thread>