如何编写一个shell for循环与case结合的脚本

这篇文章主要讲解了“如何编写一个shell for循环与case结合的脚本”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何编写一个shell for循环与case结合的脚本”吧!

核心代码:

代码如下:

#/bin/bash
set -x
HOSTS="nginx mysql php-cgi"
for myhost in $HOSTS
  do
  count=(`ps aux |grep $myhost |grep -v grep |wc -l`)
  echo "$myhost"
  echo "$count"

if [ $count -eq 0 ]; then
  case $myhost in
  nginx)
  cd /usr/local/webserver/nginx/sbin/
  ./nginx
  echo "nginx has be down"
  sleep 5

  mysql)
  /etc/init.d/mysqld start
  echo "mysql has be down"

  *)
  echo "what‘s the hell?"

esac
  fi
done
set +x

感谢各位的阅读,以上就是“如何编写一个shell for循环与case结合的脚本”的内容了,经过本文的学习后,相信大家对如何编写一个shell for循环与case结合的脚本这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!