1、终端打印:
    echo -n 去掉输出时默认的换行符
    echo -e 接受双引号内的转义序列,例:
        [root@localhost tmp]# echo -e "a\t2\t3"
        a   2   3
    终端提示符字体彩色输出:重置=0,黑=30,红=31,绿=32,黄=33,蓝=34,洋红=35,青=36,白=37
        [root@localhost tmp]# echo -e "\e[1;31m Hello World! \e[0m"
    终端提示符字体背景彩色输出:重置=0,黑=40,红=41,绿=42,黄=43,蓝=44,洋红=45,青=46,白=47
        [root@localhost tmp]# echo -e "\e[1;41m Hello World! \e[0m"
2、变量:
    Bash中无论赋值时有没有使用引号,值都会以字符串的形式存储。
    env命令查看所有与此终端进程相关的环境变量。
    cat /proc/$PID/environ 查看该进程的环境变量。
    export命令用来设置环境变量。至此之后,从当前shell脚本执行的任何程序都会继承这个变量!
    获取变量的长度:[root@localhost tmp]# echo ${#PATH}
    截取变量值:
    for ((i=0;i<=${#var};i++))
    do
        echo ${var:i:1}
    done
3、SHELL数学运算(高级操作用到expr和bc两个命令)
    let result=a+b #省略"$"
    result=$[ $a + b ] #方括号中也可以加$
    result=$(( a + b )) #括号内空格可先
    result=$(expr 1 + $b) #加号两边必须有空格
    注:以上4种格式只能进行整数运算,浮点运算可以用bc
    bc浮点运算:
        [root@localhost ~]# echo "scale=2;2*3/2.4" | bc #scale只对除有效
        2.50
        [root@localhost ~]# echo "3 * 0.506" | bc
        1.518
        [root@localhost ~]# echo "scale=2;3 * 0.506" | bc
        1.518
    bc进制转换:
    转二进制 [root@localhost ~]# echo "obase=2;172" | bc
        10101100
        [root@localhost ~]# echo "obase=2;ibase=10;172" | bc
        10101100
        [root@localhost ~]# echo "ibase=10;obase=2;172" | bc
        10101100
    转十进制 [root@localhost ~]# num=10101100
        [root@localhost ~]# echo "obase=10;ibase=2;$num" | bc
        172
        #ERROR
        [root@localhost ~]# echo "ibase=2;obase=10;$num" | bc
        10101100
        [root@localhost ~]#
        [root@localhost ~]# echo "obase=10;ibase=2;10101100" | bc
        172
        #ERROR
        [root@localhost ~]# echo "ibase=2;obase=10;10101100" | bc
        10101100
    转十进制交互界面:
        [root@localhost ~]# bc
        obase=10
        ibase=2
        10000000
        128
        quit
        #ERROR
        [root@localhost ~]# bc
        ibase=2
        obase=10
        10000000
        10000000
        quit
        注:obase和ibase使用的顺序很重要,obase在前!
    求平方及平方根
        10的10平方
        [root@localhost ~]# echo "10^10" | bc
        10000000000
        100的平方根
        [root@localhost ~]# echo "sqrt(100)" | bc
        10
4、数组:linux下支持普通数组,bash4.0后同时支持关联数组
    4.1普通数组:
        定义数组
            单行定义(产生以0为索引的连续位置上):a=(x1 x2 x3)
            数组元素单独定义:
                a[0]="x1"
                a[1]="x2"
                a[2]="x3"
        打印数组元素内容:
            echo ${a[0]}
            echo ${a[$var]}
        打印所有数组元素列表:
            echo ${a[*]}
            echo ${a[@]}
        打印数组元素个数:
            echo ${#a[*]}
            echo ${#a[@]}
    4.2关联数组:
        定义关联数组:
            声明变量为关联数组:
                declare -A as_array #4.0前的版本没有-A选项
            单行定义:
                array=([index1]=val1 [index2]=val2)
            元素单独定义:
                array[index1]="val1"
                array[index2]="val2"
        打印数组元素内容:
            同普通数组
        打印数组元素列表:
            echo ${!array[*]}   
            echo ${!array[@]}
            注:当下标含有空格时,一般用双引号括起来"${!array[@]}"
        打印元素个数:
            同普通数组
5、终端:
    5.1 stty实现用户输入密码不打印到屏幕
        [root@localhost ~]# vim 1.sh
        #!/bin/bash
        echo -e "Enter password:"
        stty -echo
        read password
        stty echo
        echo "Password is: $password"
        [root@localhost ~]# sh 1.sh
        Enter password:
        Password is: xxx
6、计算脚本运行时间
    #!/bin/bash
    start=$(date +%s)
    ……
    ……
    ……
    end=$(date +%s)
    usedtime=$(( end - start))
7、脚本调试
    第一种:在脚本中只调试部分脚本
    #!/bin/bash
    set -x #开启调试
    while true
    do
        : #冒号不做任何操作
    done
    set +x #关闭调试
    第二种:
    sh -x 1.sh
    第三种:
    #!/bin/bash -x
8、函数:
    导出函数
        export -f fname
9、子shell"()"、反引用"``"、变量保留换行符\n例:
    [root@localhost ~]# cat a.txt
    a
    b
    c
    [root@localhost ~]# cat 1.sh
    #!/bin/bash
    pwd
    (cd /etc && pwd)
    pwd
    out=$(cat a.txt)
    echo $out
    echo "######"
    echo "$out"
    [root@localhost ~]# sh 1.sh
    /root
    /etc
    /root
    a b c
    ######
    a
    b
    c
10、read读取输入并给变量赋值
    read -n 2 var       #读取两个字符为var赋值
    read -s var     #不回显用户的输入,一般用于读取密码
    read -p "Password:" var #提示信息
    read -t 10 var      #等待10秒
    read -d ":" var     #定界符读取到指定定界符则读取终止,输入yes:把yes赋值给var
11、产生序列
    seq 1 10
    {1..10}
    {a-z}
    {a-k}
    {A-Z}
    {A-K}
12、cat读取文件、拼接、从标准输入读取、压缩空白行
    echo "abc" | cat - file.txt #从标准输入读取
    cat 1.txt 2.txt 3.txt #拼接
    cat -s 1.txt #把1.txt中连续的多个空行压缩为一个空行
    cat -T 1.txt #将制表符显示为"^I"
    cat -n 1.txt #输出时添加行号,但是不会改变原文件
13、xargs读取标准输入,转换为另一个命令的参数
    [root@localhost ~]# cat a.txt
    axbxc
    dxexf
    [root@localhost ~]# cat a.txt |xargs -d x -n 2
    a b
    c
    d e
    f
         
    xargs的高级应用:好像循环,把每个参数分别执行,而不是一起作为命令的参数
    cat arg.txt
    arg1
    arg2
    arg3
    cat arg.txt | xargs -I {} ./1.sh -p {} -l #注:{}可以多次引用
         
    结合find的使用
    find . -name "*txt" -print0 | xargs -0 rm
    结合stdin 使用子shell的while循环
    cat file.txt | ( while read arg; do cat $arg;done )
    等同于:
        cat file.txt | xargs -I {} cat {}
15、使用md5sum和sha1sum进行校验、核实文件是否改变(网络传输丢包等)
    md5sum filename > filename.md5 #计算md5校验码
    md5sum -c filename.md5 #检查下载的filename.md5是否和filename当前计算的值相符,filename在当前目录。
    md5deep -r选项递归计算目录下所有文件的md5, -l相对路径
    sha1sum用法同md5sum
    sha1sum40位16进制串
    md5sum 32位16进制串
16、提取名称和扩展名
    [root@localhost tmp]# cat 1.sh
    #!/bin/bash
    file="abc.jpg"
    fname=${file%.*} #从右到左最短匹配并删除,%%贪婪匹配
    ename=${file##*.} #从左到右贪婪匹配并删除,比#匹配更正确在文件名有多个.的时候
    echo file is: $file
    echo fname is: $fname
    echo ename is: $ename
    [root@localhost tmp]# sh 1.sh
    file is: abc.jpg
    fname is: abc
    ename is: jpg
17、交互输入工具expect,例:
    [root@localhost ~]# cat 1.sh
    #!/bin/bash
    read -p "Enter number:" no
    read -p "Enter name:" name
    echo number is: $no
    echo name is: $name
    [root@localhost ~]# cat ex.sh
    #!/usr/bin/expect -f
    spawn ./1.sh #spawn为expect的内键命令,封装1.sh进程?
    expect "number:" #如果捕捉到 1.sh返回number:,则执行下一步
    send "1\n"
    expect "name:"
    send "ok\n"
    expect eof #退出
    [root@localhost ~]# chmod 777 ex.sh 1.sh
    [root@localhost ~]# ./ex.sh
    spawn ./1.sh
    Enter number:1
    Enter name:ok
    number is: 1
    name is: ok
18、生成任意大小的文件
    dd if=/dev/zero of=out.data bs=1M count=1
    注:  可用单位:
            字节(1B):     c
            字(2B):      w
            块(512B):        b
            千字节(1024B): k
            兆字节(1024KB):    M
            吉字节 (1024MB):   G
        不指定if,默认会从stdin中读取
        不指定of,将stdout作为默认输出