AI智能
改变未来

shell菜单练习2

#!/bin/bash#author leo#2020-04-13 09:22:11#!/bin/bash#设置显示颜色color_success=\"echo -en \\\\033[1;32m\"color_faile=\"echo -en \\\\033[1;31m\"color_waining=\"echo -en \\\\033[1;34m\"color_normal=\"echo -en \\\\033[1;39m\"local_time=$(date +\"%Y%m%d %H:%M:%S\")r_col=\'\\033[31m\'no_col=\'\\033[0m\'g_col=\'\\033[32m\'p_col=\'\\033[35m\'b_col=\'\\033[34m\'line1=\'++++++++++++++++++++++++\'line2=\'********************************\'line3=\'--------------------------------\'#主菜单main_menumain_menu(){clearcat << EOF$line1$local_time$(echo -e \"\\033[47;32m$p_col         主菜单         $no_col\\033[0m\")$line1`echo -e \"\\033[31m (1)\\033[0m系统信息菜单\"``echo -e \"\\033[31m (q)\\033[0m按q退出\"`$line1EOFread -p \"输入选择菜单:\" input1case $input1 in1)clearinfo_menu;;Q|q)clearexit 0;;*)echo \"----------------\"echo \"输入正确的选项\"echo \"----------------\"sleep 1clearmain_menuesac}#系统信息菜单info_menu(){NIC_name=$( ifconfig | head -1 | cut -d : -f1 )Ip_info=$(ifconfig | head -2 | grep inet | tr -s \" \")Mem=$(free -m | grep Mem | tr -s \" \" | cut -d \" \" -f2)Mem_toal=$(free -m | grep Mem | tr -s \" \" | cut -d \" \" -f7)Free_disk=$(df -h  | grep /$ | tr -s \" \" | cut -d \" \" -f 4)Cpu=$(cat /proc/loadavg | cut -d \' \' -f3)Cpu_num=$(cat /proc/cpuinfo | grep \"cpu cores\" | head -n 1 | cut -d \" \" -f3)Login_num=$(who | wc -l)Total_num=$(cat /etc/passwd | wc -l)hostname=`hostnamectl | grep Sta |awk \'{print $3}\'`linux=`awk -F \'release \' \'{print $2}\' /etc/redhat-release |cut -d \' \' -f1`iptables=`systemctl status firewalld.service | grep \"Active\" | awk \'{print $3 }\'`j=$(java -version 2>&1  | awk \'NR==1{print $3}\')selinux=`getenforce`n=`awk -F \':\' \'$3>=1000\' /etc/passwd |wc -l`cat << EOF$line2$local_time$line2$(echo -e \"\\033[47;31m           系统信息菜单        \\033[0m\")$line2网卡名:$NIC_nameip地址信息:$Ip_info内存总量:$Mem M剩余内存为:$Mem_toal Mcpu核数:$Cpu_num      cpu每十五分钟平均负载:$Cpu主机名:$hostnamelinux版本:$linux当前系统用户为:$USER    当前登录账户数:$Login_num  当前系统账户总数:$Total_num  当前系统 普通账户总数:$nselinux状态:$selinux防火墙:$iptablesjava信息:$j$line3`echo -e \"\\033[47;31m     系统信息修改菜单            \\033[0m\"`$line3$line2(1) 修改主机名(2) 修改IP地址(3) 关闭防火墙(4) 关闭SELINUX安全机制(5) ping网络IP(6) 安装/卸载Java(X)按b返回主菜单(q) 按q退出*******************************EOFread -p \"输入选择修改:\" input2case $input2 in1)read -p \"输入主机名:\"   inamehostnamectl set-hostname $inamesleep 1info_menu;;2)#file=/etc/sysconfig/network-scripts/ifcfg-$NIC_name#oldip=`ifconfig | head -2 | grep inet | tr -s \" \" | cut -d \' \' -f 3`#read -p \"请输入ip地址:\" ipnum#sed -i \'s/IPADDR=$oldip/IPADDR=$ipnum/g\' $fileecho \"还是不要改了\"sleep 1info_menu;;3)systemctl stop firewalld.servicesystemctl disable firewalld.servicesleep 1clearinfo_menu;;4)sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/selinux/configsetenforce 0sleep 1clearinfo_menu;;5)read -p \"输入要ping的网络IP前三位: \"  ipregex=\"\\b(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\"ckStep2=`echo $ip | egrep $regex | wc -l`if [ $ckStep2 -eq 0 ];thenecho \"The string $IPADDR is not a correct ipaddr!!!\"fim_ping() {ping -c2 -i0.1 -W1 $1  &>/dev/nullif [ $? -eq 0 ];thenecho \"$1 is up\"fi}#控制进程数量num=100#创建命令管道文件,并绑定文件描述符,创建其文件描述,通过重定向将数据导入管道文件pipefile=\"/tmp/multiping_$$.tmp\"mkfifo $pipefileexec 12<>$pipefilefor i in `seq $num`doecho \"\" >&12 &donefor j in {1..254}doread -u12{m_ping $ip.$jecho \"\" >&12} &donewaitrm -rf $pipefileinfo_menu;;6)#判断是否有yum源并配置阿里源test_yum(){yum clean all &>/dev/nullnum=$(yum repolist -e 0 | awk \'/repolist/{print $2}\' | sed \'s/,//\')if [ $num -le 0 ];then$color_faileecho -n \"error,,,,,,有没yum源\"echo \"配置阿里yum源\"ping www.baidu.com > /dev/null 2>&1if [ $? -ne 0 ];thenecho \"网络不通,请先配置网络\"info_menuelsewget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repofi$color_normalinfo_menuelse$color_successecho -n \"success,,,,,,有yum源\"$color_normalfi#判断是否Java并卸载test_java(){if [ $j -ne 0 ];then$color_faileecho -n \"error,,已经安装Java。请先卸载\"$color_normalecho \"当前Java版本:$j\"read -p \"是否卸载当前版本Java$j:y/n\" aif [ $a -eq y ];thenold_java=$(rpm -qa | grep jdk)for i in $old_javadoyum -y remove $i > /dev/null 2>&1rpm -e --nodeps $i > /dev/null 2»&1doneJava -version  > /dev/null 2>&1if [ $? -eq 1 ];thenecho \"java已经卸载 \"info_menufielseinfo_menufifi}#安装函数java_install(){yum -y install java-$1.0-openjdk.x86_64 >/dev/nullcat >> /etc/profile <<- EOF JAVA_HOME=/usr/lib/jvm/java-$1.0-openjdk.x86_64PATH=$PATH:$JAVA_HOME/binCLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport JAVA_HOME  CLASSPATH  PATHEOFsource  /etc/profilejava -version > /dev/null 2>&1if [ $? -eq 0 ];thenecho \"java已经安装 \"fi#安装菜单info_menu}java_menu(){case  $1 in1)java_install 1.6;;2)java_install 1.7;;3)java_install 1.8;;B|b)clearinfo_menu;;*)echo \"输入正确的选项\"clearinfo_menuesac}echo \"------------\"echo \"+----yum---+\"echo \"|  (1) 1.6 |\"echo \"|  (2) 1.7 |\"echo \"|  (3) 1.8 |\"echo \"|(b)按b返回|\"echo \"+----------+\"read -p \"选择需要安装的版本:\" numtest_yumtest_javajava_menu $numB|b)clearmain_menu;;Q|q)clearexit 0;;*)echo \"----------------\"echo \"输入正确的选项\"echo \"----------------\"sleep 1clearinfo_menuesac}main_menu
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » shell菜单练习2