default =0
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
现在想写一个bash脚本,能够
1.根据default来判断需要修改哪组个配置
2.能在dubug或者enable后面自动加参数
请问能做到吗?
抱歉浪费大家脑细胞了!
2 个解决方案
#1
有点繁杂,抛砖引玉
使用时将需要加入的参数作为shell脚本参数传入即可
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable ,helloooooooooooooooo
enable = 1,autostart ,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
使用时将需要加入的参数作为shell脚本参数传入即可
#! /bin/sh
awk -F"=" -v arg=$1 'BEGIN{flag=0;group=0}{if($0 ~"default"){flag=$2;}if($0 ~"dubug")group++;if(flag==group && $0 ~"dubug"){$0=$0","arg;} if(flag==group && $0 ~"enable")$0=$0","arg;print $0}' yourfile
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable ,helloooooooooooooooo
enable = 1,autostart ,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
#2
相当感谢!
#1
有点繁杂,抛砖引玉
使用时将需要加入的参数作为shell脚本参数传入即可
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable ,helloooooooooooooooo
enable = 1,autostart ,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
使用时将需要加入的参数作为shell脚本参数传入即可
#! /bin/sh
awk -F"=" -v arg=$1 'BEGIN{flag=0;group=0}{if($0 ~"default"){flag=$2;}if($0 ~"dubug")group++;if(flag==group && $0 ~"dubug"){$0=$0","arg;} if(flag==group && $0 ~"enable")$0=$0","arg;print $0}' yourfile
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable ,helloooooooooooooooo
enable = 1,autostart ,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
#2
相当感谢!