2017-04-24 87 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? –