serilain
serilain

Reputation: 451

bash test for yum or apt based linux

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

Answers (3)

Ian Kelling
Ian Kelling

Reputation: 10021

command -v apt-get &>/dev/null

Upvotes: 1

demure
demure

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

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

I'd probably use which:

[ -n "$(which apt-get)" ]
[ -n "$(which yum)" ]

Upvotes: 2

Related Questions