bash的基础特性五天

bash的基础特性(3)

        1、提供了编程环境
                程序:指令+数据
                程序编程风格:
                        过程式:以指令为中心,数据服务于指令
                        对象式:以数据为中心,指令服务于数据
                shell程序:提供了编程能力,解释执行
                程序的执行方式:
                        计算机:运行二进制指令:
                        编程语言:
                                低级:汇编
                                高级:
                                        编译:高级语言--》编译器--》目标代码
                                                C,C++
                                        解释:高级语言--》解释器--》机器代码
                                                shell,Perl,Python,
                过程式编程:
                        顺序执行:
                        循环执行:
                        选择执行:
                        shell编程:过程式,解释执行
                                编程语言的基本结构
                                        数据存储:变量、数组
                                        表达式
                                        语句
        shell脚本:文本文件
                shebang:
                #!/bin/bash
                #!/usr/bin/phthon
                #!/usr/bin/perl
                shebang:
                magic number:魔数
                例如:
                        #!/bin/bash
                        cat /etc/fstab
                        wc -l /etc/fstab
                运行脚本:
                        1、给与执行权限,通过具体的文件路径指定文件执行;
                        2、直接运行解释器,将脚本作伟解释器程序的参数运行;
变量:
        命名的内存空间:
        数据存储方式:
                字符:110: 24
                数值:110--> 8
                        整型:
                        浮点型:
        变量:变量类型
                作用:
                        1、数据存储格式:
                        2、参与的运算:
                        3、表示的数据范围:
                类型:
                        字符
                        数值:
                                整型:
                                浮点型:
                编程程序语言:
                        强类型
                        弱类型:bash 
                                把所有要存储的数据统统当做字符进行
                                不支持浮点数:
                逻辑运算:
                        true,false
                        1,0
                        与:1 && 1 = 1
                                1 && 0 = 0
                                0 && 1 = 0
                                0 && 0 = 0
                        或:
                                1 || 1 = 1
                                1 || 0 = 1
                                0 || 1 = 1
                                0 || 0 = 0
                        非:
                                ! 1 = 0
                                ! 0 = 1
                        异或:
                短路运算:
                        与:
                                第一个为0,结果必定为0:
                                第一个为1,第二个必须要参与运算:
                        或:
                                第一个为1,结果必定为1:
                                第二个为0,第二个必须要参与运算:
grep:
        Linux上文本处理三剑客
                grep:文本过滤(模式:pattern)工具;
                        grep,egrep,fgrep
                sed:stuream editor,文本编辑工具;
                awk:Linux上实现gawk,文本报告生成器;
        grep:Global search REgular experssion and Print out the line,
                作用:文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查:打印匹配到的行;
                        模式:由正则表达式字符及文本字符所编写的过滤条件;
                        REGEXP由一类特殊字符及文本字符所编写的模式,其中有些字符不表示字符字面意义,而表示控制或通配的功能
                                分两类:
                                        基本正则表达式:BRE
                                        扩展正则表达式:ERE
                                                grep -E, egrep
                                正则比倒是引擎:
                                grep [OPTIONS] PATTERN[FILE……]
                                                选项:
                                                        --color=auto:对匹配到的文本着色显示:
                                                        -v:显示不能够被pattern匹配到的行;
                                                        -i:忽略字符大小写;
                                                        -o:仅显示匹配到的字符串;
                                                        -q:静默模式,不输出任何信息;
                                                        -A #: after,后#行
                                                        -B #:before,前#行
                                                        -C #:context,前后各#行
                                                        -E:使用ERE:
                基本正则表达式元字符:
                        字符匹配:
                                .:匹配任意单个字符;
                                []:匹配指定范围内的任意单个字符;
                                [^]:匹配指定范围外的任意单个字符;
                                [:digit:]、[:lower:]、[:alpha:]、[:upper:]、[:alnum:]、[:punct:]、[:space:]
                        匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数;
                                *:匹配前面的字符任意次:
                                .*:任意长度任意字符:
                                \?:匹配其前面的字符0次或1次,即前面的可有可无:
                                \+:匹配其前面的字符至少1次;
                                \{m\}:匹配前面的字符m次:
                                \{m,n\}:匹配前面的字符至少M次,之多n次;
                                        \{0,n\}:匹配前面的字符至多N次;
                                        \{m,\}:匹配前面的字符至少m次
                        位置锚定:
                                ^:行首锚定:用于模式的最左侧:   ^root
                                $:行尾锚定:用于模式的最右侧:   root$
                                ^PATTERN$:用于模式匹配整行:
                                        ^$:空行:
                                        ^[[:space:]]*$
                                \<或\b:词首锚定:用于单词模式的左侧:
                                \>或\b: 词尾锚定:用于单词模式的右侧;
                                \<PATTERN\>:匹配整个单词:
                        分组:
                                \(\):将一个或多个字符捆绑在一起,当做一个整体进行处理:
                                        \(xy\)*ab
                                Note:分组括号中的模式匹配到的内容会被正则表达式引擎记录与内部的变量中,这些变量的命名方式为:\1, \2. \3, ^
                                        \1:从左侧起,第一个做括号以及与之匹配右括号之间的模式所匹配到的字符;
                                                \(ab\+\(xy\)*\):
                                                        \1: ab\+\(xy\)*
                                                        \2: xy
                                后向引用:引用前面的分组括号中的模式所匹配字符,(而非模式本身)
                                
                                
                                
                                

bash脚本编程:

        if语句、bash -n 、 bash -x
                COMDITION:
                        bash命令:
                                用命令的执行状态结果;
                                        成功:true
                                        失败:flase
                                成功或失败的意义,取决于用到的命令;
                单分支
                        if CONDITION;THEN
                                if-true
                        fi
                双分支:
                        if condition;then
                                if-true
                        else
                                if0false
                        fi
                多分支:
                        if CONDITION1;THEN
                                if-true
                        elif CONDITION2;then
                                if-true
                        elif CONDITION3;then
                                if-true
                        ……
                        else
                                all-false
                        fi
                逐条件进行判断;第一次遇到为“真”条件时,执行其分支,而后结束;
                示例:用户键入
                                 #!/bin/bash
                                 read -p "Enter a file path:" filename
                                 if [ -z "$filename" ]; then
                                         echo "Usage:Enter a file path."
                                         exit 2
                                 fi
                                 if [ ! -e "$filename" ]; then
                                         echo "No such file."
                                         exit 3
                                 fi
                                 if [ -f $filename ]; then
                                         echo "A common file."
                                 elif [ -d $filename ]; then 
                                         echo "A directory file."
                                 elif [ -L $filename ]; then
                                         echo "A symbolic file."
                                 else
                                         echo "Other type"
                                 fi
                                 注意:if语句可嵌套
        循环:for,while,until
                循环体:要执行的代码,可能要执行N遍;
                        进入条件:
                        退出条件;
                for循环:
                        for 变量名 in 列表; do
                                循环体
                        done
                        执行机制:
                                依次将列表中的原酸赋值给你“变量名”;每次赋值后即执行依次循环体;直到列表中的元素用尽。循环结束。
                        示例:添加10个用户,user1-user10;密码同用户名;
                        #!/bin/bash
                        #
                        if [ ! $UID -eq 0 ]; then
                            echo "Only root."
                            exit 1
                        fi
                        for i in {1...10}: do
                            if id user$i &> /dev/null; then
                                echo "user$i exists."
                            else
                                useradd user$i
                                if [ $? -eq 0 ]; then
                                  echo "user$i" | passwd --stdin user$1 &> /dev/null
                                    echo "Add user$i finished."
                                 fi
                             fi
                        done
                列表生成方式:
                        (1)直接给出列表
                        (2)整数列表;
                                        (a){start...end}
                                        (b)$(seq [start [step]] end)
                        (3) 返回列表的命令;
                                $(COMMAND)
                示例:判断某路径下所有文件的类型
                                #!/bin/bash
                                #
                                for file in $(ls /var): do
                                        if [ -f /var/$file ]; then
                                                echo "Command file."
                                        elif [ -d /var/$file ];then
                                                echo "Directory."
                                        elif [ -L /VAR/$file ]; then
                                                echo "Symbolic file."
                                        else
                                                echo "Other type"
                                        fi
                                done
                                示例:
                                        #!/bin/bash
                                        #
                                        declare -i estab=0
                                        declare -i lissten=0
                                        declare -i other=0
                                        for state in $( netstat -tan | grep "^tcp\>" | awk '{print $NF}');do
                                                if [ "$state" == 'ESTABLISHED' ]; then
                                                let estab++
                                                elif [ "$state" == 'listen' ]; then
                                                let listen++
                                                else
                                                let other++
                                                fi
                                        do
                                        echo "ESTABLISHED: $estab"
                                        echo "LISTEN: $listen"
                                        echo "Unkown: $other"
立即登录, 发表评论.
没有帐号? 立即注册