第二周作业

2.就业班第二周作业

2.1脚本拆分

  • 运行脚本可以显示出本机的ip地址
#!/bin/bash
`ifconfig ens160 |grep mask |tr -s " " ":" |cut -d: -f3`
  • 如果ip地址中有3这个数字,那么就打印出当前的系统时间;如果ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, … magedu_100并且所有用户同属于magedu组~~~脚本
#!/bin/bash
 i=`ifconfig ens160 |grep mask |tr -s " " ":" |cut -d: -f3 |grep -o 3`
 j="3"
if

#	num=`ifconfig ens160 |grep mask |tr -s " " ":" |cut -d: -f3 |grep -o 3`
	[ $i -eq $j ]
then
	echo -e `date`

else
	        for i in {1..100};do
                groupadd -g 9999  mage
                useradd -g 9999  mage$i
                echo "user$i is created"
         done
fi
  • 打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)~~~脚本
#!/bin/bash
echo -e "$(cat /etc/passwd |grep -v "nologin$")\n"
    1. yum安装nginx服务,并且启动该服务~~~脚本
#!/bin/bash
yum -y install yum-until &> /dev/null
yum search nginx &> /dev/null
yum -y install nginx &> /dev/null
systemctl start nginx
systemctl status nginx

2.2第二周作业脚本

#!/bin/bash
echo -e "作业一:运行脚本可以显示出本机的ip地址"
echo -e "本机ip地址:`ifconfig ens160 |grep mask |tr -s " " ":" |cut -d: -f3`"
echo -e "-------------------------------------------------------------------------------"
echo -e "作业二:(if语句)"
#!/bin/bash
i=`ifconfig ens224 |grep mask |tr -s " " ":" |cut -d: -f3 |grep -o 3`
j="3"
if
        [ ${i} -eq ${j} ]
then
        echo -e `date`
else
        for i in {1..100};do
                groupadd -g 9999  mage
                useradd -g 9999  mage$i
                echo "user$i is created"
        done

fi
echo -e "-------------------------------------------------------------------------------"
echo -e "作业三:打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)"
#!/bin/bash
echo -e "$(cat /etc/passwd |grep -v "nologin$")\n"
echo -e "-------------------------------------------------------------------------------"
echo -e "作业四:yum安装nginx服务,并且启动该服务"
#!/bin/bash
yum -y install yum-until &> /dev/null
yum search nginx &> /dev/null
yum -y install nginx &> /dev/null
systemctl start nginx
systemctl status nginx

执行结果if条件1:当if 满足第一个条件时,执行结果如下;

[root@Centos8-Minimal ~]# ./a.sh
作业一:运行脚本可以显示出本机的ip地址
本机ip地址:10.0.0.128
-------------------------------------------------------------------------------
作业二:(if语句条件1示例)
Sun Oct 30 21:22:27 CST 2022
-------------------------------------------------------------------------------
作业三:打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

-------------------------------------------------------------------------------
作业四:yum安装nginx服务,并且启动该服务
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-10-30 21:21:33 CST; 56s ago
  Process: 2072 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 2032 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 2031 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 2074 (nginx)
    Tasks: 2 (limit: 11047)
   Memory: 10.3M
   CGroup: /system.slice/nginx.service
           ├─2074 nginx: master process /usr/sbin/nginx
           └─2076 nginx: worker process

Oct 30 21:21:33 Centos8-Minimal.org systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 30 21:21:33 Centos8-Minimal.org nginx[2032]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 30 21:21:33 Centos8-Minimal.org nginx[2032]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 30 21:21:33 Centos8-Minimal.org systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Oct 30 21:21:33 Centos8-Minimal.org systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@Centos8-Minimal ~]# 

脚本执行结果if条件2:当if满足第二个条件时,执行结果如下;

[root@Centos8-Minimal ~]# ./a.sh
作业一:运行脚本可以显示出本机的ip地址
本机ip地址:192.168.2.22
-------------------------------------------------------------------------------
作业二:(if语句)
./a.sh: line 10: [: -eq: unary operator expected
groupadd: group 'mage' already exists
user1 is created
groupadd: group 'mage' already exists
user2 is created
groupadd: group 'mage' already exists
user3 is created
groupadd: group 'mage' already exists
user4 is created
groupadd: group 'mage' already exists
user5 is created
groupadd: group 'mage' already exists
user6 is created
groupadd: group 'mage' already exists
user7 is created
groupadd: group 'mage' already exists
user8 is created
groupadd: group 'mage' already exists
user9 is created
groupadd: group 'mage' already exists
user10 is created
groupadd: group 'mage' already exists
user11 is created
groupadd: group 'mage' already exists
user12 is created
groupadd: group 'mage' already exists
user13 is created
groupadd: group 'mage' already exists
user14 is created
groupadd: group 'mage' already exists
user15 is created
groupadd: group 'mage' already exists
user16 is created
groupadd: group 'mage' already exists
user17 is created
groupadd: group 'mage' already exists
user18 is created
groupadd: group 'mage' already exists
user19 is created
groupadd: group 'mage' already exists
user20 is created
groupadd: group 'mage' already exists
user21 is created
groupadd: group 'mage' already exists
user22 is created
groupadd: group 'mage' already exists
user23 is created
groupadd: group 'mage' already exists
user24 is created
groupadd: group 'mage' already exists
user25 is created
groupadd: group 'mage' already exists
user26 is created
groupadd: group 'mage' already exists
user27 is created
groupadd: group 'mage' already exists
user28 is created
groupadd: group 'mage' already exists
user29 is created
groupadd: group 'mage' already exists
user30 is created
groupadd: group 'mage' already exists
user31 is created
groupadd: group 'mage' already exists
user32 is created
groupadd: group 'mage' already exists
user33 is created
groupadd: group 'mage' already exists
user34 is created
groupadd: group 'mage' already exists
user35 is created
groupadd: group 'mage' already exists
user36 is created
groupadd: group 'mage' already exists
user37 is created
groupadd: group 'mage' already exists
user38 is created
groupadd: group 'mage' already exists
user39 is created
groupadd: group 'mage' already exists
user40 is created
groupadd: group 'mage' already exists
user41 is created
groupadd: group 'mage' already exists
user42 is created
groupadd: group 'mage' already exists
user43 is created
groupadd: group 'mage' already exists
user44 is created
groupadd: group 'mage' already exists
user45 is created
groupadd: group 'mage' already exists
user46 is created
groupadd: group 'mage' already exists
user47 is created
groupadd: group 'mage' already exists
user48 is created
groupadd: group 'mage' already exists
user49 is created
groupadd: group 'mage' already exists
user50 is created
groupadd: group 'mage' already exists
user51 is created
groupadd: group 'mage' already exists
user52 is created
groupadd: group 'mage' already exists
user53 is created
groupadd: group 'mage' already exists
user54 is created
groupadd: group 'mage' already exists
user55 is created
groupadd: group 'mage' already exists
user56 is created
groupadd: group 'mage' already exists
user57 is created
groupadd: group 'mage' already exists
user58 is created
groupadd: group 'mage' already exists
user59 is created
groupadd: group 'mage' already exists
user60 is created
groupadd: group 'mage' already exists
user61 is created
groupadd: group 'mage' already exists
user62 is created
groupadd: group 'mage' already exists
user63 is created
groupadd: group 'mage' already exists
user64 is created
groupadd: group 'mage' already exists
user65 is created
groupadd: group 'mage' already exists
user66 is created
groupadd: group 'mage' already exists
user67 is created
groupadd: group 'mage' already exists
user68 is created
groupadd: group 'mage' already exists
user69 is created
groupadd: group 'mage' already exists
user70 is created
groupadd: group 'mage' already exists
user71 is created
groupadd: group 'mage' already exists
user72 is created
groupadd: group 'mage' already exists
user73 is created
groupadd: group 'mage' already exists
user74 is created
groupadd: group 'mage' already exists
user75 is created
groupadd: group 'mage' already exists
user76 is created
groupadd: group 'mage' already exists
user77 is created
groupadd: group 'mage' already exists
user78 is created
groupadd: group 'mage' already exists
user79 is created
groupadd: group 'mage' already exists
user80 is created
groupadd: group 'mage' already exists
user81 is created
groupadd: group 'mage' already exists
user82 is created
groupadd: group 'mage' already exists
user83 is created
groupadd: group 'mage' already exists
user84 is created
groupadd: group 'mage' already exists
user85 is created
groupadd: group 'mage' already exists
user86 is created
groupadd: group 'mage' already exists
user87 is created
groupadd: group 'mage' already exists
user88 is created
groupadd: group 'mage' already exists
user89 is created
groupadd: group 'mage' already exists
user90 is created
groupadd: group 'mage' already exists
user91 is created
groupadd: group 'mage' already exists
user92 is created
groupadd: group 'mage' already exists
user93 is created
groupadd: group 'mage' already exists
user94 is created
groupadd: group 'mage' already exists
user95 is created
groupadd: group 'mage' already exists
user96 is created
groupadd: group 'mage' already exists
user97 is created
groupadd: group 'mage' already exists
user98 is created
groupadd: group 'mage' already exists
user99 is created
groupadd: group 'mage' already exists
user100 is created
-------------------------------------------------------------------------------
作业三:打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mage1:x:1000:9999::/home/mage1:/bin/bash
mage2:x:1001:9999::/home/mage2:/bin/bash
mage3:x:1002:9999::/home/mage3:/bin/bash
mage4:x:1003:9999::/home/mage4:/bin/bash
mage5:x:1004:9999::/home/mage5:/bin/bash
mage6:x:1005:9999::/home/mage6:/bin/bash
mage7:x:1006:9999::/home/mage7:/bin/bash
mage8:x:1007:9999::/home/mage8:/bin/bash
mage9:x:1008:9999::/home/mage9:/bin/bash
mage10:x:1009:9999::/home/mage10:/bin/bash
mage11:x:1010:9999::/home/mage11:/bin/bash
mage12:x:1011:9999::/home/mage12:/bin/bash
mage13:x:1012:9999::/home/mage13:/bin/bash
mage14:x:1013:9999::/home/mage14:/bin/bash
mage15:x:1014:9999::/home/mage15:/bin/bash
mage16:x:1015:9999::/home/mage16:/bin/bash
mage17:x:1016:9999::/home/mage17:/bin/bash
mage18:x:1017:9999::/home/mage18:/bin/bash
mage19:x:1018:9999::/home/mage19:/bin/bash
mage20:x:1019:9999::/home/mage20:/bin/bash
mage21:x:1020:9999::/home/mage21:/bin/bash
mage22:x:1021:9999::/home/mage22:/bin/bash
mage23:x:1022:9999::/home/mage23:/bin/bash
mage24:x:1023:9999::/home/mage24:/bin/bash
mage25:x:1024:9999::/home/mage25:/bin/bash
mage26:x:1025:9999::/home/mage26:/bin/bash
mage27:x:1026:9999::/home/mage27:/bin/bash
mage28:x:1027:9999::/home/mage28:/bin/bash
mage29:x:1028:9999::/home/mage29:/bin/bash
mage30:x:1029:9999::/home/mage30:/bin/bash
mage31:x:1030:9999::/home/mage31:/bin/bash
mage32:x:1031:9999::/home/mage32:/bin/bash
mage33:x:1032:9999::/home/mage33:/bin/bash
mage34:x:1033:9999::/home/mage34:/bin/bash
mage35:x:1034:9999::/home/mage35:/bin/bash
mage36:x:1035:9999::/home/mage36:/bin/bash
mage37:x:1036:9999::/home/mage37:/bin/bash
mage38:x:1037:9999::/home/mage38:/bin/bash
mage39:x:1038:9999::/home/mage39:/bin/bash
mage40:x:1039:9999::/home/mage40:/bin/bash
mage41:x:1040:9999::/home/mage41:/bin/bash
mage42:x:1041:9999::/home/mage42:/bin/bash
mage43:x:1042:9999::/home/mage43:/bin/bash
mage44:x:1043:9999::/home/mage44:/bin/bash
mage45:x:1044:9999::/home/mage45:/bin/bash
mage46:x:1045:9999::/home/mage46:/bin/bash
mage47:x:1046:9999::/home/mage47:/bin/bash
mage48:x:1047:9999::/home/mage48:/bin/bash
mage49:x:1048:9999::/home/mage49:/bin/bash
mage50:x:1049:9999::/home/mage50:/bin/bash
mage51:x:1050:9999::/home/mage51:/bin/bash
mage52:x:1051:9999::/home/mage52:/bin/bash
mage53:x:1052:9999::/home/mage53:/bin/bash
mage54:x:1053:9999::/home/mage54:/bin/bash
mage55:x:1054:9999::/home/mage55:/bin/bash
mage56:x:1055:9999::/home/mage56:/bin/bash
mage57:x:1056:9999::/home/mage57:/bin/bash
mage58:x:1057:9999::/home/mage58:/bin/bash
mage59:x:1058:9999::/home/mage59:/bin/bash
mage60:x:1059:9999::/home/mage60:/bin/bash
mage61:x:1060:9999::/home/mage61:/bin/bash
mage62:x:1061:9999::/home/mage62:/bin/bash
mage63:x:1062:9999::/home/mage63:/bin/bash
mage64:x:1063:9999::/home/mage64:/bin/bash
mage65:x:1064:9999::/home/mage65:/bin/bash
mage66:x:1065:9999::/home/mage66:/bin/bash
mage67:x:1066:9999::/home/mage67:/bin/bash
mage68:x:1067:9999::/home/mage68:/bin/bash
mage69:x:1068:9999::/home/mage69:/bin/bash
mage70:x:1069:9999::/home/mage70:/bin/bash
mage71:x:1070:9999::/home/mage71:/bin/bash
mage72:x:1071:9999::/home/mage72:/bin/bash
mage73:x:1072:9999::/home/mage73:/bin/bash
mage74:x:1073:9999::/home/mage74:/bin/bash
mage75:x:1074:9999::/home/mage75:/bin/bash
mage76:x:1075:9999::/home/mage76:/bin/bash
mage77:x:1076:9999::/home/mage77:/bin/bash
mage78:x:1077:9999::/home/mage78:/bin/bash
mage79:x:1078:9999::/home/mage79:/bin/bash
mage80:x:1079:9999::/home/mage80:/bin/bash
mage81:x:1080:9999::/home/mage81:/bin/bash
mage82:x:1081:9999::/home/mage82:/bin/bash
mage83:x:1082:9999::/home/mage83:/bin/bash
mage84:x:1083:9999::/home/mage84:/bin/bash
mage85:x:1084:9999::/home/mage85:/bin/bash
mage86:x:1085:9999::/home/mage86:/bin/bash
mage87:x:1086:9999::/home/mage87:/bin/bash
mage88:x:1087:9999::/home/mage88:/bin/bash
mage89:x:1088:9999::/home/mage89:/bin/bash
mage90:x:1089:9999::/home/mage90:/bin/bash
mage91:x:1090:9999::/home/mage91:/bin/bash
mage92:x:1091:9999::/home/mage92:/bin/bash
mage93:x:1092:9999::/home/mage93:/bin/bash
mage94:x:1093:9999::/home/mage94:/bin/bash
mage95:x:1094:9999::/home/mage95:/bin/bash
mage96:x:1095:9999::/home/mage96:/bin/bash
mage97:x:1096:9999::/home/mage97:/bin/bash
mage98:x:1097:9999::/home/mage98:/bin/bash
mage99:x:1098:9999::/home/mage99:/bin/bash
mage100:x:1099:9999::/home/mage100:/bin/bash

-------------------------------------------------------------------------------
作业四:yum安装nginx服务,并且启动该服务
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2022-10-30 21:42:30 CST; 2min 35s ago
  Process: 1820 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 1818 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 1817 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 1822 (nginx)
    Tasks: 2 (limit: 11047)
   Memory: 11.9M
   CGroup: /system.slice/nginx.service
           ├─1822 nginx: master process /usr/sbin/nginx
           └─1823 nginx: worker process

Oct 30 21:42:30 Centos8-Minimal.org systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 30 21:42:30 Centos8-Minimal.org nginx[1818]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 30 21:42:30 Centos8-Minimal.org nginx[1818]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 30 21:42:30 Centos8-Minimal.org systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Oct 30 21:42:30 Centos8-Minimal.org systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@Centos8-Minimal ~]# 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值