From 78c0243b8c2c9d4a8a5be3b15da620623ea7eb2d Mon Sep 17 00:00:00 2001 From: Michael Stumpf Date: Tue, 27 Nov 2018 09:54:06 +0100 Subject: [PATCH] merged trap into new script --- bash/ucf_archive_pack | 153 ------------------------------------------ bash/ucftar_pack | 17 ++++- 2 files changed, 15 insertions(+), 155 deletions(-) delete mode 100755 bash/ucf_archive_pack diff --git a/bash/ucf_archive_pack b/bash/ucf_archive_pack deleted file mode 100755 index 8f19b94..0000000 --- a/bash/ucf_archive_pack +++ /dev/null @@ -1,153 +0,0 @@ -#!/bin/bash -display_help() { - (>&2 echo "Usage: $(basename "$0") [-hv] [-f outfile] filename") - (>&2 echo "UCF archive packer") - (>&2 echo) - (>&2 echo " filename path to arbitrary file with correct sequence number") - (>&2 echo " -f, --file output file (default: archive_XXXX.ucf)") - (>&2 echo " -h, --help display this help message") - (>&2 echo " -v, --verbose verbose output") -} - -exit_script() { - #trap - SIGINT SIGTERM # clear the trap - #kill -- -$$ # Sends SIGTERM to child/sub processes - (>&2 echo "SIGINT/SIGTERM received: removing archive") - rm $fout -} - -# Parse command line arguments -if [ $# -eq 0 ]; then - display_help - exit -1 -fi -fout="" -verbose=0 -POSITIONAL=() -while [[ $# -gt 0 ]] -do -key="$1" -case $key in - -h|--help) - display_help - exit 0 - shift # past argument - ;; - -f|--file) - fout="$2" - shift # past argument - shift # past value - ;; - -v|--verbose) - verbose=1 - shift # past argument - ;; - *) # unknown option - POSITIONAL+=("$1") # save it in an array for later - shift # past argument - ;; -esac -done -set -- "${POSITIONAL[@]}" - -# Parse input filename -din=$(dirname $1) -fin=$(basename $1) -seqnum=$(echo $fin | grep -o '_[0-9]\+') -if [ -z "$seqnum" ]; then - (>&2 echo "[Error] No sequence number found in input filename.") - exit 1; -fi - -# Construct output filename if not specified -if [ -z "$fout" ]; then - fout="archive${seqnum}.ucf" -fi - -# Display verbose info -if [ $verbose -eq 1 ]; then - (>&2 echo "Creating archive: $fout") -fi - -# Check input files -fparam="parameters${seqnum}.asc" -fgrid="grid${seqnum}.bin" -fproc="proc${seqnum}.bin" -fpart="particles${seqnum}.bin" -fbuvwp="uvwp" -fbscal="scal" - -# Check if obligatory files are present -if [ ! -s ${din}/${fparam} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${fparam}") - exit 1 -fi -if [ ! -s ${din}/${fgrid} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${fgrid}") - exit 1 -fi -if [ ! -s ${din}/${fproc} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${fproc}") - exit 1 -fi -if [ ! -s ${din}/${fpart} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${fpart}") - exit 1 -fi - -# Check if all velocity fields exist and are not empty -nproc=$(cat ${din}/${fparam} | grep "nprocs" | awk '{print $NF}') -if [ $verbose -eq 1 ]; then - (>&2 echo "Number of processors: $nproc") -fi - -fuvwp=() -for (( ii=0; ii<$nproc; ii++ )); do - ftmp=$(printf uvwp${seqnum}.%05d $ii) - if [ ! -s ${din}/${ftmp} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${ftmp}") - exit 1 - fi - fuvwp+=($ftmp) -done - -# Check if all scalar fields exist (if any) and are not empty -fscal=() -if test -n "$(shopt -s nullglob; echo scal${seqnum}*)"; then - for (( ii=0; ii<$nproc; ii++ )); do - ftmp=$(printf scal${seqnum}.%05d $ii) - if [ ! -s ${din}/${ftmp} ]; then - (>&2 echo "[Error] File not found or empty: ${din}/${ftmp}") - exit 1 - fi - fscal+=($ftmp) - done -fi - -# Verbose info -if [ $verbose -eq 1 ]; then - (>&2 echo "[x] parameters") - (>&2 echo "[x] grid") - (>&2 echo "[x] processor grid") - (>&2 echo "[x] particles") - (>&2 echo "[x] fluid fields") - if [ ${#fscal[@]} -ne 0 ]; then - (>&2 echo "[x] scalar fields") - else - (>&2 echo "[ ] scalar fields") - fi -fi - -# Now tar them (remove seqence number from file names) -if [ $verbose -eq 1 ]; then - flagtar="-v" -fi -trap exit_script SIGINT SIGTERM -tar $flagtar --format ustar --transform="flags=r;s|$seqnum||" --directory=${din} -cf ${fout} ${fparam} ${fgrid} ${fproc} ${fpart} ${fuvwp[@]} ${fscal[@]} -tarexit=$? -# Set exit status accoring to tar -if [ $tarexit -ne 0 ]; then - (>&2 echo "tar failed with exit code $tarexit") - exit 254 -fi -exit 0 diff --git a/bash/ucftar_pack b/bash/ucftar_pack index 6545bac..d907474 100755 --- a/bash/ucftar_pack +++ b/bash/ucftar_pack @@ -8,7 +8,12 @@ display_help() { (>&2 echo " -h, --help display this help message") (>&2 echo " -v, --verbose verbose output") } - +exit_script() { + #trap - SIGINT SIGTERM # clear the trap + #kill -- -$$ # Sends SIGTERM to child/sub processes + (>&2 echo "SIGINT/SIGTERM received: removing archive") + rm $fout +} # Parse command line arguments if [ $# -eq 0 ]; then display_help @@ -131,8 +136,16 @@ if [ $verbose -eq 1 ]; then fi fi -# Now tar them (remove seqence number from file names) +# Now tar them and remove seqence number from file names while doing so if [ $verbose -eq 1 ]; then flagtar="-v" fi +trap exit_script SIGINT SIGTERM tar $flagtar --format ustar --transform="flags=r;s|$seqnum||" --directory=${din} -cf ${fout} ${fparam} ${fgrid} ${fproc} ${fpart} ${fuvwp[@]} ${fscal[@]} +tarexit=$? +# Set exit status accoring to tar +if [ $tarexit -ne 0 ]; then + (>&2 echo "tar failed with exit code $tarexit") + exit 254 +fi +exit 0