Application of shell Command in centos7 system
1. Count the number of users whose default shell is not / sbin/nologin in the / etc/passwd file, and display all the users.
[root@centos7 ~] # grep-v "/ sbin/nologin" / etc/passwd | wc-l & & grep-v "/ sbin/nologin" / etc/passwd | cut-d ":"-F1
fifteen
Root
Sync
Shutdown
Halt
Rick
Gentoo
Docker
Mongodb
Redis
Tomcat
Nginx
Ric
Mageia
Slackware
Test
[root@centos7 ~] #
2. Find out the user name, UID and shell type of the maximum UID of the user
Method 1:
[root@centos7 ~] # cat / etc/passwd | sort-t:-K3-nr | head-1 | cut-d:-F1 Leng 3 Leng 7
Method 2:
[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
[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. Count the number of connections to each remote host IP that is currently connected to this machine, and sort them from large to small
[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. Write a script
Createuser.sh to achieve the following functions: use a user name as a parameter, if the user of the specified parameter exists, show its existence, otherwise add it; display information such as the id number of the added user
[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. Write a script that generates the basic format of the script, including author, contact information, version, time, description, etc.
[root@centos7 ~] # cat createScript.sh
#! / bin/bash
#
# determine whether the current directory has a script with the same name
If ls | grep $1 > > / dev/null
Then
Echo "$1 is exist" & & exit 10
Fi
Cat