2

I have a little script in bash which checks if the server is a cPanel server

#!/bin/bash
echo "checking"
x=$(/usr/local/cpanel/cpanel -V)
if [ "$x" ]; then
        echo "yes"
else
        echo "no"
fi

This works fine and gives the correct expected output. However, when run on non-cPanel servers, it also throws the following error:

./testcpanel.sh: line 2: /usr/local/cpanel/cpanel: No such file or directory I would like to suppress this output.

I tried inserting 2>&1 /dev/null in the variable declaration but that doesn't work. It always gives yes as the output irrespective of whether cPanel exists on not.

how do I go about suppressing the output? (OS's tested on - CentOS 6 and Ubuntu 13.04)

rahuL
  • 3,330
  • 11
  • 54
  • 79
  • +1 for [not using `which`](http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script) to determine whether cpanel existed or not. – devnull Sep 21 '13 at 07:41

1 Answers1

4

Change the following line

x=$(/usr/local/cpanel/cpanel -V)

to

x=$(/usr/local/cpanel/cpanel -V 2>/dev/null)

Saying so would supress the error message (actually redirect it to /dev/null).

Additionally, say

if [ -z "$x" ]; then

instead. Upon inserting 2>&1 /dev/null in the variable declaration, you redirected STDERR to STDOUT and made the variable contain something like: ./testcpanel.sh: line 2: /usr/local/cpanel/cpanel: No such file or directory due to which you received a yes.

devnull
  • 118,548
  • 33
  • 236
  • 227