We (the company I work for) need to run the find2perl script on over a thousand different Unix servers of different flavors (Linux, Solaris, HP-UX, AIX) and different versions.
The one thing that all the servers have in common, is that they all have at least one implementation of perl installed. However, not all systems have it configured the same way.
Finding the location of perl is easy enough using the which command. However, on 70% of the servers, the actual directory containing find2perl (the bin folder of perl) is not present in the $PATH variable and can’t be located that way.
On some servers, perl is actually a symbolic link pointing another location, in which case I can use ls -l and sed to extract the target of the link to find where perl is actually installed.
On other servers however, it’s more complicated, as it seems perl was compiled to a custom location and the binary of perl present in /bin or /usr/bin (or wherever perl is found) is not a symbolic link, but rather a full blown executable. In this case, I thought about using the @INC variable of perl to try to find find2perl but it seems rather excessive.
What would be the better/best/fullproof method (one-liner if possible) to always get the location of find2perl on a Unix system?
Ways to locate
find2perlTwo ways, both of which rely on asking the perl install how it was configured:
Config.pm
Its probably
scriptdirexpfromConfig.pm.And indeed, that’s where find2perl is on my system. You can
use Config;in your perl scripts, which is its major advantage over the next method.perl -V:varname
As per Yanick Girouard’s comment, you can also use
perl -V:scriptdirexpto get this, in a format suitable to passing toevalin a shell script. There are actually several formats available (so, you don’t need to use e.g., cut to parse it):Full documentation is in the
perlrunmanpage.Ways to embed
find2perlIf you decide to copy over
find2perl, as per evil otto’s comment, you can actually do that by embedding it in your shell script. There are many ways. If neither of the two below work, then you can certainly useshar(which has an extremely long history, and is likely compatible with everything).Quoted here-document
The easiest way is if your shell supports quoted here-documents. They all should, as its a POSIX requirement:
Hex dump in a non-quoted here-document
If some of your shells don’t implement quoted here-documents (POSIX‽ what’s that!), then you have to protect find2perl from shell expansion. An easy way is to hex dump it, as 0–9 and a–f are all safe from shell expansion. The dump is easily done with
xxd -p /usr/bin/find2perl, which only requiresxxdon one machine. To read back the dump, you can use plain perl:Using
find2perlseveral timesNaturally, with either approach, you could also write find2perl to a temporary file (if you need to invoke it multiple times, for example). You could also embed it in a shell function.