显示本机IP并判断IP地址,批量创建用户,安装nginx服务并启动

题目:

完成一个shell脚本,脚本的作用。
1. 运行脚本可以显示出本机的ip地址
2. 如果ip地址中有3这个数字,那么就打印出当前的系统时间
3. 如果ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, ... magedu_100并且所有用户同属于magedu组
4. 打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
5. yum安装nginx服务,并且启动该服务
6. 一个脚本完成

1、编写脚本

[root@rocky8 scripts]#vim 2.sh 
#!/bin/bash
IP=$(ifconfig eth0 |sed -nr "2s/[^0-9]+([0-9.]+).*/\1/p")
echo "1.显示本机IP地址:"
echo $IP
if [[ $IP == "3.* || *.3" ]]; then  #判断IP地址中是否有3
    echo $(date)          #有3则打印系统时间
else                       #没有3则执行下面命令
    echo "2.创建用户组:"    #创建用户组
    groupadd magedu 
    echo "用户组magedu已创建"
    echo "3.批量创建100个用户:"      #批量创建用户
    for i in {01..100}; do 
        useradd magedu_$i -g magedu &> /dev/null
    echo "magedu_$i is created"
    done
fi

echo "4.显示可以登陆的用户:"
cat /etc/passwd |grep bash |cut -d: -f1

echo "5.安装nginx服务:"
. /etc/os-release

if [ $ID = "rocky" -o $ID = "centos" ];then  #判断是否是rocky系统
    echo OS version is rocky or centos
    yum -y install nginx 
elif [ $ID = "ubuntu" ];then          #判断是否是ubuntu系统
    echo OS version is Ubuntu
    apt update
    apt -y install nginx 
else
    echo "不支持OS"
    exit
fi
echo "nginx已完成安装"

echo "6.开始启动nginx服务:"
systemctl start nginx
echo "nginx服务已启动"

2、执行结果:

[root@rocky8 scripts]#bash 2.sh
1.显示本机IP地址:
11.0.1.10
2.创建用户组:
用户组magedu已创建
3.批量创建100个用户:
magedu_001 is created
magedu_002 is created
magedu_003 is created
magedu_004 is created
magedu_005 is created
magedu_006 is created
magedu_007 is created
magedu_008 is created
magedu_009 is created
magedu_010 is created
magedu_011 is created
magedu_012 is created
magedu_013 is created
magedu_014 is created
magedu_015 is created
magedu_016 is created
magedu_017 is created
magedu_018 is created
magedu_019 is created
magedu_020 is created
magedu_021 is created
magedu_022 is created
magedu_023 is created
magedu_024 is created
magedu_025 is created
magedu_026 is created
magedu_027 is created
magedu_028 is created
magedu_029 is created
magedu_030 is created
magedu_031 is created
magedu_032 is created
magedu_033 is created
magedu_034 is created
magedu_035 is created
magedu_036 is created
magedu_037 is created
magedu_038 is created
magedu_039 is created
magedu_040 is created
magedu_041 is created
magedu_042 is created
magedu_043 is created
magedu_044 is created
magedu_045 is created
magedu_046 is created
magedu_047 is created
magedu_048 is created
magedu_049 is created
magedu_050 is created
magedu_051 is created
magedu_052 is created
magedu_053 is created
magedu_054 is created
magedu_055 is created
magedu_056 is created
magedu_057 is created
magedu_058 is created
magedu_059 is created
magedu_060 is created
magedu_061 is created
magedu_062 is created
magedu_063 is created
magedu_064 is created
magedu_065 is created
magedu_066 is created
magedu_067 is created
magedu_068 is created
magedu_069 is created
magedu_070 is created
magedu_071 is created
magedu_072 is created
magedu_073 is created
magedu_074 is created
magedu_075 is created
magedu_076 is created
magedu_077 is created
magedu_078 is created
magedu_079 is created
magedu_080 is created
magedu_081 is created
magedu_082 is created
magedu_083 is created
magedu_084 is created
magedu_085 is created
magedu_086 is created
magedu_087 is created
magedu_088 is created
magedu_089 is created
magedu_090 is created
magedu_091 is created
magedu_092 is created
magedu_093 is created
magedu_094 is created
magedu_095 is created
magedu_096 is created
magedu_097 is created
magedu_098 is created
magedu_099 is created
magedu_100 is created
4.显示可以登陆的用户:
root
zhang
magedu_001
magedu_002
magedu_003
magedu_004
magedu_005
magedu_006
magedu_007
magedu_008
magedu_009
magedu_010
magedu_011
magedu_012
magedu_013
magedu_014
magedu_015
magedu_016
magedu_017
magedu_018
magedu_019
magedu_020
magedu_021
magedu_022
magedu_023
magedu_024
magedu_025
magedu_026
magedu_027
magedu_028
magedu_029
magedu_030
magedu_031
magedu_032
magedu_033
magedu_034
magedu_035
magedu_036
magedu_037
magedu_038
magedu_039
magedu_040
magedu_041
magedu_042
magedu_043
magedu_044
magedu_045
magedu_046
magedu_047
magedu_048
magedu_049
magedu_050
magedu_051
magedu_052
magedu_053
magedu_054
magedu_055
magedu_056
magedu_057
magedu_058
magedu_059
magedu_060
magedu_061
magedu_062
magedu_063
magedu_064
magedu_065
magedu_066
magedu_067
magedu_068
magedu_069
magedu_070
magedu_071
magedu_072
magedu_073
magedu_074
magedu_075
magedu_076
magedu_077
magedu_078
magedu_079
magedu_080
magedu_081
magedu_082
magedu_083
magedu_084
magedu_085
magedu_086
magedu_087
magedu_088
magedu_089
magedu_090
magedu_091
magedu_092
magedu_093
magedu_094
magedu_095
magedu_096
magedu_097
magedu_098
magedu_099
magedu_100
5.安装nginx服务:
OS version is rocky or centos
Last metadata expiration check: 0:39:09 ago on Mon 28 Nov 2022 12:11:06 AM CST.
Dependencies resolved.
=====================================================================================================
 Package                       Arch     Version                                    Repository   Size
=====================================================================================================
Installing:
 nginx                         x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream   566 k
Installing dependencies:
 nginx-all-modules             noarch   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    22 k
 nginx-filesystem              noarch   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    23 k
 nginx-mod-http-image-filter   x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    34 k
 nginx-mod-http-perl           x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    45 k
 nginx-mod-http-xslt-filter    x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    32 k
 nginx-mod-mail                x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    63 k
 nginx-mod-stream              x86_64   1:1.14.1-9.module+el8.4.0+542+81547229     appstream    84 k

Transaction Summary
=====================================================================================================
Install  8 Packages

Total download size: 870 k
Installed size: 2.0 M
Downloading Packages:
(1/8): nginx-filesystem-1.14.1-9.module+el8.4.0+542+81547229.noarch.  70 kB/s |  23 kB     00:00    
(2/8): nginx-all-modules-1.14.1-9.module+el8.4.0+542+81547229.noarch  67 kB/s |  22 kB     00:00    
(3/8): nginx-mod-http-image-filter-1.14.1-9.module+el8.4.0+542+81547 293 kB/s |  34 kB     00:00    
(4/8): nginx-mod-http-perl-1.14.1-9.module+el8.4.0+542+81547229.x86_ 321 kB/s |  45 kB     00:00    
(5/8): nginx-mod-http-xslt-filter-1.14.1-9.module+el8.4.0+542+815472 312 kB/s |  32 kB     00:00    
(6/8): nginx-mod-mail-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rp 318 kB/s |  63 kB     00:00    
(7/8): nginx-mod-stream-1.14.1-9.module+el8.4.0+542+81547229.x86_64. 368 kB/s |  84 kB     00:00    
(8/8): nginx-1.14.1-9.module+el8.4.0+542+81547229.x86_64.rpm         351 kB/s | 566 kB     00:01    
-----------------------------------------------------------------------------------------------------
Total                                                                536 kB/s | 870 kB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                             1/1 
  Running scriptlet: nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch              1/8 
  Installing       : nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch              1/8 
  Installing       : nginx-mod-http-image-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64   2/8 
  Running scriptlet: nginx-mod-http-image-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64   2/8 
  Installing       : nginx-mod-http-perl-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64           3/8 
  Running scriptlet: nginx-mod-http-perl-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64           3/8 
  Installing       : nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64    4/8 
  Running scriptlet: nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64    4/8 
  Installing       : nginx-mod-mail-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                5/8 
  Running scriptlet: nginx-mod-mail-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                5/8 
  Installing       : nginx-all-modules-1:1.14.1-9.module+el8.4.0+542+81547229.noarch             6/8 
  Installing       : nginx-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                         7/8 
  Running scriptlet: nginx-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                         7/8 
  Installing       : nginx-mod-stream-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64              8/8 
  Running scriptlet: nginx-mod-stream-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64              8/8 
  Verifying        : nginx-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                         1/8 
  Verifying        : nginx-all-modules-1:1.14.1-9.module+el8.4.0+542+81547229.noarch             2/8 
  Verifying        : nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch              3/8 
  Verifying        : nginx-mod-http-image-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64   4/8 
  Verifying        : nginx-mod-http-perl-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64           5/8 
  Verifying        : nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64    6/8 
  Verifying        : nginx-mod-mail-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                7/8 
  Verifying        : nginx-mod-stream-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64              8/8 

Installed:
  nginx-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                                                
  nginx-all-modules-1:1.14.1-9.module+el8.4.0+542+81547229.noarch                                    
  nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch                                     
  nginx-mod-http-image-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                          
  nginx-mod-http-perl-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                                  
  nginx-mod-http-xslt-filter-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                           
  nginx-mod-mail-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                                       
  nginx-mod-stream-1:1.14.1-9.module+el8.4.0+542+81547229.x86_64                                     

Complete!
nginx已完成安装
6.开始启动nginx服务:
nginx服务已启动
posted @ 2022-11-28 01:09  10ⁿ  阅读(169)  评论(0编辑  收藏  举报