awk shell

1.整理博客,内容包含awk、变量、运算符、if多分支

  http://www.cnblogs.com/oyoui/p/6603579.html

2.awk文本处理

打印uid在0~40范围内的用户名。
awk -F: '$3>0 && $3<40{print $1,$3}' passwd

打印第5-10行的行号和用户名
awk -F: 'NR>=5 && NR<=10{print NR  $1}' passwd

打印奇数行
awk '(NR%2){print $0}' passwd 

打印偶数行
awk '!(NR%2){print $0}' passwd 

打印字段数大于5的行
awk -F: '(NF>3){print $0}' passwd

打印UID不等于GID的用户名
awk -F: '($3 != $4){print $0}' passwd 

打印没有指定shell的用户
awk -F: '($NF != "/bin/bash"){print $0}' passwd

3.shell脚本编写

自动部署、初始配置、并启动nginx反向代理服务

[root@web2 mnt]# cat install_nginx_proxy.sh 
#!/bin/bash
#insatll nginx proxy

IP=`ifconfig | awk -F" " '/inet/{print $2}' | head -1`
#install yum source
rm -f /etc/yum.repo.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#insyall nginx proxy
yum install nginx -y  
touch /etc/nginx/conf.d/proxy.conf
cat > /etc/nginx/conf.d/proxy.conf <<EOF
    upstream web {
        server 192.168.16.186;
        server 192.168.16.98;
    }
  
    server {
        listen 80;
        server_name $IP;
        location / {
            proxy_pass http://web;
        }
    }
EOF
systemctl start nginx

监控脚本:监控每台机器的内存使用率>70%,则输出报警信息 

[root@web2 mnt]# cat free.sh 
#!/bin/sh
 
mem_use=`free | awk 'NR==2{print $3}'`
mem_total=`free | awk 'NR==2{print $2}'`
mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2`
 
if (( $mem_per > 70 )); then
    echo "Warning free has not enough, now free is ${mem_per}%"
else
    echo "Now free is ${mem_per}%"
fi

  

posted @ 2017-03-23 15:56  golangav  阅读(369)  评论(0编辑  收藏  举报