1.创建一个脚本,能为其他脚本添加脚本信息:
    #vim cs.sh
        #!/bin/bash
        cat > $1 <<EOF
        #!/bin/bash
        #Name: `basename $1`
        #Description:
        #Author:TEST
        #Version:0.0.1
        #Datatime:`date "+%F +%T"`
        #Usage:`basename $1`

        EOF
        vim +8 $1

2.修改脚本,若打开的脚本中有内容,则直接打开,若为空则添加信息。
    #vim cs.sh
        #!/bin/bash
        if ! grep “[^[::space]]” $1 &> /dev/null ; then
            cat > $1 <<EOF
            #!/bin/bash
            #Name: `basename $1`
            #Description:
            #Author:TEST
            #Version:0.0.1
            #Datatime:`date "+%F +%T"`
            #Usage:`basename $1`

            EOF
             vim +8 $1
        fi
        vim  + $1

3.修改脚本,添加语法测试功能。若有错误让用户选择是否退出编辑。
     #vim cs.sh
        #!/bin/bash
        if ! grep “[^[::space]]” $1 &> /dev/null ; then
            cat > $1 <<EOF
            #!/bin/bash
            #Name: `basename $1`
            #Description:
            #Author:TEST
            #Version:0.0.1
            #Datatime:`date "+%F +%T"`
            #Usage:`basename $1`

            EOF
             vim +8 $1
        fi
        vim  + $1

        until banshi -n $1 &> /dev/null ;do
            read  -p "Syntax error ,q|Q for quit,others for edit." OPT
            case $1 in {
                q|Q )
                    echo "quit"
                    exit 5
                    ;;
                *)
                      vim + $1
                      ;;
            }
            esac
        done

        chmod +x $1

4.使用刚才的cs.sh创建脚本,并为脚本添加选项的功能。:
    #./cs.sh opt.sh
             #!/bin/bash
            #Name: opt.sh
            #Description:
            #Author:TEST
            #Version:0.0.1
            #Datatime:xxxx-xxxx
            #Usage:opt.sh
            function USAGE() {
                echo "Usage:  opt.sh [-b|-d] args"

            }
            while getopts  ":b:d:"  SW ;do
                case $SW in
                    b)
                        echo "the option is  b"
                        echo $OPTARG
                        ;;
                    d)
                        echo "the option is d"
                        echo $OPTARG
                        ;;
                    \?)
                        echo "wrong options"
                        USAGE
                        ;;
                esac
            done
        #./opt.sh -b test1
        #./opt.sh -d test2
        #./opt.sh -c test3

5.修改 sc.sh,创建脚本时使用选项让用户输入description。
      #vim cs.sh
        #!/bin/bash
        while getopts ":d:" SW;do
            case $SW in
                d)
                    DESC=$OPTARG
                \?)
                    echo "Usage: sc.sh [-d DESCRIPTION] FILENAME"
            esca
        done

        shift $[$OPTIND-1]

        if ! grep “[^[::space]]” $1 &> /dev/null ; then
            cat > $1 <<EOF
            #!/bin/bash
            #Name: `basename $1`
            #Description:$DESC
            #Author:TEST
            #Version:0.0.1
            #Datatime:`date "+%F +%T"`
            #Usage:`basename $1`

            EOF
             vim +8 $1
        fi
        vim  + $1

        until banshi -n $1 &> /dev/null ;do
            read  -p "Syntax error ,q|Q for quit,others for edit." OPT
            case $1 in {
                q|Q )
                    echo "quit"
                    exit 5
                    ;;
                *)
                      vim + $1
                      ;;
            }
            esac
        done

        chmod +x $1   

6.编辑opt.sh,查看OPTIND的具体情况。
    #vim opt.sh
        #!/bin/bash
        #Name: opt.sh
        #Description:
        #Author:TEST
        #Version:0.0.1
        #Datatime:xxxx-xxxx
        #Usage:opt.sh
        function USAGE() {
            echo "Usage:  opt.sh [-b|-d] args"

        }
        while getopts  ":bd"  SW ;do
            case $SW in
                b)
                    echo "the option is  b"
                    echo $OPTARG
                    echo $OPTIND
                    ;;
                d)
                    echo "the option is d"
                    echo $OPTARG
                    echo $OPTIND
                    ;;
                \?)
                    echo "wrong options"
                    USAGE
                    ;;
            esac
        done
    #./opt.sh -b
        2
    #./opt.sh -b -d
        2
        3
7.写一个脚本ift.sh,可以接受选项 i I a ,要求如下:
    usage如下: ift.sh [-i INTERFACE|-I  IP| -a ]
    -i 显示指定网卡的ip地址,-I 显示IP地址对应的接口,-a显示出io接口外的所有接口和ip信息。

    #vim ift.sh
        #!/bin/bash
        SHOWIP() {
            if  ! ifconfig|grep -o "^[^[:space:]]\{1,\}" |grep $1 &> /dev/null; then
                return 15
            fi

            echo -n "${1}: "
            ifconfig $1 |grep -o "inet addr:[0-9\.]\{1,\}" |cut -d: -f2
            echo
        }

        SHOWIF(){
            if ! ifconfig |grep  -o "inet addr:[0-9\.]\{1,\}" |cut -d: -f2|grep $1 &> /dev/null;then
                return 16
            fi

            echo -n "${1}:"
            ifconfig |grep -B 1 "$1" |grep -o "^[^[:space:]]\{1,\}"
            echo
        }

        SHOWALL(){
          暂时没想出来 先空着!!稍后补齐


        }

        while getopts  ":i:I:a:" SW ;do
            case $SW in
                i)
                    SHOWIP $OPTARG
                    if [$? -eq 15] && echo "wrong interface!"
                    ;;
                I)
                    SHOWIF $OPTARG
                    f [$? -eq 16] && echo "wrong ip!"
                    ;;
                a)
                    SHOWALL
                    ;;

                \?)
                    echo “Usage ift.sh [-i INTERFACE|-I  IP| -a ]”
                    ;;
            esac



            


#getopts "[:]OPTION[:]..." [OPTARG]
    内置参数:
        OPTARG 参数
        OPTIND 选项索引,脚本中有多个选项时,最开始指向第二个选项,注意不是第一个!! 。使用shift后则指向第三个选项,以此类推。

    OPTION 选项可以有多个,如果参数后边有 OPTARG选项内置的参数,选项后如果后参数,则要在选项后边加上冒号(:)。
    若不想输出错误信息,则在所有选项最前头加上冒号(:)。
    注意,一次只能跟一个选项,不能跟多个。