Monday, July 08, 2013

Memory Mapped Files

Wednesday, July 03, 2013

Shell Scripts

Shell Scripts Links

How to pass arguments to bash script:
https://stackoverflow.com/questions/18568706/check-number-of-arguments-passed-to-a-bash-script?rq=1

String manipulation in bash scripts:
https://gist.github.com/magnetikonline/90d6fe30fc247ef110a1

Parse command line arguments:
https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash


---------------------------------------------------------------------------------------------------

Script to execute a bunch of commands:

#!/bin/bash
#
# Invoke commands stored in a file, say "cmdfile",  by running this script:
#       ./run_script cmdfile
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

if [ "$#" -ne 1 ]; then
    echo "Usage: $0  "
    exit
fi

# Loop through the file
for line in `cat "$1"`; do
    a=(eval "$line" ">& /dev/null")
    "${a[@]}"
    let status=$?
    if [ $status != 0 ]; then
        echo "${a[1]} ================> FAILED"
    fi
done



-------------------------------------------------------------------------------------------------

  #!/bin/bash

#------------------------------------------------------------------------
#
# This script expects a command line argument: the name of a file that
# contains a sequence of jobnames, a pattern to replace and a pattern to replace with.
#
# For instance, the following commands are saved in a file called "jobs":
#   job1_v03i
#   job2_v03i
#   job3_v03i
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

if [ "$#" -lt 3 ]; then
    echo "Usage: $0  [ ]"
    echo "For example:"
    echo "             $0 jobs v03i v04 /path/to/binary/v03i /different/path/to/binary/v04"
    echo "             $0 jobs v03i v03j"
    exit
fi

# Loop through the file
for job in `cat "$1"`; do
    inputjobxml="${job}.jobxml"
    outputjobxml="${inputjobxml//"$2"/$3}"
    tempfile="/tmp/a"
    echo "$inputjobxml"
    echo "$outputjobxml"
    if [ "$#" -eq 5 ]; then
        eval "sed 's+$4+$5+g' $inputjobxml > $tempfile"
    fi
    eval "sed 's/$2/$3/g' $tempfile > $outputjobxml"
    echo "--------"
  done



--------------------------------------------------------------------------------------------------

#!/bin/bash

#------------------------------------------------------------------------
#
# This script expects a command line argument: the name of a file that
# contains a sequence of filenames, a pattern to replace and a pattern to replace with.
#
# For instance, the following commands are saved in a file called "list":
#   filename1.ext
#   filename2.ext
#   filename3.ext
#
#
#------------------------------------------------------------------------

# Set the field separator to a newline
IFS="
"

# Extract versions from command line arguments
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -f)
    fileslist="$2"
    shift # past argument
    shift # past value
    ;;
    -o|--oldver)
    oldver="$2"
    shift # past argument
    shift # past value
    ;;
    -n|--newver)
    newver="$2"
    shift # past argument
    shift # past value
    ;;

    -h|--help)
    echo ""
    echo "Usage: $0 "
    echo ""
    echo "-f {name}                             file containing  filenames"
    echo "-o {ver} | --oldver {ver}             string representing old version"
    echo "-n {ver} | --newver {ver}             string representing new version"
    echo ""
    echo "For example: "
    echo "             $0 -f parfiles --oldver v03i --newver v04"
    echo "             $0 -f parfiles -o v03i -n v03j"
    echo ""
    exit
    ;;

    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

echo "${fileslist}"
echo "${oldver}"
echo "${newver}"

# Loop through the file
for inputfile in `cat "$fileslist"`; do
    outputfile="${inputfile//"$oldver"/$newver}"
    echo "$inputfile"
    echo "$outputfile"
    eval "sed 's/$oldver/$newver/g' $inputfile > $outputfile"
    echo "--------"
done



A similar loop:


# Loop through the file

 for line in `cat "$fileslist"`; do    
    inputfile="$(cut -d' ' -f 1 <<< $line)"
    np="$(awk '{print $2}' <<< $line)"
    outputfile="${inputfile//"$oldver"/$newver}"
    eval "sed 's/$oldver/$newver/g' $inputfile > $outputfile"

    cmd="mpirun -f $hostfile -perhost 1 -np $np $binary $args"
    echo "Running $cmd ... "
    eval $cmd
    let status=$?
    if [ $status != 0 ]; then
        echo "FAILED"
    else
        echo "SUCCESSFUL"
    fi
done


Note: Can also be done using, but this did not execute the "eval $cmd" line each time.

while read line; do
done < $fileslist