For a shell script I need to call an executable by name, regardless of the path leading there; thus far I know this code reports the right name:
tmpprog=`which program`
prog=${tmpprog%%*/}
if $prog=""
then
echo >&2 "Main Program not found"
else
echo >&2 "$prog"
fi
How can I cut the code so that I may trim the path to keep the executable name and remove the path in a one liner?
Beware: basename
won't do it, since I may be working on a hybrid environment (cygwin/busybox on windoze), and sometimes the paths have spaces.
program
directly? Why go to this effort of finding whereprogram
is located to then discard that information? – muru Jan 16 '24 at 03:47program
instead of messing around with the path. In your code, that'd be like:if ! command -v program > /dev/null; then echo >&2 "Main Program not found"; else echo >&2 program; fi
– muru Jan 16 '24 at 06:48if $prog=""
do? Have you tried it with various values ofprog
? Why do you say you can't usebasename
due to spaces in the paths? – ilkkachu Jan 16 '24 at 07:01which
, and avoiding the pitfall of a space in a path in a windoze directory withbasename
. – jarnosc Jan 16 '24 at 19:56program
is available or not, to proceed accordingly... – jarnosc Jan 16 '24 at 20:07pdfroff
, which is a shell script, on busybox on windoze, and the script could not find windoze'sgswin64c
because the path has a space in it. By inspecting the code, I found a baroque functionsearchpath()
which does some checking, and reports installation errors to the user. For most *ix people this must be very weird, but makes sense as the script was designed to run on either OS, but not in a hybrid system. Now I see thathash
does the job, and that the script needs indeed some simplification. – jarnosc Jan 17 '24 at 22:08searchpath
has to determine not only that the program exists, but also the name under which it is available... – jarnosc Jan 18 '24 at 17:24