zoukankan      html  css  js  c++  java
  • bash编程笔记

    1、脚本该如何些
     hello.sh
     命名,执行权限,如何执行
    2、在写bash脚本时,如果传参数
    [root@mail bash]# cat  hello.sh
    #!/bin/bash
    # first bash shell script
    echo "Hello World!"
    echo $$
    echo "\$0 是什么???是指脚本名称" $0
    echo "\$1 这是脚本的第1个位置参数:"$1
    echo "\$2 这是脚本的第2个位置参数:"$2
    echo "\$3 这是脚本的第3个位置参数:"$3
    echo "\$4 这是脚本的第4个位置参数:"$4
    echo "\$5 这是脚本的第5个位置参数:"$5
    echo "\$6 这是脚本的第6个位置参数:"$6
    echo "\$# 是指当前脚本的位置参数个数"  $#

    3、学习条件判断语句if
    if 条件
    then
        条件成立要执行的命令
    else
        条件不成立要执行的命令
    fi
    *******if关键字后面跟的是“条件“  , “条件“ 实际是一个命令,if会判断命令的返回值,返回为0,则条件成立,返回值为非零,则条件不成立


    4、脚本执行的返回值,由脚本执行过程中,最后一条执行的命令的返回值来决定

    [root@mail bash]# cat  checkuser.sh
    #!/bin/bash
    # test user
    if id $1 &> /dev/null
    then
            echo "user exist"
    else
            echo "no this user"
    fi

    5、脚本异常处理功能
    [root@mail bash]# cat checkuser.sh
    #!/bin/bash
    # test user

    if test $# -ne 1
    then
            echo "ERROR!"
            echo "Syntax: $0 <username>"
            exit 3
    fi
    if id $1 &> /dev/null
    then
            echo "user exist"
    else
            echo "no this user"
            exit 2
    fi

    6、总结if语句语法
    if condition
    then
        command
    fi
    --------
    if condition
    then
        command
    else
        command
    fi
    -----------
    if condition1
    then
        command
    elif condition2
    then
        command
    elif conndition3
    then
        command
    else
        command
    fi
    ------------------

    7、if语句的练习
    1)
    [root@mail bash]# cat checkfiletype.sh
    #!/bin/bash

    if ! [ -e $1 ]
    then
            echo $1 is not exist
    elif [ -d $1 ]
    then
            echo "$1 is directory"
    elif [ -L $1 ]
    then
            echo "$1 is a symbolic link"
    elif [ -b $1 ]
    then
            echo "$1 is block special"
    elif test -c $1
    then
            echo "$1 is character special"
    elif test -p $1
    then
            echo "$1 is a named pipe"
    elif test -S $1
    then
            echo "$1 is socket file"
    elif test -f $1
    then
            echo "$1 is regular file"
    else
            echo "unknown"
    fi
    ----
    2)
    [root@mail bash]# cat authuser.sh
    #!/bin/bash


    if test $# -ne 2
    then
            echo "Syntax: $0 <username> <password>"
            exit 2
    fi

    #if [ "$1" = "root" ] && [ "$2" = "123" ]
    if [ "$1" = "root" -a "$2" = "123" ]
    then
            echo "root user auth successed"
    elif [ "$1" = "pg" ] && [ "$2" = "123" ]
    then
            echo "pg user auth successed"
    else
            echo "unknow user"
    fi

    3)
    [root@mail bash]# cat testping.sh
    #!/bin/bash

    if ping -c 1 -W 1 $1 &> /dev/null
    then
            echo "link ok"
    else
            echo "no link"
    fi



    8、case语句,专用于对字符串做判断
    case  变量名  in
            value1)
                command1
            ;;
            value2)
                command2
            ;;
            value3)
                command3
            ;;
            *)
                command4
            ;;
    esac



    [root@mail bash]# cat  case.sh
    #!/bin/bash

    case "$1" in
            root|pg)
                    echo "$1 ok"
            ;;
            *)
                    echo "fail"
            ;;
    esac

    #case $1 in
    #       root)
    #               echo "$1 ok"
    #       ;;
    #       pg)
    #               echo "$1 ok"
    #       ;;
    #       *)
    #               echo "fail"
    #       ;;
    #esac


    [root@mail bash]# cat   authuser_case.sh
    #!/bin/bash

    case "$1:$2" in
            root:123)
                    echo "$1 ok"
            ;;
            pg:123)
                    echo "$1 ok"
            ;;
            *)
                    echo "unknow"
            ;;
    esac

    ++++++++++++++++++++++++++++++++++++++++++
    循环


    1)while语句语法:
    n=1
    while 条件       #while后所写的条件是命令
    do
        条件成立,执行的操作
        ((n++))
    done
    [root@mail bash]# cat   while.sh
    #!/bin/bash

    count=1
    while test $count -le 100
    do
            echo $count
            count=$[$count+1]
            #((count++))
    done
    # 变量初始值(水缸为空),条件(水缸没满), 变量自增(挑一桶水,做一个记号,如自增1)

    [root@mail bash]# cat ./while2.sh
    #!/bin/bash

    while :
    do
            echo "haha"
    done



    2)for语句语法:

    [root@mail bash]# cat ./for1.sh
    #!/bin/bash
    for((n=1;n<=100;n++))
    do
            echo $n
    done


    [root@mail bash]# cat ./for2.sh
    #!/bin/bash

    for n in 1 2 3 4 5 6 7 8 9 10
    do
            echo $n
    done


    for 变量名 in value1 value2 value3 value4
    do
        echo $变量名
    done

     
    [root@mail bash]# cat  for3.sh
    #!/bin/bash

    for file in /*
    do
            echo $file
    done


    [root@mail bash]# cat  pingfor.sh
    #!/bin/bash

    for((i=1;i<=254;i++))
    do
            (if ping -c 1 -W 1 192.168.1.$i &> /dev/null
            then
                    echo 192.168.1.$i
            fi) &
            trap 'echo "收到!";exit 0' 2
    done


    [root@mail bash]# cat   pingfor2.sh
    #!/bin/bash

    for i in `seq 1 254`
    do
            if ping -W 1 -c 1 192.168.1.$i
            then
                    echo 192.168.1.$i
            fi
    done

    #for i in {1..254}
    #do
    #       if ping -W 1 -c 1 192.168.1.$i
    #       then
    #               echo 192.168.1.$i
    #       fi
    #done






    [root@mail bash]# cat ./while3.sh
    #!/bin/bash

    while :
    do
            echo 1
            sleep 3
    done
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    定义一个名为array1的数组
    方法1:
    元素分别为aa bb cc dd 共4个元素
    [root@mail bash]# array1=(aa bb cc dd)
    [root@mail bash]# echo ${array1[0]} 通过下标0,来访问第一个数组元素
    aa
    [root@mail bash]# echo ${array1[1]} 通过下标1,来访问第二个数组元素
    bb
    总结:数组的标是从0到数组元素个数减1
    [root@mail bash]# echo ${array1[2]}
    cc
    [root@mail bash]# echo ${array1[4]}

    [root@mail bash]# echo ${array1[3]}
    dd
    [root@mail bash]# echo ${array1[*]} 显示所有数组元素
    aa bb cc dd
    [root@mail bash]# echo ${#array1[*]} 显示数组元素个数
    4



    echo $[${#array1[*]}-1]
    数组array1的最大下标值。

    方法2:
    先声明数组,再定义其每个元素的值
    [root@mail bash]# declare -a array2 声明一个数组
    [root@mail bash]# array2[0]=aaa  定义第一个数组元素的值
    [root@mail bash]# array2[1]=bbb  。。。。
    [root@mail bash]# array2[2]=ccc  。。。。
    [root@mail bash]# array2[3]=ddd  。。。。
    [root@mail bash]# echo ${array2[*]}
    aaa bbb ccc ddd
    [root@mail bash]# echo ${array2[0]}
    aaa
    [root@mail bash]# echo ${array2[1]}
    bbb
    [root@mail bash]# echo ${array2[2]}
    ccc
    [root@mail bash]# echo ${array2[3]}
    ddd
    [root@mail bash]#


    例子:

    [root@mail bash]# cat  100array.sh
    #!/bin/bash


    #array100=(`for((n=1;n<=100;n++));do echo $RANDOM;done`)
    declare -a array1000
    for((n=0;n<=99;n++))
    do
            array1000[$n]=$RANDOM
    done
    echo ${array1000[*]}
    echo ${#array1000[*]}
    [root@mail bash]#

    ================脚本位置参数============
    [root@mail bash作业2答案]# set -- "1a 2a" 3a 4a 5a
    [root@mail bash作业2答案]# echo $1
    1a 2a
    [root@mail bash作业2答案]# echo $2
    3a
    [root@mail bash作业2答案]# echo $3
    4a
    [root@mail bash作业2答案]# echo $4
    5a
    [root@mail bash作业2答案]# echo $#
    4
    [root@mail bash作业2答案]# echo $*
    1a 2a 3a 4a 5a
    [root@mail bash作业2答案]# echo $@
    1a 2a 3a 4a 5a
    [root@mail bash作业2答案]# for i in "$@";do echo $i ;done
    1a 2a
    3a
    4a
    5a
    [root@mail bash作业2答案]# for i in "$*";do echo $i ;done
    1a 2a 3a 4a 5a

    写一个脚本,作用是将脚本的所有位置参数(输入的位置参数均是整数)相加,并打印出相加的结果。
    方法1:
    ]# cat  10.sh
    #!/bin/bash

    count=0
    #for i
    for i in "$@"
    do
            count=$[$count+$i]
    done
    echo $count
    [root@mail bash作业2答案]# ./10.sh  1 2 3 4 5 6
    21

    方法2:
    ]# cat 10.sh
    count=0
    m=$#
    for((n=1;n<=m;n++))
    do
            count=$[$count+$1]
    #       echo $1
            shift 1

    done
    echo $count

    =================函数==============
    定义函数的目的:将有特定功能的代码块,定义一个函数名称,在要使用此代码块时,直接应用函数名,而不用再些一边此代码块

    1、如下定义函数的方法
    函数名() {
        函数体(代码块)
    }

    [root@mail bash作业2答案]# cat  fun1.sh
    #!/bin/bash

    myls() {
            ls
    }
    [root@mail bash作业2答案]# cat  10.sh
    #!/bin/bash

    total(){
            count=0
            m=$#
            for((n=1;n<=m;n++))
            do
                    count=$[$count+$1]
                    shift 1

            done
            echo $count
    }
    2、使用函数:
    方法一:
    在脚本中直接定义函数,在定义过后,直接写函数名调用函数


    ]# cat  10.sh
    #!/bin/bash

    total(){   此处为函数定义
            count=0
            m=$#
            for((n=1;n<=m;n++))
            do
                    count=$[$count+$1]
                    shift 1

            done
            echo $count
    }
    total $1 $2 $3 此处为通过函数名直接使用函数

    再比如:
     /etc/init.d/httpd  脚本中定义了start() {command1;command2;}
    脚本后面 case "$1" in
            start)
                start 此处则是使用函数名调用函数
            ;;
            ...
            ...
        esac

    方法2:
    在一个文件中专门定义好函数,在另一个脚本中source  /函数文件,然后直接把函数名当命令使用。
    比如:
    vim /etc/init.d/httpd
    # Source function library.
    . /etc/rc.d/init.d/functions 此文件定义了大多数重要的系统函数,系统函数名一般是以双下划线开头命名

    在脚本后面,直接使用了/etc/rc.d/init.d/functions中定义好的status函数

    取消函数:
    unset -f 函数明
    ]# unset -f total
    取消变量:
    unset 变量名

    执行顺序:别名-特殊内建命令-函数-内建命令-外部命令

    ======bash的变量操作==========
    [root@mail test]# file=1a.sh 定义一个普通变量
    [root@mail test]# echo ${file} 打印一个普通变量的值
    1a.sh
    [root@mail test]# echo ${#file} 打印一个变量的值的字符个数
    5
    [root@mail test]# echo ${file:2} 略去变量的前2个字符
    .sh
    [root@mail test]# echo ${file: -3} 取出变量的后3个字符
    .sh
    [root@mail test]# file=/dir1/dir2/dir3/file.sh 定义一个普通变量
    [root@mail test]# echo ${file:6:4} 略去前6个,往右数4个取出
    dir2


    [root@mail test]# echo ${file}
    /dir1/dir2/dir3/file.sh
    [root@mail test]# echo ${file: -10} 取出后10个字符
    r3/file.sh
    [root@mail test]# echo ${file: -10: 3} 取出后10个字符,往右数并取出3个字符
    r3/

    [root@mail test]# echo ${file: -3} 取出后3个字符
    .sh
    [root@mail test]# echo ${file:0:${#file}-3}  
    /dir1/dir2/dir3/file

    ------------例子-------------------
    [root@mail test]# ls
    1a.sh  1e.sh  2d.sh  3c.sh  4b.sh  5a.sh  5e.sh
    1b.sh  2a.sh  2e.sh  3d.sh  4c.sh  5b.sh
    1c.sh  2b.sh  3a.sh  3e.sh  4d.sh  5c.sh
    1d.sh  2c.sh  3b.sh  4a.sh  4e.sh  5d.sh
    [root@mail test]# for file in ./*;do mv $file ${file:0:${#file}-3};done
    [root@mail test]# ls
    1a  1c  1e  2b  2d  3a  3c  3e  4b  4d  5a  5c  5e
    1b  1d  2a  2c  2e  3b  3d  4a  4c  4e  5b  5d


    ---------------------
    [root@mail test]# echo ${file}
    /dir1/dir2/dir3/file.sh
    [root@mail test]# echo ${file%dir*} 从右往左数到第一个dir字符串,把dir及其右边的字符都删除
    /dir1/dir2/
    [root@mail test]# echo ${file%/*} 从右往左数到第一个/字符串,把/及其右边的字符都删除
    /dir1/dir2/dir3
    [root@mail test]# echo ${file}
    /dir1/dir2/dir3/file.sh
    [root@mail test]# echo ${file%%i*}从右往左数到最后一个i字符串,把i及其右边的字符都删除
    /d

    [root@mail test]# echo ${file}
    /dir1/dir2/dir3/file.sh
    [root@mail test]# echo ${file#*i} 从左往右数,数到第一个i字符,把i及其左边的字符都删除
    r1/dir2/dir3/file.sh

    [root@mail test]# echo ${file##*/}从左往右数,数到最后一个/字符,把/及其左边的字符都删除
    file.sh

    [root@mail test]# basename /dir1/dir2/dir3/file.sh
    file.sh
    ----------------------
    [root@mail test]# ls
    1a.txt  1e.docs  2d.sh  3c.sh  4b.sh  5a.sh  5e.sh
    1b.sh   2a.sh    2e.sh  3d.sh  4c.sh  5b.sh
    1c.sh   2b.sh    3a.sh  3e.sh  4d.sh  5c.sh
    1d.sh   2c.sh    3b.sh  4a.sh  4e.sh  5d.sh

    [root@mail test]# for i in ./*;do mv $i ${i%.*};done
    [root@mail test]# ls
    1a  1c  1e  2b  2d  3a  3c  3e  4b  4d  5a  5c  5e
    1b  1d  2a  2c  2e  3b  3d  4a  4c  4e  5b  5d


    ************************
    [root@mail test]# echo ${file} 定义一个变量file
    /dir1/dir2/dir3/file.sh
    [root@mail test]# echo ${file:-abc}  :-的意思是变量file如果被定义,则还是原始变量的值,如果变量没有被定义,则临时将字符串abc赋值给变量file
    /dir1/dir2/dir3/file.sh
    [root@mail test]# unset file 将变量file取消
    [root@mail test]# echo ${file:-abc}
    abc
    [root@mail test]# echo ${file}

    [root@mail test]#

    :- 在一般情况下可以等价写为-
    echo ${file:-abc} 等价写为 echo ${file-abc}
    但是当 file="" 赋值为空时,会有如下结果
    [root@mail test]# file=""
    [root@mail test]# echo ${file-"abc"}  -认为变量被定义了

    [root@mail test]# echo ${file:-"abc"} :-认为变量没有被定义,于是临时赋值为新的字符串
    abc



    [root@mail test]# file=/dir1/dir2/dir3/file.txt
    [root@mail test]# echo ${file:+abc} 表示如果file被定义过,则重新临时赋值为abc,如果没有被定义过,则不重新定义
    abc
    [root@mail test]# echo ${file}
    /dir1/dir2/dir3/file.txt

    [root@mail test]# echo ${file2} 如:file2变量没有被定义过,则不重新定义

    [root@mail test]# echo ${file2:+abc}

    [root@mail test]#

    :+ 在一般情况下可以等价写为+
    echo ${file:+abc} 等价写为 echo ${file+abc}
    但是当 file="" 赋值为空时会不一样




    [root@mail test]# echo ${file4}

    [root@mail test]# echo ${file4:=abc} :=表示如果变量没有被定义,则永久赋值为新的字符串abc,如果变量已经被定义,则操持原始值
    abc
    [root@mail test]# echo ${file4}
    abc
    [root@mail test]#


    [root@mail test]# echo ${file7:?is not set} 表示如果file7没有被定义,则提示:?后面的字符串。
    bash: file7: is not set
    [root@mail test]# file7="123" 如果file7已经被定义,则不做提示
    [root@mail test]# echo ${file7:?is not set}
    123



    [root@mail pg]# echo -en "\\033[0;39m"
    [root@mail pg]# echo -en "\\033[0;33m"
    [root@mail pg]# echo -en "\\033[0;35m"
    [root@mail pg]# echo -en "\\033[0;36m"
    [root@mail pg]# echo -en "\\033[0;37m"
    [root@mail pg]# echo -en "\\033[0;38m"
    [root@mail pg]# echo -en "\\033[0;39m"
    [root@mail pg]# echo -en "\\033[0;40m"
  • 相关阅读:
    面试题来积累自己
    基于php基础语言编写的小程序之计算器
    配置apache的虚拟机+软件下载
    执行表空间使用率SQL突然变慢问题分析
    DM-建库选项-字符串比较大小写敏感-测试
    OGG再次遇到虚拟列无法处理,导致进程abend二
    OGG应用进程abend报错无法insert虚拟列
    Oracle Asm Failgroup测试学习
    OGG复制进程报错,存在update set 主键列 is null
    测试:OGG初始化同步表,源端抽取进程scn<源端事务的start_scn时,这个变化是否会同步到目标库中?
  • 原文地址:https://www.cnblogs.com/end/p/2701915.html
Copyright © 2011-2022 走看看