I am making a script. i need to automate a portion that looks at all the files in the directory containing the string "HNAZXLCOM" in the name then take the newest file with that string in the name and put the filename into a variable.
Thanks
function latest {
if [[ $FUNCNAME == ${FUNCNAME[1]} ]]; then
unset -v x latest files
printf -v "$@"
elif (($# > 2)); then
printf '%s\n' "Usage: $FUNCNAME <glob> <varname>" 'Error: Takes at most 2 arguments. Glob defaults to *'
return 1
else
if ! shopt -q nullglob; then
typeset -f +t "$FUNCNAME"
trap 'shopt -u nullglob; trap - RETURN' RETURN
shopt -s nullglob
fi
IFS= typeset -a 'files=(${1:-*})'
typeset latest x
for x in "${files[@]}"; do
[[ -d $x || $x -ot $latest ]] || latest=$x
done
${2:+"$FUNCNAME"} "${2:-printf}" -- %s "$latest"
fi
}
latest '*HNAZXLCOM*' myVar