Reputation: 1642
I am trying to check if md5sum or digest exists on solaris and script is used on different machines.
Here is the function in sh script which is called from a ksh script
getMD5cmd ()
{
PATH="${PATH}:/bin:/usr/bin:/usr/sfw/bin:/usr/local/bin:/usr/sbin/bin"
if type -p md5sum;then
MD5CMD=`type -p md5sum`
elif type -p digest;then
MD5CMD="`type -p digest` -a md5"
fi
echo "HERE ${MD5CMD}"
}
When I run scripts I get
-p not found
md5sum not found
-p not found
digest is /bin/digest
HERE
However, when I type it in a terminal, works as exptected
Any Ideas? Thanks
Upvotes: 2
Views: 3829
Reputation: 30803
As you are setting the PATH, knowing where precisely the command is seems unnecessary.
getMD5cmd ()
{
PATH=${PATH}:/bin:/usr/bin:/usr/sfw/bin:/usr/local/bin:/usr/sbin/bin
md5sum /dev/null >/dev/null 2>&1 && MD5CMD=md5sum || MD5CMD="digest -a md5"
echo "HERE ${MD5CMD}"
}
getMD5cmd
Upvotes: 1
Reputation: 359855
You are likely running ksh
or possibly Bash for your interactive shell. Both of these have a -p
option for type
. The shell (probably sh
) that your script is running in has type
but doesn't have the -p
option so it's looking for "-p" as the name of an executable and it doesn't find it.
So you could change your script to use ksh
or you could use the which
program. The latter is probably more portable, since some systems don't have ksh
.
Upvotes: 2
Reputation: 19717
if which md5sum >/dev/null 2>&1; then
md5cmd="md5sum"
elif which digest >/dev/null 2>&1; then
md5cmd="digest -a md5"
else
echo "No md5 command found" >&2
exit 1
fi
$md5cmd YOUR_FILE
Upvotes: 0
Reputation: 30595
Have you tried the following syntax:
MD5CMD="$(type -p md5sum digest |sed -e 's/digest$/digest -a md5/' |head -1)"
if [ -z "$MD5CMD" ]; then
echo 'no md5 sum command found' >&2
exit 1
fi
echo "HERE $MD5CMD"
I tried this in Cygwin and type
will return multiple rows, so it works.
Upvotes: 0