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

[root@localhost ~]# grep -v sbin/nologin /etc/passwd
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
edward:x:1000:1000:edward:/home/edward:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash
slackware:x:2002:2019:slackware name,depops,123,456:/home/slackware:/bin/tcsh
test1:x:2003:2003::/home/test1:/bin/bash
test2:x:2004:2004::/home/test2:/bin/bash
test3:x:2005:2005::/home/test3:/bin/bash
test4:x:2006:2006::/home/test4:/bin/bash
[root@localhost ~]# grep -v sbin/nologin /etc/passwd |wc -l
11
[root@localhost ~]# grep -v sbin/nologin /etc/passwd |cut -d: -f1,7
root:/bin/bash
sync:/bin/sync
shutdown:/sbin/shutdown
halt:/sbin/halt
edward:/bin/bash
mageia:/bin/bash
slackware:/bin/tcsh
test1:/bin/bash
test2:/bin/bash
test3:/bin/bash
test4:/bin/bash
[root@localhost ~]#

 


查出用户 UID 最大值的用户名、UID 及 shell 类型。

[root@localhost ~]# cat /etc/passwd | sort -t: -k 4 -n -r | cut -d: -f1,3,7  | head -1
nfsnobody:65534:/sbin/nologin

[root@localhost ~]# getent passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7
nfsnobody:65534:/sbin/nologin

[root@localhost ~]# sort -rnt ':' -k 3 /etc/passwd | cut -d : -f 1,3,7  |head -1
nfsnobody:65534:/sbin/nologin

[root@localhost ~]#

统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序。

[root@localhost ~]# netstat -tun |grep "[0-9]" |tr -s " " ":" | cut -d: -f6 |sort |uniq -c |sort -nr
      2 172.18.0.104
[root@localhost ~]# 

编写脚本 createuser.sh ,实现如下功能:
使用一个用户名做为参数;
如果指定参数的用户存在,就显示其存在,否则添加之;
显示添加的用户的id号等信息。

#!/bin/bash
  read -p "Please enter the username:" username
  id $username &> /dev/null
  if [ $? -eq 0 ]; then
      echo "The user name \"$username\" is exist."
  else
      useradd $username &> /dev/null
      read -p "Please enter the password:" passwd
      echo $passwd | passwd --stdin $username  &> /dev/null
      echo "======================================="
      echo "***  $username is create successful!  ***"
      echo "Details :"
      getent passwd $username
      echo "======================================="
  fi
[root@localhost ~]# ./createuser.sh 
Please enter the username:a6
Please enter the password:123123
=======================================
***  a6 is create successful!  ***
Details :
a6:x:2012:2012::/home/a6:/bin/bash
=======================================
[root@localhost ~]# 

编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等。

[root@localhost ~]# vim  ~/.vimrc

autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
    if expand("%:e")=='sh'
            call setline(1,"#!/bin/bash")
            call setline(2,"#")
            call setline(3,"#==============================================")
            call setline(4,"#  Author:          Edward Han")
            call setline(5,"#  Email:           1234567890@qq.com")
            call setline(6,"#  Version:         1.0")
            call setline(7,"#  Date:            ".strftime("%Y-%m-%d"))
            call setline(8,"#  Description:     Add a description here.")
            call setline(9,"#==============================================")
    endif
endfunc
[root@localhost ~]# vim a.sh
#!/bin/bash
#
#==============================================
#  Author:          Edward Han
#  Email:           1234567890@qq.com
#  Version:         1.0
#  Date:            2020-05-09
#  Description:     Add a description here.
#==============================================