文本处理与shell脚本基础练习

2020-02-20  本文已影响0人  jamas

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

grep -v "/sbin/nologin"$ /etc/passwd | cut -d: -f1 | cat -n

验证结果:

[root@node1 ~]# grep -v "/sbin/nologin"$ /etc/passwd | cut -d: -f1 | cat -n
1 root
2 sync
3 shutdown
4 halt
5 mao9

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

cat /etc/passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7

验证结果:

[root@node1 ~]# cat /etc/passwd | sort -t: -k3 -n | tail -1 | cut -d: -f1,3,7
mao9:1000:/bin/bash

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

netstat -tn | grep "ESTABLISHED" | tr -s " " : | cut -d: -f4 | sort | uniq -c | sort -nr

验证结果:

[root@node1 ~]# netstat -tn | grep "ESTABLISHED" | tr -s " " : | cut -d: -f4 | sort | uniq -c | sort -nr
4 172.16.2.131

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

#!/bin/bash
#
[ $# -eq 0 ] && { echo "please type the username ";exit 2; }
id $1 &> /dev/null
if [ $? -eq 0 ] ;then
   echo "$1 has exist."
else
   useradd $1 &> /dev/null && id $1
fi

验证结果:

[root@node1 test]# ./createuser.sh
please type the username
[root@node1 test]# ./createuser.sh root
root has exist.
[root@node1 test]# ./createuser.sh mao9
mao9 has exist.
[root@node1 test]# ./createuser.sh mao99
uid=1001(mao99) gid=1001(mao99) groups=1001(mao99)

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

vim init.sh
#!/bin/bash
#
cat >> ~/.vimrc <<EOF
set ignorecase
set cursorline
set autoindent
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:        xxx") 
    call setline(5,"#QQ:            1234567890") 
    call setline(6,"#Date:          ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName:      ".expand("%"))
    call setline(8,"#URL:           http://www.magedu.com")
    call setline(9,"#Description:       The test script") 
    call setline(10,"#Copyright (C):    ".strftime("%Y")." All rights reserved")
    call setline(11,"#********************************************************************") 
    call setline(12,"") 
    endif
endfunc
autocmd BufNewFile * normal G

验证结果:


snipaste_20200220_181726.png
上一篇 下一篇

猜你喜欢

热点阅读