Shell implementation Multilevel menu system Installation Maintenance script instance sharing _linux Shell

Source: Internet
Author: User
Tags sleep

Demo Effect:

1, the first level menu

2, Level two menu

3. Perform operation

Script reference:

Copy Code code as follows:

#!/bin/bash
#author Lic (Oldboy Linux student)
#date 1304
disk_no= "/DEV/SDA1"
Nginx_dir= "/usr/local/tdoa/nginx/sbin/nginx"
Mysql_dir= "/usr/local/tdoa/mysql/bin/mysqld_safe"
server1= "WEB"
Server2= "MYSQL"
Server1_start= "/usr/local/tdoa/nginx/sbin/nginx"
server1_stop= "Kill-quit ' Cat/usr/local/tdoa/logs/nginx/nginx.pid '"
server1_restart= "Kill-hup ' Cat/usr/local/tdoa/logs/nginx/nginx.pid '"
server2_start= "/etc/init.d/mysql START"
server2_stop= "/etc/init.d/mysql STOP"
server2_restart= "/etc/init.d/mysql Restart"
#date
Date= ' date + '%y-%m-%d%h:%m:%s '
#ip
Ipaddr= ' ifconfig eth0|grep ' inet addr ' |sed ' s/^.*addr://g ' |sed ' s/bcast:.*$//g '
#hostname
Hostname= ' Hostname-s '
#user
User= ' WhoAmI '
#disk_check
Disk_sda= ' Df-h | grep $DISK _no | awk ' {print $} '
#cpu_average_check
Cpu_uptime= ' Cat/proc/loadavg | Cut-c1-14 '
#process_check
function Process_check () {
For dir in $NGINX _dir $MYSQL _dir
Todo
process_count=$ (Ps-ef | grep "$dir" | grep-v grep | wc-l)
For service in Nginx MySQL
Todo
echo "$dir" |grep-q "$service"
If [$?-eq 0]
Then
If [$process _count-eq 0]
Then
echo "$service ............... [Not RUN]
Else
echo "$service ............... [RUNNING] "
Continue
Fi
Fi
Done
Done
}
DECLARE flag=0
Clear
While ["$flag"-eq 0]
Todo
echo "========================================"
Process_check
echo "========================================"
Cat << EOF
|-----------System Infomation-----------
| DATE: $DATE
| HOSTNAME: $HOSTNAME
| USER: $USER
| IP: $IPADDR
| disk_used: $DISK _SDA
| Cpu_average: $cpu _uptime
----------------------------------------
|****please Enter Your choice:[0-5]****|
----------------------------------------
(1) Configure $SERVER 1 Service
(2) Configure $SERVER 2 Service
(3) Configure Networke Service
(4) Change Passwd
(5) Logs
(0) Quit
Eof
Read-p "Please enter your choice[0-5]:" Input
Case $input in
#web Service
1)
Clear
While ["$flag"-eq 0]
Todo
Cat << EOF
----------------------------------------
|****please Enter Your choice:[0-3]****|
----------------------------------------
(1) Start $SERVER 1 Service
(2) Stop $SERVER 1 Service
(3) Restart $SERVER 1 Service
(0) Back
Eof
Read-p "Please enter your choice[0-3]:" INPUT1
Case $input 1 in
1)
Echo-e "\n>>>>>>>>>>> $DATE Start $SERVER 1" >>/log.txt
$SERVER 1_start 2>>/log.txt
if [$ = = 0];then
echo "Start $SERVER 1 ... ..... ..... ..... ..... ..... ..... ..... ...... ..... ...... ...... ... [OK] "
Else
echo "Start $SERVER 1 ... ..... ..... ..... ..... ..... ..... ..... ...... ..... ...... ...... ... [FAILED] "
Fi
Sleep 5
Clear
;;
2)
Echo-e "\n>>>>>>>>>>> $DATE Stop $SERVER 1" >>/log.txt
$SERVER 1_stop 2>>/log.txt
if [$ = = 0];then
echo "Stop $SERVER 1 ... ... ..... ..... ..... ..... ...... ...... ...... ..... ..... ...... ..... ... [OK] "
Else
echo "Stop $SERVER 1 ... ... ..... ..... ..... ..... ...... ...... ...... ..... ..... ...... ..... ... [FAILED] "
Fi
Sleep 5
Clear
;;
3)
ECHO-E "\n>>>>>>>>>>> $DATE restart $SERVER 1" >>/log.txt
/USR/LOCAL/TDOA/PHP/SBIN/PHP-FPM Restart 2>>/log.txt
$SERVER 1_restart 2>>/log.txt
if [$ = = 0];then
echo "Restart $SERVER 1 ..... ........ ....... ...... ....... ....... ...... ...... ... [OK] "
Else
echo "Restart $SERVER 1 ..... ........ ....... ...... ....... ....... ...... ...... ... [FAILED] "
Fi
Sleep 5
Clear
;;
0)
Clear
Break
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"
For i in ' seq-w 10-1 1 '
Todo
Echo-ne "\b\b$i";
Sleep 1;
Done
Clear
;;
Esac
Done
;;
#mysql Service
2)
Clear
While ["$flag"-eq 0]
Todo
Cat << EOF
----------------------------------------
|****please Enter Your choice:[0-3]****|
----------------------------------------
(1) Start $SERVER 2 Service
(2) Stop $SERVER 2 Service
(3) Restart $SERVER 2 Service
(0) Back
Eof
Read-p "Please enter your choice[0-3]:" Input2
Case $input 2 in
1)
Echo-e "\n>>>>>>>>>>> $DATE Start $SERVER 2" >>/log.txt
$SERVER 2_start 2>>/log.txt
if [$ = = 0];then
echo "Start $SERVER 2 ... ..... ..... ..... ..... ..... ..... ..... ...... ..... ...... ...... ... [OK] "
Else
echo "Start $SERVER 2 ... ..... ..... ..... ..... ..... ..... ..... ...... ..... ...... ...... ... [FAILED] "
Fi
Sleep 5
Clear
;;
2)
Echo-e "\n>>>>>>>>>>> $DATE Stop $SERVER 2" >>/log.txt
$SERVER 2_stop 2>>/log.txt
if [$ = = 0];then
echo "Stop $SERVER 2 ... ... ..... ..... ..... ..... ...... ...... ...... ..... ..... ...... ..... ... [OK] "
Else
echo "Stop $SERVER 2 ... ... ..... ..... ..... ..... ...... ...... ...... ..... ..... ...... ..... ... [FAILED] "
Fi
Sleep 5
Clear
;;
3)
ECHO-E "\n>>>>>>>>>>> $DATE restart $SERVER 2" >>/log.txt
$SERVER 2_restart 2>>/log.txt
if [$ = = 0];then
echo "Restart $SERVER 2 ..... ........ ....... ...... ....... ....... ...... ...... ... [OK] "
Else
echo "Restart $SERVER 2 ..... ........ ....... ...... ....... ....... ...... ...... ... [FAILED] "
Fi
Sleep 5
Clear
;;
0)
Clear
Break
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"
For i in ' seq-w 10-1 1 '
Todo
Echo-ne "\b\b$i";
Sleep 1;
Done
Clear
;;
Esac
Done
;;
#网络服务
3)
Clear
While ["$flag"-eq 0]
Todo
Cat << EOF
----------------------------------------
|****please Enter Your choice:[0-3]****|
----------------------------------------
(1) Restart network Service
(2) Configure network Service
(0) Back
Eof
Read-p "Please enter your choice[0-3]:" Input3
Case $input 3 in
1)
ECHO-E "\n>>>>>>>>>>> $DATE Restart Network" >>/log.txt
/etc/init.d/network Restart 2>>/log.txt
if [$ = = 0];then
echo "Restart Network ... ..... ....... ........ ........ ........ ....... ....... ... [OK] "
Else
echo "Restart Network ... ..... ....... ........ ........ ........ ....... ....... ... [FAILED] "
Fi
Sleep 5
Clear
;;
2)
Clear
Setup
Clear
;;
0)
Clear
Break
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"
For i in ' seq-w 10-1 1 '
Todo
Echo-ne "\b\b$i";
Sleep 1;
Done
Clear
;;
Esac
Done
;;
4)
Clear
While ["$flag"-eq 0]
Todo
Cat << EOF
----------------------------------------
|****please Enter Your choice:[0-3]****|
----------------------------------------
(1) Change ROOT Passwd
(2) Change Samba Passwd
(0) Back
Eof
Read-p "Please enter your choice[0-3]:" Input5
Case $input 5 in
1)
Echo-e "\n>>>>>>>>>>> $DATE change System administertor Root passwd\n" >>/ Log.txt
passwd Root 2>>/log.txt
if [$ = = 0];then
echo "Change ROOT Passwd ... ..... ..... ..... ..... ..... ..... ..... ..... ..... ...... ....... ... [OK] "
Else
echo "Change ROOT Passwd ... ..... ..... ..... ..... ..... ..... ..... ..... ..... ...... ....... ... [FAILED] "
Fi
Sleep 5
Clear
;;
2)
Echo-e "\n>>>>>>>>>>> $DATE change Samba Passwd" >>/log.txt
Smbpasswd-a Tdoa 2>>/log.txt
if [$ = = 0];then
echo "Change Samba Passwd ... ..... ..... ... ..... ..... ..... ...... ..... ...... ...... ....... ... [OK] "
Else
echo "Change Samba Passwd ... ..... ..... ... ..... ..... ..... ...... ..... ...... ...... ....... ... [FAILED] "
Fi
Sleep 5
Clear
;;
0)
Clear
Break
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"
Sleep 8
Clear
;;
Esac
Done
;;
5)
Clear
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "|"
Less/log.txt
echo "|"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
;;
0)
Clear
Exit 0
;;
*) echo "----------------------------------"
echo "|            Warning!!! |"
echo "|   Please Enter right choice! |"
echo "----------------------------------"
For i in ' seq-w 10-1 1 '
Todo
Echo-ne "\b\b$i";
Sleep 1;
Done
Clear
;;
Esac
Done

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.