On 2025-12-20 23:32:51, sr()strahinja!org wrote:
>> I wanted a generic way to find the path to the daemon that would
>> not be influenced by the caller's PATH. (safety, consistency)
>
> A bit safer (not dependant on PATH) might be something like:
>
> prog=${0##*/}
>
> if [ -x /usr/sbin/"$prog" ]; then
> daemon=/usr/sbin/"$prog"
> elif [ -x /usr/local/sbin/"$prog" ]; then
> daemon=/usr/local/sbin/"$prog"
> else
> exit 1
> fi
>
> The if-construct is used deliberately instead of altering PATH,
> to preserve it for $prog.
Thank you for the suggestion, can't say I like the look of many ifs.
I think a better version using parameter assignment for single command is:
#!/bin/ksh
daemon="$(PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/libexec:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin:/usr/local/libexec
whence -p ${0##*/})"
. /etc/rc.d/rc.subr
rc_cmd $1
PATH is only changed for the whence command.
You can now see the search path list and edit it.
One less nesting of $() commands.
Daemon line now extremely long.
It does make the script twice as long but
safety is more important then shortness.
I copied the path from root's .profile and inserted
the /usr/libexec /usr/local/libexec directories.
What's important is the search order consistency.
Still seems a bit much for a generic script.
Makes just copying another script and editing daemon=
line all the more attractive option.