开发者

How can I check if ldconfig is in the PATH, using bash?

I have scrapped a perl snippet off the web for use in my bash scrip and for reasons too long to go into, it will be better if I could achieve what it tries to do directly in bash.

Here is the script:

bash stuff
...
perl <<'EOF'

use 5.006;
use strict;
use warnings;

if (! can_run("ldconfig")) {
    die "you need to have ldconfig in your PATH env to proceed.\n";
}

# check if we can run some command
sub can_run {
    my ($cmd) = @_;

    #warn "can run: @_\n";
    my $_cmd = $cmd;
    开发者_如何学编程return $_cmd if -x $_cmd;

    return undef;

EOF
more bash stuff

Basically, the question could be rephrased as , "how can I check if ldconfig is in the PATH env using bash?"


You want bash's builtin type command:

if type -P ldconfig; then
  echo "ldconfig is in the PATH"
else
  echo "ldconfig is not in the PATH"
fi

Expressed negatively:

if ! type -P ldconfig; then
  echo "ldconfig is not in the PATH"
fi


A more straightforward solution would be to invoke the shell and the which command:

$path = `which ldconfig`;

if ($path) {
  ...
}

If ldconfig is recognised, the path to its executable will be returned, empty output otherwise.

Or, if this Perl script is not going to do anything more than that, you can dismiss it and execute the same command from bash.


I refined @glenn jackman's answer to make it "quiet". It worked as it was but it output "/sbin/ldconfig" to the screen in addition to the echo when in the path. With this modification, only the echo is output:

type ldconfig &>/dev/null

if [ "$?" -eq 0 ]
then
  echo "ldconfig is in the PATH"
else
  echo "ldconfig is not in the PATH"
fi

Thanks to all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜