shell基本语法

Posted luchuangao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell基本语法相关的知识,希望对你有一定的参考价值。

作业一:整理博客,内容包含awk、变量、运算符、if多分支

 

作业二:awk文本处理

1、打印uid在30~40范围内的用户名

[[email protected] ~]# awk -F: ‘$3>=30 && $3<=40{print $1}‘ /etc/passwd
rpc
ntp

2、打印第5-10行的行号和用户名

[[email protected] ~]# awk -F: ‘NR>=5 && NR<=10{print NR,"-------",$1}‘ /etc/passwd
5 ------- lp
6 ------- sync
7 ------- shutdown
8 ------- halt
9 ------- mail
10 ------- operator

3、打印奇数行

[[email protected] ~]# awk ‘(NR%2){print NR,"----",$0}‘ test 
1 ---- root:x:0:0:root:/root:/bin/bash
3 ---- nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin
5 ---- abominate:x:1010:1011::/home/abominate:/bin/bash
7 ---- atomize:x:1012:1013::/home/atomize:/bin/bash
9 ---- Wpq2222b:x:1014:1015::/home/Wpq2222b:/bin/bash
11 ---- egon666:x:1016:1017::/home/egon666:/bin/bash
[[email protected] ~]# 

4、打印偶数行

[[email protected] ~]# awk ‘!(NR%2){print NR,"----",$0}‘ test
2 ---- bin:x:1:1:bin:/bin:/sbin/nologin
4 ---- abominable:x:1009:1010::/home/abominable:/bin/bash
6 ---- anomie:x:1011:1012::/home/anomie:/bin/bash
8 ---- Alex213sb:x:1013:1014::/home/Alex213sb:/bin/bash
10 ---- yH438PIG:x:1015:1016::/home/yH438PIG:/bin/bash
12 ---- egon:x:1017:1002::/home/egon:/bin/bash

5、打印字段数大于5的行

[[email protected] ~]# awk -F: ‘NF>5{print $1,"-----",NF}‘ test
root ----- 7
bin ----- 7
nginx ----- 7
abominable ----- 7
abominate ----- 7
anomie ----- 7
atomize ----- 7
Alex213sb ----- 7
Wpq2222b ----- 7
yH438PIG ----- 7
egon666 ----- 7
egon ----- 7

6、打印UID不等于GID的用户名

[[email protected] ~]# awk -F: ‘$3!=$4{print $3,"----",$4}‘ test    
991 ---- 547
1009 ---- 1010
1010 ---- 1011
1011 ---- 1012
1012 ---- 1013
1013 ---- 1014
1014 ---- 1015
1015 ---- 1016
1016 ---- 1017
1017 ---- 1002

7、打印没有指定shell的用户

[[email protected] ~]# awk -F: ‘$NF!~/bash$/{print $0}‘ test
bin:x:1:1:bin:/bin:/sbin/nologin
nginx:x:991:547:Nginx web server:/var/lib/nginx:/sbin/nologin

 

作业三:shell脚本编写

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

#!/bin/sh

ngxStatus=`ps aux | grep -v grep |grep -c nginx`

if [ -e /usr/sbin/nginx ];then
    echo "nginx already installed"
    exit 110
else
    yum install 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"
fi
if [ -f /etc/nginx/nginx.conf ];then
    /bin/cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
    sed -i ‘/^http/a\\t upstream luchuangao { \n\t server web01;\n\t server web02;\n\t server web03;\n\t }‘ /etc/nginx/nginx.conf
    sed -i ‘/^        location \/ {/a\\t\t proxy_pass http:\/\/luchuangao;‘ /etc/nginx/nginx.conf
    echo "Configuration successful"
fi
if [ $ngxStatus -lt 2 ];then
    systemctl start nginx
    echo "Start nginx successful"
fi

2、自动部署、初始配置、并启动三台web

#!/bin/sh

ngxStatus=`ps aux | grep -v grep |grep -c nginx`

if [ -e /usr/sbin/nginx ];then
    echo "nginx already installed"
    exit 110
else
    yum install 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"
fi
if [ -f /etc/nginx/nginx.conf ];then
    /bin/cp /etc/nginx/nginx.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"     
fi
if [ $ngxStatus -lt 2 ];then
    systemctl start nginx
    echo "Start nginx successful"
fi

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

#!/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 [ -e /usr/bin/bc ];then
    echo "bc already installed"
else
    yum install bc -y -q
    echo "bc nginx successful"
fi

#if [ $mem_per -gt 70 ];then
if (( $mem_per > 70 )); then
    echo "Warning free has not enough, now free is ${mem_per}%"
else
    echo "Now free is ${mem_per}%"
fi

以上是关于shell基本语法的主要内容,如果未能解决你的问题,请参考以下文章

shell基本语法

shell语法 函数

shell编程:基本语法

linux之shell编程基本语法

Shell脚本学习——基本语法阶段一

shell编程基本语法