PDA

View Full Version : greping return code error



learnbash
August 15th, 2013, 07:21 AM
Hi folks,

I am running below code which is giving me output "httpd (no pid file) not running", how can i grep this code in shell script.

I have tried below code but it is not giving me error in echoing.




./webserver stop

if [ $? -eq 0 ]

echo " Everything is OK"

else

echo "Error code $0 and output"



error message command below part of error which i want to grep.



./webserver stop
httpd (no pid file) not running

DarkAmbient
August 15th, 2013, 11:23 AM
Not sure if you simplied provided code or, but you need a if then, and end if with an fi :)



if [ $? -eq 0 ]; then
echo " Everything is OK"
else
echo "Error code $0 and output"
fi


This would also work.


[ $? -eq 0 ] && echo "Everything is ok" || echo "Error code $0 and output"

You can save the outcome in a variable:


o=`./webserver stop 2>&1 > /dev/null`
[ $? -gt 0 ] && echo OUTPUT: $o

learnbash
August 15th, 2013, 12:18 PM
webservice is already stop but it is showing everything is ok.



#x=`./webserver stop 2>&1`

#echo $?
0

DarkAmbient
August 15th, 2013, 12:34 PM
What is the output of $x then? (In your example)

learnbash
August 15th, 2013, 01:54 PM
It is also showing value 0.

DarkAmbient
August 15th, 2013, 02:36 PM
What is the content of webserver then?

learnbash
August 15th, 2013, 06:48 PM
./webserver stop [ it is stopping weberver ]
./webserver start [ it is starting weberver ]





It is starting webserver and also stop webserver, both are valid arguments so that's why it i not showing anything. Actually my motive of writing script is that if httpd process is present in memory or not, if not then start the apache process but main problem is that i am not able to grep httpd process because its very difficult to get apache process looks like that.

DarkAmbient
August 15th, 2013, 11:56 PM
You could check if there is any process:


pgrep apache2 > /dev/null && echo Webserver up || echo Webserver down

or same but if


if pgrep apache2 > /dev/null; then
echo Webserver up
else
echo Webserver down
fi