第二周作业

#!/bin/bash
#
#***********************************************************
#Author:            yanli
#Date:              2022-08-05
#FileName:          week2works.sh
#Description:       1.显示出本机的ip地址
#                   2.若ip地址中有3这个数字,那么就打印出当前的系统时间
#                   3.若ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, ...magedu_100并且所有用户同属于magedu组
#                   4.打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
#                   5.yum安装nginx服务,并且启动该服务
#***********************************************************
#定义IP变量
IP=`ifconfig|sed -rn '2s/[^0-9]+([0-9.]+).*/\1/p'`
COLOR1="echo -e \E[1;36m"
COLOR2="echo -e \E[1;35m"
END="\E[0m"

#1.打印主机ip地址
ipprint(){
${COLOR1}"\n本机的ip地址为:${IP}\n"${END}
}

#2.判断ip地址是否含有3
judgeip(){
if [[ ${IP} =~ "3" ]];then
    ${COLOR1}"\n当前系统时间为:`date "+%F %T"`\n"${END}
else
    groupadd magedu
    for i in `seq -f "%02g" 0 100`;do
        useradd -g magedu magedu_${i}
        ${COLOR1}"\nmagedu_${i} was successfully created!\n"${END}
    done
fi
}

#3.打印可登录的用户
userprint(){
USER=`awk -F: '/bash$/{print $1}' /etc/passwd|xargs|awk -F ' ' '{print $1","$2}'`
${COLOR1}"\n/etc/passwd这个文件中可以登陆的用户有:${USER}\n"${END}
}

#4.yum安装nginx,并启动该服务                                                                           
install(){
systemctl status firewalld &> /dev/null
if [[ $? -eq 0 ]];then
    ${COLOR1}"\n防火墙未关闭,请关闭!\n"${END}
    systemctl disable --now firewalld &> /dev/null
    ${COLOR1}"\n防火墙已经关闭了,可以安装了!\n"${END}
else
    ${COLOR1}"\n防火墙处于关闭状态!\n"${END}
fi
setenforce 0 &> /dev/null
if [[ $? -eq 0 ]];then
    ${COLOR1}"\nselinux关闭!\n"${END}
fi

##安装yum源
yum -y install epel-release
if [[ $? -ne 0 ]];then
    ${COLOR1}"\nYUM源安装失败,请检查!\n"${END}
else
    ${COLOR1}"\nYUM源安装成功\n"${END}
fi
##更新yum
yum -y update
if [[ $? -ne 0 ]];then
    ${COLOR1}"\nYUM源更新失败,请检查!\n"${END}
else
    ${COLOR1}"\nYUM源更新成功\n"${END}
fi
##安装nginx
yum -y install nginx
if [[ $? -ne 0 ]];then
    ${COLOR1}"\nnginx安装失败,请检查!\n"${END}
else
    ${COLOR1}"\nnginx安装成功\n"${END}
fi
##启动服务
systemctl start nginx
${COLOR1}"\nnginx服务启动成功!\n"${END}
##使其自动启动
systemctl enable nginx &> /dev/null
}

while :;do
${COLOR2}"1.打印主机ip地址"${END}
${COLOR2}"2.判断ip是否包含3,打印系统时间或批量建立用户"${END}
${COLOR2}"3.打印可登录的用户"${END}
${COLOR2}"4.安装nginx"${END}
${COLOR2}"5.退出!"${END}
read -p "$(${COLOR2}'请输入你的选项:'${END})" option
case $option in
1)
  ipprint
  ;;
2)
  judgeip
  ;;
3)
  userprint
  ;;
4)
  install
  ;;
5)
  break
  ;;
*)
  ${COLOR2}"输入错误!"${END}
esac
done
  • rokey8运行示例:

  • centos7运行示例:


  转载请注明: 焱黎的博客 第二周作业

  目录