centos7系统有关shell命令的应用
admin
2023-02-03 06:00:08
0次

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

[root@centos7 ~]# grep -v "/sbin/nologin" /etc/passwd |wc -l && grep -v "/sbin/nologin" /etc/passwd |cut -d ":" -f1

15

root

sync

shutdown

halt

rick

gentoo

docker

mongodb

redis

tomcat

nginx

ric

mageia

slackware

test

[root@centos7 ~]#
2、查出用户UID最大值的用户名、UID及shell类型

方法一:

[root@centos7 ~]# cat /etc/passwd |sort -t: -k3 -nr |head -1 |cut -d: -f1,3,7

方法二:

[root@centos7 ~]# cat max_uid.sh

#!/bin/bash

uid=`cat /etc/passwd |cut -d ":" -f3`

for i in $uid

do

        max=0

        if [ $i -gt $max ]

        then

                max=$i

        fi

done

grep $max /etc/passwd |cut -d ":" -f1,3,7

[root@centos7 ~]# sh max_uid.sh     

test:2003:/bin/bash

[root@centos7 ~]# tail -3 /etc/passwd

mageia:x:1100:1100::/homenux:/bin/bash

slackware:x:2002:2019::/home/slackware:/bin/tcsh

test:x:2003:2003::/home/test:/bin/bash

[root@centos7 ~]#
3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@centos7 ~]#ss -tnla |grep ESTAB |tr -s " " |cut -d " " -f5 |sort |uniq -c |sort -nr |head

    119 127.0.0.1:3306

     40 ::ffff:127.0.0.1:3306

     21 ::ffff:59.39.91.244:3306

      4 ::ffff:127.0.0.1:60009

      2 127.0.0.1:55555

      1 ::ffff:61.146.78.146:64634

      1 ::ffff:61.143.130.174:30895

      1 ::ffff:59.39.91.244:58830

      1 ::ffff:59.39.91.244:58818

      1 ::ffff:59.39.91.244:58263
4、编写脚本 centos7系统有关shell命令的应用createuser.sh,实现如下功能:使用一个用户名做为参数,如果 指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等 信息

[root@centos7 ~]# cat createuser.sh

#!/bin/bash

#********************************************************************

#Author:                rickzhu

#QQ:                    1779526363

#Date:                  2020-02-28

#FileName:             createuser.sh

#URL:                   http://www.magedu.com

#Description:          The test script

#Copyright (C):         2020 All rights reserved

#********************************************************************

if grep $1 /etc/passwd >/dev/null

then

        echo "$1 is exist."

else

        useradd $1

        echo "`id $1`"

fi

[root@centos7 ~]# sh createuser.sh nginx

nginx is exist.

[root@centos7 ~]# sh createuser.sh test

uid=2003(test) gid=2003(test) groups=2003(test)

[root@centos7 ~]#
5、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@centos7 ~]# cat createScript.sh

#!/bin/bash

#

#判断当前目录是否有同名的脚本

if ls |grep $1 >>/dev/null

then

        echo "$1 is exist" && exit 10

fi

 

cat < $1 && vim + $1

#!/bin/bash

#

#********************************************************************

#Author:                rickzhu

#QQ:                    1779526363

#Date:                  `date +%F`

#FileName:             $1

#Version                1

#URL:                   http://www.magedu.com

#Description:          The $1 script

#Copyright (C):         2020 All rights reserved

#********************************************************************

EOF

[root@centos7 ~]#

[root@centos7 ~]# sh createScript.sh test.sh

#!/bin/bash

#

#********************************************************************

#Author:                rickzhu

#QQ:                    1779526363

#Date:                  2020-02-28

#FileName:             test.sh

#Version                1              

#URL:                   http://www.magedu.com

#Description:          The test.sh script

#Copyright (C):         2020 All rights reserved

#********************************************************************                                               

~     


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...