Shell腳本函數

1.讀取配置

配置文件:
key=value

#!/bin/bash
declare -A kv_dict=()

function LoadConf()
{
    file_name=$1

    IFS="="
    while read key value 
    do
        kv_dict[$key]=$value
    done < $file_name
}

2.修改文件指定行

參考鏈接

3.自定義shell命令

#!/bin/bash

check_path(){
    str=${1}
    len=${#str}
    let begin=len-1
    tmp=${str:begin:len}
    if [ "${tmp}" == "/" ]
    then
        return 0
    else
        return 1
    fi
}

if [ $# -eq 2 ]
then
    check_path ${2}
    ret=$?
    if [ ${ret} -eq 0 ]
    then
        path=${2}
    else
        path=${2}"/"
    fi
    case "${1}" in
        -ls)
            curl api/list?path=${path} | jq -r .data
            exit 0
            ;;  
        -test)
            ret_code=`curl  api/test?path=${path} | jq -r .code`
            mes=`curl  api/test?path=${path} | jq -r .message`
            if [ ${ret_code} -eq  0  ]
            then
                if [[  ${mes} =~ "not" ]]
                then
                    echo "NOT EXIST"
                    exit 1
                else
                    echo "EXIST"
                    exit 0
                fi
            else
                echo "NOT EXIST"
            fi
            exit ${ret_code}
            ;;
        -touch)
            ret_code=`curl api/touch?path=${path} | jq -r .code`
            if [ ${ret_code} -eq 0 ]
            then
                echo "TOUCH SUCCESS"
            else
                echo "TOUCH FAILED"
            fi
            exit ${ret_code}
            ;;
        -mkdir)
            ret_code=`curl api/mkdir?path=${path} | jq -r .code`
            if [ ${ret_code} -eq 0 ]
            then
                echo "MKDIR SUCCESS"
            else
                echo "MKDIR FAILED"
            fi
            exit ${ret_code}
            ;;
    esac
fi

if [ $# -eq 3 ]
then
case ${1} in 
    -put)
        check_path ${2}
        ret=$?
        if [ ${ret} -eq 0 ]
        then
            local_path=${2}
        else
            local_path=${2}"/"
        fi
        check_path ${3}
        ret=$?
        if [ ${ret} -eq 0 ]
        then
            dist_path=${3}
        else
            dist_path=${3}"/"
        fi
        local_path=${2}
        dist_path=${3}
        ret_code=`curl -F "file=@${local_path}" api/put?path=${dist_path} | jq -r .code`
        if [ ${ret_code} -eq 0 ]
        then
            echo "PUT SUCCESS"
        else
            echo "PUT FAILED"
        fi
        exit ${ret_code}
        ;;
esac
fi

if [ $# -eq 4 ]
then
    case ${1} in
        -get)
        check_path ${3}
        ret=$?
        if [ ${ret} == 0 ]
        then
            source_path=${3}
        else
            source_path=${3}"/"
        fi
        local_path=${4}
        case ${2} in
            -f)
                if test -d ${local_path}
                then
                    file=`basename ${source_path}`
                    echo ${source_path}
                    curl api/download?path=${source_path} > ${local_path}"/"${file}
                    if test -f ${local_path}"/"${file}
                    then
                        exit 0
                    else
                        exit 1
                    fi
                else
                    echo ${source_path}
                    curl api/download?path=${source_path} > ${local_path}
                    if test -f ${local_path}
                    then
                        exit 0
                    else
                        exit 1
                    fi
                fi
                ;;
            -d)
                data=`curl api/list?path=${source_path} | jq -r .data`
                str="${data}"
                len=${#str}
                let len=len-2
                new_str=${str:1:len}
                for item in ${new_str}
                do
                    tmp=${item%\"*}
                    tmp=${tmp#*\"}
                    file=`basename ${tmp}`
                    echo ${tmp}
                    curl api/download?path=${tmp}"/" > ${local_path}"/"${file}
                done
                if test -f ${local_path}"/_SUCCESS"
                then
                    exit 0
                else
                    exit 1
                fi
                ;;
        esac
        ;;
esac
fi

echo "usage:"
echo "cos -ls path"
echo "cos -get -f  source_path local_path"
echo "cos -get -d  source_path local_path"
echo "cos -test -f filepath"
echo "cos -test -d filepath"
echo "cos -put local_path dist_path"
exit 1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章