首页 > 技术文章 > 《shell条件测试语句,字符串测试apache是否开启》

linux-super-meng 2014-06-01 11:03 原文

  

还得我想了10分钟才明白”!=“和"-n"的用法区别,做个笔记捋一捋

 

第一种方法:测试apache是否开启?字符串测试

#!/bin/bash

web=`/usr/bin/pgrep httpd`

if [ -n "$web" ];  //$web返回值是否为空

then        

  echo "httpd is running"

else        

  /etc/init.d/httpd start

fi

第二种:

#!/bin/bash

web=`/usr/bin/pgrep httpd`

if [ "$web" !=“” ];      //$web返回值是否等于空

then        

  echo "httpd is running"

else        

  /etc/init.d/httpd start

fi

推荐阅读