Reputation: 451
Simple question -- what's the canonical way to test in bash whether yum or apt-get are present? I'm writing a script that will run on client machines and install unison, then create a cron job for it ...
Upvotes: 8
Views: 6736
Reputation: 7617
With bash script (or sh, zsh, ksh) you could use the builtin command -v yum
and command -v apt-get
.
(yes, the command is named command)
Zero output means it isn't there, otherwise you get the commands path.
You could then use a test statement to test whether the result was null or not.
[ -n "$(command -v yum)" ]
[ -n "$(command -v apt-get)" ]
Note: A downside to @ansgar-wiechers's suggested which
is suppressing stderr for if 'yum' isn't present:
[ -n "$(which yum 2>/dev/null)" ]
An upside of which, is it isn't a shell builtin.
Upvotes: 15
Reputation: 200273
I'd probably use which
:
[ -n "$(which apt-get)" ]
[ -n "$(which yum)" ]
Upvotes: 2