Homework One: Organize your blog with awk, variables, operators, if multiple branches
Job Two: awk text processing
1, print the UID within the scope of 30~40 user name
[Email protected] ~]# awk-f: ' $3>=30 && $3<=40{print $ '/etc/passwdrpcntp
2. Print the line number and user name of the 第5-10 line
[Email protected] ~]# awk-f: ' nr>=5 && nr<=10{print NR, '-------', ' $ '/etc/passwd5-------lp6-------syn C7-------shutdown8-------halt9-------mail10-------operator
3. Print odd lines
4. Print even lines
[[email protected] ~]# awk '! (nr%2) {print NR, '----', ' $ Test2 '----BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN4----abominable:x:1009:1010::/home/abominable:/ BIN/BASH6----ANOMIE:X:1011:1012::/HOME/ANOMIE:/BIN/BASH8----ALEX213SB:X:1013:1014::/HOME/ALEX213SB:/BIN/BASH10 ----YH438PIG:X:1015:1016::/HOME/YH438PIG:/BIN/BASH12----Egon:x:1017:1002::/home/egon:/bin/bash
5. Lines with more than 5 print fields
[Email protected] ~]# awk-f: ' Nf>5{print $, '-----', NF} ' testroot-----7bin-----7nginx-----7abominable-----7AB Ominate-----7anomie-----7atomize-----7ALEX213SB-----7wpq2222b-----7yh438pig-----7egon666-----7egon-----7
6, the print UID is not equal to the GID user name
[Email protected] ~]# awk-f: ' $3!=$4{print $, '----', $4} ' test 991----5471009----10101010----10111011----101 21012----10131013----10141014----10151015----10161016----10171017----1002
7. Print users with no shell specified
[Email protected] ~]# awk-f: ' $NF!~/bash$/{print $} ' Testbin:x:1:1:bin:/bin:/sbin/nologinnginx:x:991:547:nginx Web Server:/var/lib/nginx:/sbin/nologin
Job Three: Shell scripting
1, automatic deployment, initial configuration, and start Nginx reverse proxy service
#!/bin/shngxstatus= ' ps aux | grep-v grep |grep-c nginx ' If [-e/usr/sbin/nginx];then Echo ' nginx already installed ' exit 110else Yum INS Tall epel-release-y-Q yum install gcc-* glibc-* OpenSSL openssl-devel pcre pcre-devel zlib zlib-devel-y-Q Yum Install nginx-y-Q echo "Install nginx successful" fiif [-f/etc/nginx/nginx.conf];then /bin/cp/etc/nginx/ngin X.conf/etc/nginx/nginx.conf.bak sed-i '/^http/a\\t upstream Luchuangao {\n\t Server web01;\n\t server web02;\n\t SE RVer web03;\n\t} '/etc/nginx/nginx.conf sed-i '/^ location/ {/a\\t\t proxy_pass Http:\/\/luchuangao; '/etc/ Nginx/nginx.conf echo "Configuration successful" fiif [$ngxStatus-lt 2];then systemctl start Nginx echo "Start nginx Successful" fi
2, automatic deployment, initial configuration, and start three web
#!/bin/shngxstatus= ' ps aux | grep-v grep |grep-c nginx ' If [-e/usr/sbin/nginx];then Echo ' nginx already installed ' exit 110else Yum INS Tall epel-release-y-Q yum install gcc-* glibc-* OpenSSL openssl-devel pcre pcre-devel zlib zlib-devel-y-Q Yum Install nginx-y-Q echo "Install nginx successful" fiif [-f/etc/nginx/nginx.conf];then /bin/cp/etc/nginx/ngin X.conf/etc/nginx/nginx.conf.bak sed-i '/^ location \ {/a\\t\t root/data/www/html;\n\t\t index index.html, '/etc/nginx/nginx.conf mkdir-p/data/www/html echo ' hostname ' >/data/www/html/index.html echo "Configuration successful" fiif [$ngxStatus-lt 2];then systemctl start Nginx echo "Start Ngi NX Successful "fi
3, monitoring script: Monitor the memory utilization of each machine >70%, then output alarm information
#!/bin/shmem_use= ' Free | awk ' Nr==2{print $ mem_total= ' free | awk ' Nr==2{print $ ' mem_per= ' echo ' scale=2, $mem _use/$mem _total "|bc-l |cut-d. -f2 ' If [-e/usr/bin/bc];then Echo ' BC already installed ' else yum install bc-y-Q Echo ' BC nginx successful ' fi#if [$mem _per-gt];thenif (($mem _per > 70)); Then Echo ' Warning free ' is ${mem_per}% ' else echo ' Now ' is ${mem_per}% ' fi
Shell basic Syntax (i)