Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash test for yum or apt based linux

Tags:

bash

yum

apt-get

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 ...

like image 420
serilain Avatar asked May 16 '13 20:05

serilain


2 Answers

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.

like image 104
demure Avatar answered Sep 28 '22 10:09

demure


I'd probably use which:

[ -n "$(which apt-get)" ]
[ -n "$(which yum)" ]
like image 27
Ansgar Wiechers Avatar answered Sep 28 '22 08:09

Ansgar Wiechers