[2] | 1 | #!/usr/bin/awk -f |
---|
| 2 | |
---|
| 3 | #************************************************************** |
---|
| 4 | # Author: Patrick Brockmann |
---|
| 5 | # Contact: Patrick.Brockmann@cea.fr |
---|
[4] | 6 | # $Date$ |
---|
[5] | 7 | # $Author$ |
---|
| 8 | # $Revision$ |
---|
[2] | 9 | # IPSL (2006) |
---|
| 10 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 11 | # History: |
---|
| 12 | # Modification: |
---|
| 13 | # |
---|
| 14 | #************************************************************** |
---|
| 15 | |
---|
| 16 | #========================== |
---|
| 17 | function myprint(str) { |
---|
| 18 | if (debug) { |
---|
| 19 | print str |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | #========================== |
---|
| 24 | BEGIN { |
---|
| 25 | |
---|
| 26 | nbarg=ARGC |
---|
| 27 | if (match(ARGV[1],"-d")) { |
---|
| 28 | debug=1 |
---|
| 29 | file=ARGV[2] |
---|
| 30 | section=ARGV[3] |
---|
| 31 | option=ARGV[4] |
---|
| 32 | value=ARGV[5] |
---|
| 33 | delete ARGV[1] ; delete ARGV[3] ; delete ARGV[4] ; delete ARGV[5] |
---|
| 34 | nbarg-- |
---|
| 35 | } else { |
---|
| 36 | debug=0 |
---|
| 37 | file=ARGV[1] |
---|
| 38 | section=ARGV[2] |
---|
| 39 | option=ARGV[3] |
---|
| 40 | value=ARGV[4] |
---|
| 41 | delete ARGV[2] ; delete ARGV[3] ; delete ARGV[4] |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | # When exit statement, 'END' rule is always executed, so defined a exit_value to manage this |
---|
| 46 | exit_value=0 |
---|
| 47 | if (nbarg != 5) { |
---|
| 48 | print "Usage: IGCM_card_WriteOption [-d] file section option value" |
---|
| 49 | print |
---|
| 50 | print "Args:" |
---|
| 51 | print " file = file at CARD format" |
---|
| 52 | print " section = section to find" |
---|
| 53 | print " option = option to find" |
---|
| 54 | print " value = value option to write" |
---|
| 55 | print |
---|
| 56 | print "Options:" |
---|
| 57 | print " -d = debug mode" |
---|
| 58 | print |
---|
| 59 | exit_value=1 |
---|
| 60 | exit |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | section_found=0 |
---|
| 64 | section_inside=0 |
---|
| 65 | option_found=0 |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | #========================== |
---|
| 69 | { |
---|
| 70 | |
---|
| 71 | myprint($0) |
---|
| 72 | |
---|
| 73 | # Find section with delimiters '[' and ']' |
---|
| 74 | if (match($0, "\\[" section "\\]")) { |
---|
| 75 | |
---|
| 76 | myprint("---->section found") |
---|
| 77 | section_found=1 |
---|
| 78 | section_inside=1 |
---|
| 79 | print $0 |
---|
| 80 | |
---|
| 81 | } else if (section_inside == 1 && (match($0, "^[ ]*" option "[ ]*="))) { |
---|
| 82 | |
---|
| 83 | myprint("---->option found") |
---|
| 84 | option_found=1 |
---|
| 85 | |
---|
| 86 | print option "= " value |
---|
| 87 | myprint("---->new value ") |
---|
| 88 | |
---|
| 89 | } else if (section_inside == 1 && match($0, /^[\[*\]]/)) { |
---|
| 90 | |
---|
| 91 | myprint("---->end section") |
---|
| 92 | section_inside=0 |
---|
| 93 | print $0 |
---|
| 94 | |
---|
| 95 | } else { |
---|
| 96 | print $0 |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | #========================== |
---|
| 102 | END { |
---|
| 103 | |
---|
| 104 | if (! exit_value ) { |
---|
| 105 | |
---|
| 106 | myprint("###############################") |
---|
| 107 | myprint("section ====> " section) |
---|
| 108 | myprint("option ====> " option) |
---|
| 109 | myprint("value ====> " value) |
---|
| 110 | |
---|
| 111 | if (section_found == 0) { |
---|
| 112 | print "Error: Section not found" |
---|
| 113 | } else if (option_found == 0) { |
---|
| 114 | print "Error: Option not found" |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | #========================== |
---|