#!/bin/bash
#
#********************************************************************
#Author: GL
#QQ:
#Date: 2022-11-27
#FileName: homework-week02.sh
#URL: https://www.cnblogs.com/glinux/
#Description: homework-week02
#Copyright (C): 2022 All rights reserved
#*******************************************************************
COLOR_RED='echo -e \E[1;31m'
COLOR_GREEN='echo -e \E[1;32m'
COLOR_YELLOW='echo -e \E[1;33m'
COLOR_END='\E[0m'
HOST_IP=`ifconfig -a | sed -n '2p' | tr -s ' ' | cut -d' ' -f3`
_menu (){
echo '***** week02 homework *****'
echo '1. 运行脚本可以显示出本机的ip地址'
echo '2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间'
echo '3. 如果ip地址中不含3这个数字,就批量建立用户magedu00,magede01...magedu100,并且所有用户同属于magedu组'
echo '4. 打印/etc/passwd这个文件中可以登录的用户(非/usr/sbin/nologin)'
echo '5. yum安装nginx服务,并且启动该服务'
read -p '' GET_INPUT
case $GET_INPUT in
1)
clear
echo '1. 运行脚本可以显示出本机的ip地址'
echo '*************************'
echo $HOST_IP
;;
2)
clear
echo '2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间'
echo '*************************'
echo $HOST_IP | grep 3 > /dev/null && date || echo '没有3这个数字'
;;
3)
clear
echo '3. 如果ip地址中不含3这个数字,就批量建立用户magedu00,magede01...magedu100,并且所有用户同属于magedu组'
echo '*************************'
_create_user
;;
4)
clear
echo '4. 打印/etc/passwd这个文件中可以登录的用户(非/usr/sbin/nologin)'
echo '*************************'
sed -r '/.*nologin$/d' /etc/passwd | grep -wv /usr/sbin/nologin
;;
5)
clear
echo '5. yum安装nginx服务,并且启动该服务'
echo '*************************'
_install_nginx
;;
esac
}
_create_user (){
sed -n ''/^magedu.*/p /etc/passwd | cut -d ':' -f1 > /root/1.txt
while read LINE;do userdel -r $LINE;done < /root/1.txt
echo magedu{00..10} | xargs -n1 useradd
echo '********** 用户创建完毕';sleep 2
cat -n /etc/passwd
}
_install_nginx (){
ss -nutlp | grep 80 >/dev/null ; n=`echo $?`
if [ $n -eq 0 ];then
echo 'nginx 服务已启动'
else
echo '服务未安装,正在安装中...'
rpm -qa | grep -w nginx || yum -y install nginx >/dev/null
/usr/sbin/nginx>/dev/null
ss -nutlp | grep 80 && $COLOR_GREEN"nginx 启动成功"$COLOR_END
fi
}
_main (){
clear
while [ 1 -eq 1 ];do
_menu
echo '5秒之后自动跳转...'
sleep 5;clear
done
}
#_create_user
#_menu
_main