用于编辑配置文件的脚本

时间:2017-04-24 18:29:35

标签: bash shell sed

我正在编写一个允许我添加,删除或编辑配置文件的脚本。我已经对它进行了一些测试,看起来我至少使用了一个文件,但是我希望能够只执行 .config或fi .config并让它执行期望的行动。

我将不胜感激。

配置文件外观看起来类似于这个更大的

-- Config File
-- Environment DEV7
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
-- General properties
-------------------------------------------------------------------------------

com.x.yy.zz.version=2.0.2

com.x.yy.zz.instanceRole.ServerA=PRIMARY
com.x.yy.zz.instanceRole.ServerB=SECONDARY

com.x.yy.zz.StopDelay=30
com.x.yy.zz.sourceType=t
com.x.yy.zz.sNumberInc=20000
com.x.yy.zz.sNumberMin=20000
com.x.yy.zz.sNumberMax=9980000

so -a would allow me to add a line after something
ex. -a StopDealy New
com.x.yy.zz.StopDelay=30
New





#!/bin/bash
i=1

usage()
{
        echo "test usage"
}

if [[ $# -gt 4  ]]
then
        i=2
fi

while [[ $# -gt $i ]]
do
key="$1"

case $key in
        -f|--file)
        file="$2"
        shift
        ;;
    -a|--after)
    sed -i  "/$2/a $3" $file
    #shift # past argument
    ;;
    -b|--before)
    sed -i "/$2/i $3" $file
        #shift
        ;;
    -d|--delete)
    sed -i "/$2/d" $file
        #shift
        ;;
        -e|--edit)
        sed -ie "s/$2/$3/g" $file
        shift
        ;;
    *)
            usage
    ;;
esac
shift # past argument or value
done

1 个答案:

答案 0 :(得分:0)

我还没有测试过,但这是我理解你想要达到的最接近的版本。

#!/bin/bash

usage() {
        echo "Usage: $0 -f file1 -f *.txt -[abde] ... -f file3 ... -[abde] ... "
}

# Do all the required action on one file
do_work() {
    file="$1" # 1st argument must be the file to work on
    shift
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -f|--file)   while [[ ! "$2" = -* && $# -gt 0 ]]; do shift; done ;; # Ignore any "file" since we have one to work on.
            -a|--after)  sed -i  "/$2/a $3" $file;  shift 2 ;;
            -b|--before) sed -i "/$2/i $3" $file;   shift 2 ;;
            -d|--delete) sed -i "/$2/d" $file;      shift ;;
            -e|--edit)   sed -ie "s/$2/$3/g" $file; shift 2 ;;
        esac
        shift # past argument or value
    done
}

# Check the arguments for files and print the valid ones
#   Other arguments will just be skipped
#   Invalid arguments will be displayed.
identify_files() {
    while [[ $# -gt 0 ]]; do
        case "$1" in
            -f|--file)   # Validate the the file exists (just in case)
                # check each following argument until next option
                # ... or end of arguments
                while [[ ! "$2" = -* && $# -gt 0 ]]; do
                    if [[ -f "$2" ]]; then
                        echo "$2"
                    else
                        echo "Error: Invalid file '$2'." >&2
                    fi
                    shift
                done ;;
            -[abe]) shift 2 ;;
            -d)     shift ;;
            -h)     usage >&2; exit ;;
            *)  echo "Invalid otpion '$1'" >&2 ;;
        esac
        shift
    done
}

# Do the required actions on all files received in the options
for File in $(identify_files "$@"); do
    do_work "$File" "$@"
done

## Alternative on predefined files (not the ones received as arguments)
##    Usage: $0 -[abde] ...
#for File in $(ls *.config); do
#    do_work "$File" "$@"
#done
相关问题