Commit 131c4545 authored by Nadine Wieters's avatar Nadine Wieters

Merge branch...

Merge branch '190-changing-namelist-entries-in-runscript-will-delete-other-entries-that-start-with-same-string' into develop
parents 2af8677e d474c952
......@@ -23,6 +23,15 @@ general_remove_namelist_value()
}
## @fn general_replace_namelist_value()
## @brief
## Changes namelist file entries.
##
## @author Dirk Barbi
## Last changes:
## Added comments (Nadine Wieters)
## Bugfix: Fixed false behaviour when searching for string that is findable in more than one line of namelist. (Nadine Wieters)
general_replace_namelist_value()
{
namelist_file=$1
......@@ -46,6 +55,7 @@ general_replace_namelist_value()
fi
if [[ -r "$namelist_file" ]]; then
# grep -iw will only find if whole word (chapter) is equal search string for chapter
chapter_exists=`grep -iw "$chapter" $namelist_file | wc -l`
chapter_exists="$(echo -e "${chapter_exists}" | tr -d '[:space:]')"
......@@ -53,35 +63,56 @@ general_replace_namelist_value()
touch new_namelist
if [[ "x$chapter_exists" = "x1" ]]; then
actual_chapter=`grep -ioh "$chapter" "$namelist_file" | tr -d '\015'`
actual_chapter=`grep -iwoh "$chapter" "$namelist_file" | tr -d '\015'`
flag=0
while read line
do
# Remove carriage return
line=`echo $line | tr -d '\015'`
case $line in
${actual_chapter}* | ${actual_chapter}) flag=1; echo "$line" >> new_namelist; continue ;;
\&*|\&) flag=2; echo "$line" >> new_namelist; continue ;;
${actual_chapter}* | ${actual_chapter})
flag=1
echo "$line" >> new_namelist
continue
;;
\&*|\&)
flag=2
echo "$line" >> new_namelist
continue
;;
/|/* )
# If within the right chapter (flag=1)
# This will always add the changed entry at the end of the chapter
# If a line with '/' has been reached and flag is still 1, means: You are in the right chapter but the entry is not in the chapter and will be added at the end.
if [[ "x$flag" = "x1" ]]; then
if [[ "x$value" = "xREMOVE_FROM_NAMELIST" ]]; then
echo " Removing $thismodel namelist parameter $namelist_file -> $chapter -> $parameter"
# Actually do nothing...
else
echo " Changing $thismodel namelist parameter $namelist_file -> $chapter -> $parameter = $value"
echo -e "\t$parameter = $value" >> new_namelist
echo -e "\t$parameter=$value" >> new_namelist
fi
fi
flag=0 ;;
flag=0
;;
esac
# If within the right chapter (flag=1)
if [ "$flag" -eq 1 ]; then
echo "$line" | awk -v pat="${parameter}" ' BEGIN { FS = "=" } ; toupper($1) !~ toupper(pat) {print "\t" $0}' >> new_namelist
# If exact pattern is not found in line then write to new_namelist: Meaning, your are in the right chapter but this is not the entry you are looking for.
# The following line leads to error, if the parameter can be found in more than only one line.
# The line that includes the same search string will not be saved to new_namelist.
# echo "$line" | awk -v pat="${parameter}" ' BEGIN { FS = "=" } ; toupper($1) !~ toupper(pat) {print "\t" $0}' >> new_namelist
# Solution: Get rid of spaces (\040) in $line first and then compare if line holds the exact search string you are looking for.
echo $line | tr -d '\040' | awk -v pat="${parameter}" -F= 'toupper($1) != toupper(pat) {print "\t" $0}' >> new_namelist
elif [ "$flag" -eq 2 ]; then
# This is a line with the chapter.
echo "$line" | awk ' BEGIN { FS = "=" } ; {print "\t" $0}' >> new_namelist
else
echo "$line" >> new_namelist
fi
done < ${namelist_file}
else
# If chapter is not already in namelist:
if ! [[ "x$value" = "xREMOVE_FROM_NAMELIST" ]]; then
cp ${namelist_file} new_namelist >> $dumpfile
cat >> new_namelist << EOF
......@@ -90,10 +121,13 @@ $parameter = $value
/
EOF
else
# If no chapter found and parameter should only be removed (but is not in the namelist anyway)
# Copies namelist only back and forth with no changes.
mv ${namelist_file} new_namelist >> $dumpfile
fi
fi
else
# If there is no such namelist found: creates new namelist
if ! [[ "x$value" = "xREMOVE_FROM_NAMELIST" ]]; then
cat -> new_namelist << EOF
$chapter
......@@ -101,10 +135,12 @@ $parameter = $value
/
EOF
else
# If namelist entry is only going to be removed in a namelist that does not exists: Doing nothing
mv ${namelist_file} new_namelist >> $dumpfile
fi
fi
# Make new_namelist to the namelist for the experiment.
mv new_namelist ${namelist_file} >> $dumpfile
unset namelist_file line flag chapter parameter actual_chapter value chapter_exists
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment