This feed contains pages in the "bash" category.

I had to move some existing code into a namespace. As it was too much code for doing this manually, I developed the following bits of shell script:

#!/bin/bash
#
#############################################################
# "THE BEER-WARE LICENSE" (Revision 42):
# <robert -at- xenim -dot- de> wrote this file. As long as you
# retain this notice you can do whatever you want
# with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.
#############################################################
NAMESPACE="mynamespace"
SRCDIR=/path/to/sources

find $SRCDIR -name '*.cpp' | while read f; do 
    LNUM=`awk '/#include/{a=NR}; END{print a}' "$f"`;
    if [ ! -z "$LNUM" ]; then LNUM=1; fi;
    sed -i -e "$LNUM a \\\nnamespace $NAMESPACE {" -e '$,$ a } /* end namespace $NAMESPACE */' "$f";
done

find $SRCDIR -name '*.h' | while read f; do
    LNUM=`awk '/#include/{a=NR}; END{print a}' "$f"`;
    if [ -z "$LNUM" ]; then 
        LNUM=`awk '/#define/{a=NR}; END{print a}' "$f"`;
    fi;
    sed -i -e "$LNUM a \\\nnamespace $NAMESPACE {" "$f";
    LNUM=`awk '/#endif/{a=NR}; END{print a}' "$f"`;
    sed -i -e "$LNUM i } /* end namespace $NAMESPACE */\n" "$f";
done

The first find loops over all source-files (assumed suffix is .cpp here) and inserts a namespace-declaration after the last occuring #include. If you keep all #inludes at the top of your file, this should work fine. The corresponding closing bracket is simply added at the end of the file.

The second loop edits the header-files. The namespace-declaration is also added after the last #include if there is any. Otherwise the declaration is inserted right after the include-guard. The closing bracket is added here before the include-guard ends.

And finally the usual warning: Backup your files before using this!

Posted Wed Aug 10 13:42:01 2011 Tags: bash

During my work on a term paper for "nonlinear finite element method" I encountered the problem to do a study about the convergence of a certain problem.

As the the program we had to use to do the calculations (feap) reads all informations needed to solve the problem from an "so-called" inputfile and writes the results to another file called "outputfile". When there is the need to adjust five parameters, it gets a bit messy to change the inputfile, run FEAP and grab the resulting displacement out of the outputfile for several combinations of the parameters.

As an experienced user the fasted way to accomplish this, is to write a little which automates that. Since the people which evaluated the term paper where quite impressed about it, I am documenting it here too. (I am still wondering how they worked with FEAP for years without such a fairly easy wrapper)

So here is the code:

I reduced the number of parameters to three and removed the part which generates a some gnuplot-code for clarity. Just add another loop if you have more parameters.

In the inputfile all relevant parameters should be also declared as parameters:

...
para
p=2    !load
dx=30  !# of horizontal elmts
dy=10  !# of vertical elmts
nu=0.1 !
...
After calculating the solution, there should be a statement to output the displacement of the node you are interested in:
...
disp,node,0,50
...
The script which puts everything together looks like:
#!/bin/bash
#
#############################################################
# "THE BEER-WARE LICENSE" (Revision 42):
# <robert -at- xenim -dot- de> wrote this file. As long as you
# retain this notice you can do whatever you want
# with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.
#############################################################
#

function setParm() {
  sed -i "s/^\($1=\)[^ ]*/\1$2/" $workfile;
}

INPUT=Igruendung
workfile=Ifound

output=${workfile/I/Out}
result=$INPUT-results

cp $INPUT $workfile

i=0
for nue in 0.1 0.499; do
  res=$result-$nue
  rm $res
  setParm nu $nue
  for n in {1..20}; do
    dx=$[ $n * 3 ]
    dy=$[ $n * 1 ]
    setParm dx $dx
    setParm dy $dy
    echo -n $[ dx*dy*2 ] >> $res
    echo -en "\t" >> $res
    echo -en "Run $i:\tdx=$dx\tdy=$dy\tnue=$nue ... "
    ./feap -i$workfile -o$output
    val=`grep -A1 '^   Node' $output | tail -n 1 | sed 's/.* //'`
    echo $val
    echo -en "$val\t" >> $res
    rm $output
    let i=i+1
    echo >> $res
  done
done
rm $workfile

Posted Mon Jul 13 15:44:10 2009 Tags: bash