Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a script that combines shell for loop with case

Shulou Source: shulou.com Published: 2022-06-03 07:40:43 09月30日 Update

This article mainly explains "how to write a script that combines shell for loop and case". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to write a script that combines shell for loop and case".

Core code:

The code is as follows:

# / 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

Thank you for reading, the above is the content of "how to write a script that combines shell for loop and case". After the study of this article, I believe you have a deeper understanding of how to write a script that combines shell for loop and case, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Scripts loops learning code content that is ideas situations articles more core knowledge knowledge points articles follow questions practice push research verification Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info NVidia OPPO Reno Apple