2017-04-24 88 views
0

我正在研究一個允許我添加,刪除或編輯配置文件的腳本。我已經對它進行了一些測試,看起來好像我至少能夠使用單個文件工作,但我希望能夠執行.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 
+0

你的配置文件是什麼樣的?你想在做-a,-b或-d之後看起來像什麼? –

+0

配置文件只是一個巨大的屬性文件,其中包含版本號,IP地址,密碼等。-a在找到匹配項後添加所需的行,-b相同但在之前,-d刪除找到的行,-e查找匹配項並更換。大多數情況下,我只能在一個配置文件上工作,但有可能我希望它發生在一堆不同的配置文件中 –

+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 "[email protected]"); do 
    do_work "$File" "[email protected]" 
done 

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

我很感激幫助。我可能做得不對,但我已經嘗試了* .txt和「* .txt」和.txt,它可以在第一個文件上工作,但是我在第二個有效文件上得到了無效文件。也需要在esac之前完成,並刪除底部的esac循環..無效otpion'f.txt' 與f.txt是一個實際的文件 –

+0

我想出了多個文件。現在測試,但我認爲這正是我所期待的。謝謝! –

+1

任何方式來做文件* .txt?或者你只需​​要做-f file.txt -f f.txt? –