Linux 下Shell的学习2


0. 查看帮助(比如内置功能)
    man bash    -->变量处理大全

1.-计算变量长度的不同方法及不同方法的耗时对比
    尽可能的用内置的命令处理,速度快
        time for i in $(seq 1111);do count=${#char};done;
2.场景的变量数值计算
    0.$(())  let  expr  bc  $[]
    1.(())的使用:只需要将特定的算术运算表达式用$(())括起  -->效率高
        echo $((1+2*2*2*2))  -->17
            ==> ((a=1+2*2*2*2)) -->echo $a
            ==>b=$((1+2*2*2*2)) -->echo $b
        $((6 > 4 )) && echo "YES"
        注意:+ - * / % += -=
              **为幂运算
              a++:显输出,后自加
              ++a: 先自加,后输出
              计算器: echo $(($1$2$3))
    2.let命令的使用
        let 赋值表达式
            let a=9-8
            let a=a+1
    3.expr:一般用于整数,也可以验证扩展名
            1.常规计算
                expr 2 + 2           -->直接输出结果,但是注意运算符左右有空格
                expr 2 * 2          -->直接输出结果,特殊符号需要转义

            2.增量计数
                 i=0
                 i=`expr $i+1`
                 echo $i
                expr $[$a+$b], $a,$b可以为整数值
                    expr $[2+3]          -->不用添加空格,直接输出结果
                    expr $[2**3]         -->不用转义,直接输出结果

            3.判断扩展名 expr String : REGEXP
                if expr "hello.txt" : ".*.txt"  -->判断文件拓展名是否为.txt  注意空格
                    ->为真,则输出   9   显示匹配后的字符数,包括.txt
                    ->为假,则输出非 0
            4.判断变量是否是整数
                read -p "please input the num:" a
                expr $a + 0 &>/dev/null && echo int || echo char
            5.计算字符串长度
                echo $(expr length "Hello World") --> 11 调用length函数
                    ==> echo "Hello World"|wc -m  --> 12 多计算了一个空格
                    ==> char="Hello World";echo ${#char} --> 11

    4.bc Unix下的计算器
        1.用于计算
            a=54
            echo $a+5|bc   -->输出59
            echo 10.6+6|bc -->输出16.6    不用管空格限制
            seq -s "+" 10|bc  -->输出55
        2.字符转换
            echo "scale="
    5.$[] 的使用
        echo $[2+3]
            ==>echo $[ 3 + 2 ]
            ==>echo $((2+3))

3.shell变量的输入
    1.read
        read [参数] [变量名] 
            p;prompt: 设置提醒 
            t: time  : 超时时间
        read -t 5 -p "Please input 2 number:" a  b
            ==>echo -n "please input a num:"
               read a b
    2.  脚本里面直接定义,外部传入
            a=$1
            b=$2

4.条件测试
    1.文件测试
        test<条件表达式>
            ==> [条件表达式]
        [[条件表达式]]  -->扩展的1,
                        -->可以使用通配符进行模式匹配。&&,||,<,>可以应用双中括号[[]]中
                        -->但是单中括号[]有问题
            注意:[[ -f $file && -f $f2 ]]
                    ==> [ -f $file ] && [ -f $f ]         -->推荐
        test -f file && echo "this is file"||echo "this is not a file"
            ==>[ -f file ]&& echo "this is file"||echo "this is not a file"
            ==>[[ -f file && -d folder ]] && echo "this is file"||echo "this is not a file"
        test ! -f file && echo "this is file"||echo "this is not a file"
            ==>[ ! -f file ]&& echo "this is file"||echo "this is not a file"
            ==>[[ ! -f file && ! -d folder ]] && echo "this is file"||echo "this is not a file"


    2.文件测试操作符    -->更多可以man test学习   
        -d  文件存在,且为目录文件          dounent
        -f  文件存在,且为普通文件          file
        -s: 文件存在,且大小非0,内容非空   size
        -e: 文件存在则为真                  exist
        -r: 文件可读                        read
        -w: 文件可写                        write
        -x: 文件可执行                        execute
        -L: 连接文件                        link
        f1 -nt f2 :文件1比文件2新则为真        new than
        f1 -ot f2 :文件1比文件2旧则为真        old than

    3.字符串测试操作符:比较2个字符串是否相同,是否为0,
         =:是否相同,对于$a这种最好用"{$a}"
        -z: 字符串长度为0,则为真          
        -n: 字符串长度不为0,则真          -->表示存在
        "s1"=="s2": 相等为真
        "s1"!="s2": 不相等为真

    4.整数比较测试符号
        -eq: 相等
        -ne: 不等
        -gt: 大于  great
        -ge: 大于等于
        -lt: 小于  lower
        -le: 小于等于

    5.特别提醒:
        在中括号[]中使用 >,<时,需要转义,因为重定向也用这个
        等号==和不等号!=在[]中使用,不需要转义
        变量shell最好添加上双引号
        字符串比较一定要加上双引号 [ -n "$file" ]
        "-a"和"-o" 逻辑操作符中括号[]使用
        "&&"和"||" 逻辑操作符双中括号[[]]使用
        中括号2端必须有空格
        如果有多个命令,可以使用{ command1; command2} 注意封号
            [ $ERROR -eq 0 ] && echo "YES" || {echo "NO" ; exit 1}
        自己设定的环境变量一定要在脚本里重新体现以下
            export hhh_IP=$1   -->输出环境变量
        &:后台执行
            ==>bg:   当前脚本后台执行
            ==>nohup 程序后台运行
                -->  nohup ./while.sh &
        jobs:查看执行的脚本或任务
        fg:当前脚本或任务拿到前台执行,多任务可以用+任务编号执行,例如,fg 1
        ctrl + z :暂停执行当前脚本
        ctrl + c :停止执行当前脚本

    6.逻辑操作符:
        &&:   ==> -a
        ||:   ==> -o
        !:取反

5.if条件语句
    1.格式:
        第一个:
            if [  ]; then
            fi
        第二个:
            if [  ]
                then
                    命令
                else
                    命令               
            fi
            ==> if [ -f "$file" ];then echo 1;else echo 0;fi
            ==> if [ -f "$file" ] && echo 1 ||echo 0;fi
        第三个:只有一个else哦
            if [  ]
            then
                命令
             elif [  ]
                 then
                     指令
            else
            fi
    2.   

6.判断字符串是否为数字的多种方法
    1.sed + 正则表达式
        [ -n "`echo $num|sed 's/[0-9]//g'`" -a -n "`echo $2|sed 's/[0-9]//g'`" ] && echo "Both are not number" && exit 1
            ==>[ -n "`echo $num|sed 's/[0-9]//g'`" -a -n "`echo $2|sed 's/[0-9]//g'`" ] && { echo "both are not num ; exit 1"}
            ==>[ -n "`echo $1|sed 's/[0-9]//g'`" ] && [ -n "`echo $2|sed 's/[0-9]//g'`" ]
            ==>替换数字为空,-n表示存在且不为空
    2.变量的子串替换+正则表达式
        num=hello
        [ -z "`echo "${num//[0-9]}"`" ]
    3.expr
        expr $a + 0 &>/dev/null && echo int || echo char

7.Mysql数据库是否正常的判断
    1.Mysql端口号监控
    2.Mysql进程监控
    3.通过Mysql命令和账户连接Mysql,根据返回状态判断 
        -->注意远程连接 mysql -uroot -proot -h localhost -e "select version()"
        -->必须有mysql客户端,要有账户,密码,主机的授权
    4.根据php/java程序判断             -->推荐,报警的依据是网站数据库是否访问正常,而不是服务是否开启
    5.综合运用以上4个
    6.多考虑,多用变量,规范化

8.Case条件语句
    1.结构语法
        case "字符变量" in
            值1):
                指令;;
            值2):
                指令;;
            ...
            *):
                指令;;
        esac

    2.

9.while条件句
    1.while读取文件的方式
        1.while read line
            do
                echo $line
            done</var/log/messages      -->利用while去读内容
        2.cat $FILE |while read line
            ...

10.until条件句  -->了解
    until 条件
        do
            指令
        done

11.for循环结构
    格式1;
        for i in $@
    格式2:c语言
        for ((i=0;i<1;i++))
    例子: for i in 5 4 3 2 1
           for i in {5..1}

12.break,continue,exit
        break: 退出当前xunh
        continue:退出当前判断,执行后续的循环
        exit: 结束shell程序
        return: 退出函数

13.shell函数的语法
    1.格式
        function 方法名(){                  方法名(){

            }                                        }
    2.调用
        直接执行函数名 
        函数名 参数1 参数2   -->check_url  www.baidu.com
            -->命令行给脚本传参数
                hhh(){
                    echo "$1"
                }
                hhh $1
    3.函数体和执行脚本的分离
        1.创建函数库脚本  /etc/init.d/hhh_function
        2.添加执行权限    chmod +x /etc/init.d/hhh_function
        3.查看执行权限    ll /etc/init.d/hhh_function
        4.开发执行脚本调用函数  . /etc/init.d/hhh_function  (也可以使用source调用)

14.数组:相同数据类型的元素按照一定的顺序排列的结合
    0.man bash 搜索Arrays
    1.数组的定义:
        array=( a b c )
        array=([1]=2015 [2]=2016 [3]=2017)    
            --> echo ${arr[1]} 输出2015
        array[0]=2015 array[1]=2016 array[2]=2017

        重点:文件的输出作为数组:
                array=($(ls *.sh))
                     ==> array=(`ls *.sh`)

    2.数组的长度
        echo ${#array[*]}
        echo ${#array[@]}
    3.数组的添加/取消/替换
        unset array
        unset array[0]  -->删除第一个数组元素
        array[3]=d
    4.数组的全部输出
        echo ${array[@]}
        echo ${array[*]}
    5.数组内容的截取和替换
        echo ${array[*]:2:1}    -->输出b, 从数组下标2开始取,取出来1个数字     
        echo ${array[*]/d/e}    -->匹配到d替换为e,实际中不生效,显示生效
            array1=${array[*]/d/e}
            echo ${array[*]}
    6.数组的替换
        echo ${array[@]/g/h}

15.SHELL脚本的调试技巧
    1.使用dos2unix命令处理脚本
        检查脚本没有问题,但是执行就是报错,使用dos2unix格式化
    2.使用echo命令调试
        例如,在变量读取和修改之前加入echo命令

原文地址:https://www.cnblogs.com/ftl1012/p/9310505.html