I would like to use the following routine in my job submission bash script which expects the allowed walltime in the format [[HH:]MM:]SS
. (Brackets indicating optionality.) For easier readability I want to convert any input value into the fixed format HH:MM:SS
where the value for hours is open ended.
I think it works okay, but I am a bit worried if I have overlooked possible points where a user could make a mistake. This could lead to submitting an invalid jobscript and a lot of computation jobs not starting.
I also ask myself whether or not my code is optimal.
#!/bin/bash
#
# If there is an unrecoverable error: display a message and exit.
#
printExit ()
{
case $1 in
[iI]) echo INFO: "$2" ;;
[wW]) echo WARNING: "$2" ;;
[eE]) echo ERROR: "$2" ; exit 1 ;;
*) echo "$1" ;;
esac
}
#
# Test if a given value is an integer
#
isnumeric ()
{
if ! expr "$1" : '[[:digit:]]*$' > /dev/null; then
printExit E "Value for $2 ($1) is no integer."
fi
}
#
# Test if a given value is a proper duration
#
istime ()
{
local tempTime=$1
# Split time in HH:MM:SS
theTempSeconds=${tempTime##*:}
isnumeric "$theTempSeconds" "seconds"
if [[ ! "$tempTime" == "${tempTime%:*}" ]]; then
tempTime="${tempTime%:*}"
theTempMinutes=${tempTime##*:}
isnumeric "$theTempMinutes" "minutes"
fi
if [[ ! "$tempTime" == "${tempTime%:*}" ]]; then
tempTime="${tempTime%:*}"
theTempHours=${tempTime##*:}
isnumeric "$theTempHours" "hours"
fi
if [[ ! "$tempTime" == "${tempTime%:*}" ]]; then
printExit E "Unrecognised format."
fi
theSeconds=`expr $theTempSeconds % 60`
theTempMinutes=`expr $theTempMinutes + $theTempSeconds / 60`
theMinutes=`expr $theTempMinutes % 60`
theHours=`expr $theTempHours + $theTempMinutes / 60`
printf -v theWalltime "%d:%02d:%02d" $theHours $theMinutes $theSeconds
}
theWalltime="$1"
echo "$theWalltime"
istime $theWalltime
echo "$theWalltime"
exit 0
I use the first two functions quite a lot in my script so I used them for the target function also.