I came up with one solution:
highest=-1
current_dir=`pwd`
cd $my_dir
for file in $(ls heat*) ; do #assume I've already checked for dir existence
if [ "${file:4:$(($(expr length $file)-9))}" -gt "$highest" ]; then
highest=${file:4:$(($(expr length $file)-9))}
fi
done
cd $current_dir
....Okay I took your suggestions and edited my solution to scrap the expr and pre-save the variable. In 1000 trials, my method (modified) on average was faster that Jon's but slower than GhostDog's, but the standard deviation was relatively large.
My revised script is seen below in my trial, as are Jon and Ghost Dog's solutions...
declare -a timing
for trial in {1..1000}; do
res1=$(date +%s.%N)
highest=-1
current_dir=`pwd`
cd $my_dir
for file in $(ls heat*) ; do
#assume I've already checked for dir existence
file_no=${file:4:${#file}-9}
if [ $file_no -gt $highest ]; then
highest=$file_no
fi
done
res2=$(date +%s.%N)
timing[$trial]=$(echo "scale=9; $res2 - $res1"|bc)
cd $current_dir
done
average=0
#compile net result
for trial in {1..1000}; do
current_entry=${timing[$trial]}
average=$( echo "scale=9; (($average+$current_entry/1000.0))"|bc)
done
std_dev=0
for trial in {1..1000}; do
current_entry=${timing[$trial]}
std_dev=$(echo "scale=9; (($std_dev + ($current_entry-$average)*($current_entry-$average)))"|bc)
done
std_dev=$(echo "scale=9; sqrt (($std_dev/1000))"|bc)
printf "Approach 1 (Jason), AVG Elapsed Time: %.9F\n" $average
printf "STD Deviation: %.9F\n" $std_dev
for trial in {1..1000}; do
res1=$(date +%s.%N)
highest=-1
current_dir=`pwd`
cd $my_dir
max=$(ls heat[1-9]*.conf |
sed 's/heat\([0-9][0-9]*\)\.conf/\1/' |
sort -n |
tail -n 1)
res2=$(date +%s.%N)
timing[$trial]=$(echo "scale=9; $res2 - $res1"|bc)
cd $current_dir
done
average=0
#compile net result
for trial in {1..1000}; do
current_entry=${timing[$trial]}
average=$( echo "scale=9; (($average+$current_entry/1000.0))"|bc)
done
std_dev=0
for trial in {1..1000}; do
current_entry=${timing[$trial]}
#echo "(($std_dev + ($current_entry-$average)*($current_entry-$average))"
std_dev=$(echo "scale=9; (($std_dev + ($current_entry-$average)*($current_entry-$average)))"|bc)
done
std_dev=$(echo "scale=9; sqrt (($std_dev/1000))"|bc)
printf "Approach 2 (Jon), AVG Elapsed Time: %.9F\n" $average
printf "STD Deviation: %.9F\n" $std_dev
for trial in {1..1000}; do
res1=$(date +%s.%N)
highest=-1
current_dir=`pwd`
cd $my_dir
for file in heat*.conf
do
num=${file:4}
num=${file%.conf}
[[ $num -gt $max ]] && max=$num
done
res2=$(date +%s.%N)
timing[$trial]=$(echo "scale=9; $res2 - $res1"|bc)
cd $current_dir
done
average=0
#compile net result
for trial in {1..1000}; do
current_entry=${timing[$trial]}
average=$( echo "scale=9; (($average+$current_entry/1000.0))"|bc)
done
std_dev=0
for trial in {1..1000}; do
current_entry=${timing[$trial]}
#echo "(($std_dev + ($current_entry-$average)*($current_entry-$average))"
std_dev=$(echo "scale=9; (($std_dev + ($current_entry-$average)*($current_entry-$average)))"|bc)
done
std_dev=$(echo "scale=9; sqrt (($std_dev/1000))"|bc)
printf "Approach 3 (GhostDog), AVG Elapsed Time: %.9F\n" $average
printf "STD Deviation: %.9F\n" $std_dev
... the results are:
Approach 1 (Jason), AVG Elapsed Time: 0.041418086
STD Deviation: 0.177111854
Approach 2 (Jon), AVG Elapsed Time: 0.061025972
STD Deviation: 0.212572411
Approach 3 (GhostDog), AVG Elapsed Time: 0.026292145
STD Deviation: 0.145542801
Good job GhostDog!!! And thanks to both you Jon and the commenters for your tips! :)