linux foundation week 4

Posted by Skara on Sun, 08 Dec 2019 22:16:17 +0100

1. Count the number of users whose default shell is not / sbin/nologin in / etc/passwd file, and display the users

[root@localhost ~]# awk -F: -v i="0" '$NF !~ "/sbin/nologin"{print ++i,$1,$NF}' /etc/passwd 
1 root /bin/bash
2 sync /bin/sync
3 shutdown /sbin/shutdown
4 halt /sbin/halt

2. Find out the user name, uid and shell type with the maximum UID

[root@localhost ~]# awk -F: '{print $3}' /etc/passwd | sort -rn
999
998
192
99
89
81
74
14
12
11
8
7
6
5
4
3
2
1
0
[root@localhost ~]# awk -F: '$3=="999"{print $1,$3,$NF}' /etc/passwd
polkitd 999 /sbin/nologin

3. Count the number of IP connections of each remote host currently connected to the local machine, and sort them from large to small

[root@localhost ~]# ss -nt |awk -F" +|:" '{print $(NF-2)}' |sort -rn |uniq -c 
      2 192.168.3.7
      1 Address

4. Write the script createuser.sh to realize the following functions: use a user name as a parameter, if the user of the specified parameter exists, it will display its existence, otherwise it will be added; display the id number of the added user and other information

#!/bin/bash

read -p "please input a username:" un
id $un &> /dev/null
if [[ $? == 0 ]];then
    echo "user is already exit!"
else
    useradd $un
    id $un
fi

5. Write the script in the basic format of the generated script, including the author, contact information, version, time, description, etc

#!/bin/bash

read -p "input the filename:" name
read -p "input version of the script:" version
read -p "input the describe:" describe
touch $name
echo "#!/bin/bash

#**************************************************************
#Filename:     *         *     $name                 
#Author:        *       *      zfc                   
#Contact:        *     *       804870471              
#version:         *   *        $version                
#Date:             * *         `date "+%Y-%m-%d"`       
#Describe           *          $describe
#**************************************************************" > $name
chmod +x $name

Topics: Operation & Maintenance shell