[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

SOLVED save time output to a script variable (was RE: unexpected script output)



> Thanks Dave!
>
> PS, I'm getting ready to post a request for help in capturing the output of time
> to a script variable. I can't seem to make it work despite many different
> attempts. I don't think that's a dash issue, but if you know that it is, let me
> know.

Well, that didn't take long. I'm posting a simple script that captures the
system time output of the time command to a variable in case anyone else
has spent a good bit of time trying to get this to work, like me. This works
even if /bin/sh is linked to /bin/dash, so my assumption appears to have
been correct.

$cat gettime.sh 
#!/bin/sh

##################################################
# Script CONSTANTS:                              #
##################################################
BLOCKSIZE=10000
INFILE="/dev/urandom"
OUTFILE="/tmp/random.$RANDOM"
TIME_CMD="/usr/bin/time"
TIME_FORMAT="%S"

##################################################
# Exit status codes:                             #
##################################################
E_BAD_CLEANUP=65
SUCCESS=0

##################################################
# Script Variables:                              #
##################################################
blockcount=1000

totaltime=$(/usr/bin/time -f $TIME_FORMAT dd if=$INFILE of=$OUTFILE bs=$BLOCKSIZE \
   count=$blockcount status=noxfer 2>&1 | sed -e "1,2d")

echo "Total time: $totaltime"

# CLEAN-UP:
if [ -e $OUTFILE ]
then
   rm -f $OUTFILE || exit $E_BAD_CLEANUP
fi

exit $SUCCESS

Script output:
$ sh gettime.sh
Total time: 0.74

PS The reason I started the original post was because I noticed
OUTFILE was being defined as random. instead of random.####
so I wanted to track that down first.
 		 	   		  

Reply to: