Changeset 1166 for trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh
- Timestamp:
- 02/03/15 12:22:08 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libIGCM/libIGCM_comp/libIGCM_comp.ksh
r1165 r1166 950 950 } 951 951 952 953 954 #======================================================================= 955 function IGCM_comp_modifyFile 956 { 957 # 958 # syntax: IGCM_comp_modifyFile filein key [value] 959 # 960 # For example : IGCM_comp_modifyFile metrics_template.py case_id \'SE_${YEARS}\' 961 # 962 # This function is used to replace a pattern in a file for a specific variable. 963 # 964 # Arguments: 965 # - filein : the file in run directory in which the variable should be set 966 # - key : the variable to modify 967 # - value : the value to set the key equal to 968 # 969 IGCM_debug_PushStack "IGCM_comp_modifyFile" 970 971 typeset filein key value pattern 972 973 # Set local variables and test the arguments 974 if [ $# = 3 ] ; then 975 # Normal case with 3 arguments 976 filein=$1 ; key=$2 ; value=$3 977 else 978 IGCM_debug_Exit "IGCM_comp_modifyFile: Bad number of arguments." 979 IGCM_debug_PopStack "IGCM_comp_modifyFile" 980 return 981 fi 982 IGCM_debug_Print 1 "Entering IGCM_comp_modifyFile with arguments: ${filein} ${key} ${value}" 983 984 # Test if the file exist 985 if [ ! -f ${filein} ] ; then 986 IGCM_debug_Exit "IGCM_comp_modifyFile: ${filein} does not exist." 987 IGCM_debug_PopStack "IGCM_comp_modifyFile" 988 return 989 fi 990 991 # Read the line with key in the file without the comments 992 pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" ) 993 994 # Verify the existance of the pattern 995 if [ X"${pattern}" = X ] ; then 996 # Variable key is not set in filein, stop. 997 IGCM_debug_Exit "IGCM_comp_modifyFile : Variable ${key} is not set in correct file. It should be set in ${filein}." 998 IGCM_debug_PopStack "IGCM_comp_modifyFile" 999 return 1000 fi 1001 1002 # Now change key in filein 1003 sed -e "s:${pattern}:${key}=${value}:" ${filein} > ${filein}.tmp 1004 IGCM_debug_Print 1 "IGCM_comp_modifyFile: In ${filein} set ${key}=${value}" 1005 \mv ${filein}.tmp ${filein} 1006 1007 IGCM_debug_PopStack "IGCM_comp_modifyFile" 1008 } 1009 952 1010 #======================================================================= 953 1011 function IGCM_comp_modifyDefFile … … 977 1035 IGCM_debug_PushStack "IGCM_comp_modifyDefFile" 978 1036 1037 typeset type filein key value 1038 typeset filelist nb_occ modify 1039 979 1040 # Set local variables and test the arguments 980 1041 if [ $# = 4 ] ; then … … 989 1050 return 990 1051 fi 991 IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: $ type $filein $key $value"1052 IGCM_debug_Print 1 "Entering IGCM_comp_modifyDefFile with arguments: ${type} ${filein} ${key} ${value}" 992 1053 993 1054 # Test if first argument is correct 994 if [ $ type != blocker ] && [ $type != nonblocker ] && [ $type!= force ] ; then1055 if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then 995 1056 IGCM_debug_Exit "IGCM_comp_modifyDefFile: Error in first argument must be blocker, nonblocker or force" 996 1057 IGCM_debug_PopStack "IGCM_comp_modifyDefFile" … … 1000 1061 # Test if the file exist 1001 1062 if [ ! -f ${filein} ] ; then 1002 IGCM_debug_Exit "IGCM_comp_modifyDefFile: $ fileindoes not exist."1063 IGCM_debug_Exit "IGCM_comp_modifyDefFile: ${filein} does not exist." 1003 1064 IGCM_debug_PopStack "IGCM_comp_modifyDefFile" 1004 1065 return … … 1009 1070 1010 1071 # Count number of occurances for the key in all files 1011 nb_occ=$( grep -w $ key $filelist |grep -v "#" |wc -l )1072 nb_occ=$( grep -w ${key} ${filelist} | grep -v "#" | wc -l ) 1012 1073 1013 1074 # Test if key is set several times 1014 if [ $ nb_occ-gt 1 ] ; then1015 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $ filein: Variable=$keyis set ${nb_occ} times"1075 if [ ${nb_occ} -gt 1 ] ; then 1076 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} is set ${nb_occ} times" 1016 1077 IGCM_debug_PopStack "IGCM_comp_modifyDefFile" 1017 1078 return … … 1019 1080 1020 1081 # Treatement according to different cases 1021 if [ $ nb_occ -eq 0 ] && [ $type= blocker ] ; then1082 if [ ${nb_occ} -eq 0 ] && [ ${type} = blocker ] ; then 1022 1083 # Stop if the key is never set and the function is blocker 1023 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in $ filein: Variable=$keyhas been removed but this function is blocker. "1024 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore $ filein for variable $key."1025 elif [ $ nb_occ -eq 0 ] && [ $type= nonblocker ] ; then1084 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Error in ${filein}: Variable=${key} has been removed but this function is blocker. " 1085 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: restore ${filein} for variable ${key}." 1086 elif [ ${nb_occ} -eq 0 ] && [ ${type} = nonblocker ] ; then 1026 1087 # The key is not set but it is a nonblocker call so nothing is done. 1027 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $ key is not set in $filein. This is a nonblocker call so nothing is done."1028 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for $ keyfrom the model will be used."1088 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is not set in ${filein}. This is a nonblocker call so nothing is done." 1089 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: Default value for ${key} from the model will be used." 1029 1090 modify=no 1030 elif [ $( grep $ key $filein| grep -v "\#" |wc -l ) = 0 ] ; then1091 elif [ $( grep ${key} ${filein} | grep -v "\#" |wc -l ) = 0 ] ; then 1031 1092 # Variable key is not set in filein, stop. 1032 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable $ key is not set in correct file. It should be set in $filein."1093 IGCM_debug_Exit "IGCM_comp_modifyDefFile : Variable ${key} is not set in correct file. It should be set in ${filein}." 1033 1094 fi 1034 1095 1035 1096 # Check if AUTO is set in the filein on the same line as the key variable 1036 if [ $( grep -w $ key $filein| grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then1097 if [ $( grep -w ${key} ${filein} | grep -v "\#" | grep AUTO | wc -l ) = 1 ] ; then 1037 1098 # Modification will be done for all cases 1038 1099 modify=yes 1039 1100 else 1040 1101 # The variable was not set to AUTO 1041 if [ $ type= blocker ] ; then1102 if [ ${type} = blocker ] ; then 1042 1103 # Exit because this is a blocker call 1043 IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $ keycannot be modified. It should be set to AUTO."1104 IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} cannot be modified. It should be set to AUTO." 1044 1105 return 1045 elif [ $ type= nonblocker ] ; then1106 elif [ ${type} = nonblocker ] ; then 1046 1107 # Do nothing. Suppose that the user did set the variable correct 1047 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $ keyis set by the user. Nothing done."1108 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key} is set by the user. Nothing done." 1048 1109 modify=no 1049 elif [ $ type= force ] ; then1110 elif [ ${type} = force ] ; then 1050 1111 # Force modification 1051 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=$ keywas not set to AUTO. Modification will be forced."1112 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile : Variabl=${key} was not set to AUTO. Modification will be forced." 1052 1113 modify=yes 1053 1114 fi … … 1055 1116 1056 1117 # Do the modifcation now 1057 if [ $ modify= yes ] ; then1118 if [ ${modify} = yes ] ; then 1058 1119 1059 1120 # For option DEFAULT, read default value from file. 1060 if [ X"$ value" = XDEFAULT ] || [ X"$value" = X ] ; then1121 if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then 1061 1122 # Case to set DEFAULT value 1062 1123 # Read default value from filein 1063 value=$( grep $ key $filein| grep -v "\#" | awk -F"DEFAULT *=" '{print $2}')1064 1065 if [ X"$ value" = X ] ; then1066 IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable $ key needs a DEFAULT value in $filein."1067 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in $ fileinshould be:"1068 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: $ key=_AUTO_:DEFAULT=def_value"1124 value=$( grep ${key} ${filein} | grep -v "\#" | awk -F"DEFAULT *=" '{print $2}') 1125 1126 if [ X"${value}" = X ] ; then 1127 IGCM_debug_Exit "IGCM_comp_modifyDefFile : The variable ${key} needs a DEFAULT value in ${filein}." 1128 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: The syntax in ${filein} should be:" 1129 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: ${key}=_AUTO_:DEFAULT=def_value" 1069 1130 IGCM_debug_PopStack "IGCM_comp_modifyDefFile" 1070 1131 return … … 1073 1134 1074 1135 # Now change key in filein 1075 sed -e "s/^${key}\ *=.*/${key}= ${value}/" $ filein > $filein.tmp1076 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In $ filein set $key=$value"1077 \mv $ filein.tmp $filein1136 sed -e "s/^${key}\ *=.*/${key}= ${value}/" ${filein} > ${filein}.tmp 1137 IGCM_debug_Print 1 "IGCM_comp_modifyDefFile: In ${filein} set ${key}=${value}" 1138 \mv ${filein}.tmp ${filein} 1078 1139 fi 1079 1140 IGCM_debug_PopStack "IGCM_comp_modifyDefFile" … … 1107 1168 IGCM_debug_PushStack "IGCM_comp_modifyNamelist" 1108 1169 1170 typeset type filein key value pattern modify 1171 1109 1172 # Set local variables and test the arguments 1110 1173 if [ $# = 4 ] ; then … … 1119 1182 return 1120 1183 fi 1121 IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: $ type $filein $key $value"1184 IGCM_debug_Print 1 "Entering IGCM_comp_modifyNamelist with arguments: ${type} ${filein} ${key} ${value}" 1122 1185 1123 1186 # Test if first argument is correct 1124 if [ $ type != blocker ] && [ $type != nonblocker ] && [ $type!= force ] ; then1187 if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then 1125 1188 IGCM_debug_Exit "IGCM_comp_modifyNamelist: Error in first argument must be blocker, nonblocker or force" 1126 1189 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" … … 1130 1193 # Test if the file exist 1131 1194 if [ ! -f ${filein} ] ; then 1132 IGCM_debug_Exit "IGCM_comp_modifyNamelist: $ fileindoes not exist."1195 IGCM_debug_Exit "IGCM_comp_modifyNamelist: ${filein} does not exist." 1133 1196 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 1134 1197 return … … 1136 1199 1137 1200 # Read the line with key in the file without the comments 1138 pattern=$( grep "^ *$ key *=" $filein| sed -e "s% *\!.*%%" )1201 pattern=$( grep "^ *${key} *=" ${filein} | sed -e "s% *\!.*%%" ) 1139 1202 1140 1203 # Verify the existance of the pattern 1141 1204 if [ X"$pattern" = X ] ; then 1142 1205 # Variable key is not set in filein, stop. 1143 IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable $ key is not set in correct file. It should be set in $filein."1206 IGCM_debug_Exit "IGCM_comp_modifyNamelist : Variable ${key} is not set in correct file. It should be set in ${filein}." 1144 1207 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 1145 1208 return … … 1152 1215 else 1153 1216 # The variable was not set to AUTO 1154 if [ $ type= blocker ] ; then1217 if [ ${type} = blocker ] ; then 1155 1218 # Exit because this is a blocker call 1156 IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $ keycannot be modified. It should be set to AUTO."1219 IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} cannot be modified. It should be set to AUTO." 1157 1220 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 1158 1221 return 1159 elif [ $ type= nonblocker ] ; then1222 elif [ ${type} = nonblocker ] ; then 1160 1223 # Do nothing. Suppose that the user did set the variable correct 1161 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $ keyis set by the user. Nothing done."1224 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key} is set by the user. Nothing done." 1162 1225 modify=no 1163 elif [ $ type= force ] ; then1226 elif [ ${type} = force ] ; then 1164 1227 # Force modification 1165 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=$ keywas not set to AUTO. Modification will be forced."1228 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist : Variabl=${key} was not set to AUTO. Modification will be forced." 1166 1229 modify=yes 1167 1230 fi … … 1169 1232 1170 1233 # Do the modifcation now 1171 if [ $ modify= yes ] ; then1234 if [ ${modify} = yes ] ; then 1172 1235 1173 1236 # For option DEFAULT, read default value from file. 1174 if [ X"$ value" = XDEFAULT ] || [ X"$value" = X ] ; then1237 if [ X"${value}" = XDEFAULT ] || [ X"${value}" = X ] ; then 1175 1238 # Case to set DEFAULT value 1176 1239 # Read default value from filein 1177 1240 value=$( echo $pattern | awk -F"DEFAULT *=" '{print $2}') 1178 1241 1179 if [ X"$ value" = X ] ; then1180 IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable $ key needs a DEFAULT value in $filein."1181 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in $ fileinshould be:"1182 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: $ key=_AUTO_:DEFAULT=def_value"1242 if [ X"${value}" = X ] ; then 1243 IGCM_debug_Exit "IGCM_comp_modifyNamelist : The variable ${key} needs a DEFAULT value in ${filein}." 1244 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: The syntax in ${filein} should be:" 1245 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: ${key}=_AUTO_:DEFAULT=def_value" 1183 1246 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" 1184 1247 return … … 1187 1250 1188 1251 # Now change key in filein 1189 sed -e "s/${pattern}/ $ key=${value}/" $filein > $filein.tmp1190 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In $ filein set $key=$value"1191 \mv $ filein.tmp $filein1252 sed -e "s/${pattern}/ ${key}=${value}/" ${filein} > ${filein}.tmp 1253 IGCM_debug_Print 1 "IGCM_comp_modifyNamelist: In ${filein} set ${key}=${value}" 1254 \mv ${filein}.tmp ${filein} 1192 1255 fi 1193 1256 IGCM_debug_PopStack "IGCM_comp_modifyNamelist" … … 1221 1284 IGCM_debug_PushStack "IGCM_comp_modifyXmlFile" 1222 1285 1286 typeset type filein keyid keyattrib value modify 1287 1223 1288 # Set local variables and test the arguments 1224 1289 if [ $# = 5 ] ; then … … 1230 1295 return 1231 1296 fi 1232 IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=$ type file=$filein, id=$keyid attribute=$keyattrib, value=$value"1297 IGCM_debug_Print 1 "Entering IGCM_comp_modifyXmlFile with arguments: type=${type} file=${filein}, id=${keyid} attribute=${keyattrib}, value=${value}" 1233 1298 1234 1299 # Test if first argument is correct 1235 if [ $ type != blocker ] && [ $type != nonblocker ] && [ $type!= force ] ; then1300 if [ ${type} != blocker ] && [ ${type} != nonblocker ] && [ ${type} != force ] ; then 1236 1301 IGCM_debug_Exit "IGCM_comp_modifyXmlFile: Error in first argument must be blocker, nonblocker or force" 1237 1302 IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" … … 1241 1306 # Test if the file exist 1242 1307 if [ ! -f ${filein} ] ; then 1243 IGCM_debug_Exit "IGCM_comp_modifyXmlFile: $ fileindoes not exist."1308 IGCM_debug_Exit "IGCM_comp_modifyXmlFile: ${filein} does not exist." 1244 1309 IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" 1245 1310 return … … 1247 1312 1248 1313 # Test if keyid is set in filein, otherwise exit 1249 if [ $( grep -w $ keyid $filein| wc -l ) = 0 ] ; then1314 if [ $( grep -w ${keyid} ${filein} | wc -l ) = 0 ] ; then 1250 1315 # Variable key is not set in filein, stop. 1251 IGCM_debug_Exit "IGCM_comp_modifyXmlFile : $ keyid is not set in the file. Bad syntax of $fileinfile."1316 IGCM_debug_Exit "IGCM_comp_modifyXmlFile : ${keyid} is not set in the file. Bad syntax of ${filein} file." 1252 1317 IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" 1253 1318 return … … 1255 1320 1256 1321 # Check if AUTO is set on the same line as keyid and keyattrib 1257 if [ $( grep -w $ keyid $filein| grep AUTO | wc -l ) = 1 ] ; then1322 if [ $( grep -w ${keyid} ${filein} | grep AUTO | wc -l ) = 1 ] ; then 1258 1323 # Modifification will be done 1259 1324 modify=yes 1260 1325 else 1261 if [ $ type= blocker ] ; then1326 if [ ${type} = blocker ] ; then 1262 1327 # Exit, the variable must be set to AUTO 1263 IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The $ keyattrib for $keyid must be set to AUTO in $filein."1328 IGCM_debug_Exit "IGCM_comp_modifyXmlFile : blocker function. The ${keyattrib} for ${keyid} must be set to AUTO in ${filein}." 1264 1329 IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" 1265 1330 return 1266 elif [ $ type= nonblocker ] ; then1331 elif [ ${type} = nonblocker ] ; then 1267 1332 # Nothing will be done 1268 1333 IGCM_debug_Print 1 "Nonblocker nothing is done for ${filein}, id=${keyid} and attribute ${keyattrib}" 1269 1334 modify=no 1270 elif [ $ type= force ] ; then1335 elif [ ${type} = force ] ; then 1271 1336 # Force modification 1272 IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=$ keyattrib for id=$keyidwas not set to AUTO. Modification will be forced."1337 IGCM_debug_Print 1 "IGCM_comp_modifyXmlFile : Attribute=${keyattrib} for id=${keyid} was not set to AUTO. Modification will be forced." 1273 1338 modify=yes 1274 1339 fi … … 1276 1341 1277 1342 # Do the modifcation now 1278 if [ $ modify= yes ] ; then1279 if [ $ keyattrib= NONE ] ; then1343 if [ ${modify} = yes ] ; then 1344 if [ ${keyattrib} = NONE ] ; then 1280 1345 # Case to modify the variable itself 1281 1346 IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting the variable=${value}" 1282 sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" $ filein > $filein.tmp1347 sed -e "s/\(<[^\"]*\"${keyid}\".*>\)\([^<]*\)\(<[^>]*\)/\1${value}\3/" ${filein} > ${filein}.tmp 1283 1348 else 1284 1349 # Check if keyattrib is set on the same line as keyid 1285 if [ $( grep -w $ keyid $filein | grep $keyattrib| wc -l ) = 1 ] ; then1350 if [ $( grep -w ${keyid} ${filein} | grep ${keyattrib} | wc -l ) = 1 ] ; then 1286 1351 # Case to modify the attribute value 1287 1352 IGCM_debug_Print 1 "Now modify ${filein} for id=${keyid} by setting attribute to ${keyattrib}=${value}" 1288 sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" $ filein > $filein.tmp1353 sed -e "/id=\"${keyid}\"/s/\(${keyattrib}=\"\)[^\"]*\(\"\)/\1${value}\2/" ${filein} > ${filein}.tmp 1289 1354 else 1290 1355 # Case to add the attribute and its value 1291 1356 IGCM_debug_Print 1 "Now add in ${filein} for id=${keyid} the attribute ${keyattrib} to the value ${value}" 1292 sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" $ filein > $filein.tmp1357 sed -e "/id=\"${keyid}\"/s/\/>/ ${keyattrib}=\"${value}\"\/>/" ${filein} > ${filein}.tmp 1293 1358 fi 1294 1359 fi 1295 \mv $ filein.tmp $filein1360 \mv ${filein}.tmp ${filein} 1296 1361 fi 1297 1362 IGCM_debug_PopStack "IGCM_comp_modifyXmlFile" … … 1693 1758 # Append the sync call and the copy sequence to the IGCM_FlushRebuild function if needed 1694 1759 if [ ${#rebuildedActionsList[*]} -ne 0 ] ; then 1695 echo "IGCM_sys_sync " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh1760 echo "IGCM_sys_sync " >> ${RUN_DIR}/REBUILD_${PeriodDateBegin}/rebuild.ksh 1696 1761 i=0 1697 1762 until [ ${i} -ge ${#rebuildedActionsList[*]} ]; do
Note: See TracChangeset
for help on using the changeset viewer.