Issue
What's the difference between
[ -e /usr/local/nagios ] && echo yes
and
if [ -e /usr/local/nagios ]; then echo yes
fi
And when is it right to use any of them?
Let's say I want to test if Nagios was compiled from source or installed through yum (the default installation location from source is /usr/local/nagios
and through YUM
it's /etc/nagios
) - then I will know where the plugins folder resides and it will allow me to pull the plugins from the Nagios server to the Nagios client and place them in the right directory.
So which approach is the best to achieve this goal?
Thanks in advance
Solution
[
is usually an other name for the command test
, just expecting a ]
as last argument. You could actually write the two commands:
test -e /usr/local/nagios && echo yes
and
if test -e /usr/local/nagios; then echo yes; fi
test
and [
are just shell commands performing checks and returning 0
or 1
, that's why you can use it like this [ -e file ] && echo exists
(or test -e file && echo exists
).
When writing a script a few line long, I usually find the use of if
clearer and more explicit.
As of your other question, relying on hard coded paths is usually a bad idea (you can install from source to another directory and yum
may install nagios somewhere else in the future or depending on some configuration). I suggest you try querying yum
to see if it has nagios installed (with yum info nagios
or yum list installed | grep nagios
for example). Then, if nagios wasn't installed with yum, but the binary is in your PATH
, you can try which nagios
which will give you the full path of the nagios
binary.
Answered By - Dettorer Answer Checked By - Dawn Plyler (WPSolving Volunteer)