This is a separate reply since I didn’t know that you can include shell functions here.
I made this little function read_latest_log() because I just want to “read the latest log file” in a directory full of timestamped log files. I made a helper function separator_line_with_text() to help with the output, basically setting off the file-info portion (just the filename for now) from the file contents.
<span style="color:#323232;"># # separator_line_with_text
</span><span style="color:#323232;"># # Centers text in a separator line
</span><span style="color:#323232;"># #
</span><span style="color:#323232;"># # Usage:
</span><span style="color:#323232;"># # separator_line_with_text «separator_char» «text»
</span><span style="color:#323232;">separator_line_with_text() {
</span><span style="color:#323232;">local separator_char="$1"
</span><span style="color:#323232;">local contents_str="$2"
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Calculate separator_length
</span><span style="color:#323232;">local separator_length=$(( $(tput cols) - 2 - ${#contents_str} ))
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Calculate the width of the left and right parts of the separator line
</span><span style="color:#323232;">local half_line_width=$(( (${separator_length}) / 2 ))
</span><span style="color:#323232;">
</span><span style="color:#323232;"># Construct the separator line using the $separator_char and $contents_str
</span><span style="color:#323232;">for ((i = 0; i « half_line_width; i++))
</span><span style="color:#323232;">do
</span><span style="color:#323232;">echo -n ${separator_char}
</span><span style="color:#323232;">done
</span><span style="color:#323232;">
</span><span style="color:#323232;">echo -n ${contents_str}
</span><span style="color:#323232;">
</span><span style="color:#323232;">for ((i = 0; i < half_line_width; i++))
</span><span style="color:#323232;">do
</span><span style="color:#323232;">echo -n ${separator_char}
</span><span style="color:#323232;">done
</span><span style="color:#323232;">
</span><span style="color:#323232;">echo ""
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="color:#323232;"># # read_latest_log
</span><span style="color:#323232;"># # Reads the latest log file with a timestamp in the filename.
</span><span style="color:#323232;"># #
</span><span style="color:#323232;"># # Usage:
</span><span style="color:#323232;"># # read_latest_log [[«name_filter»] «extension»] «separator» «timestamp_field_number»
</span><span style="color:#323232;">read_latest_log () {
</span><span style="color:#323232;"> # Check if the function has sufficient parameters
</span><span style="color:#323232;"> if [[ $# -lt 2 ]]; then
</span><span style="color:#323232;"> echo "Error: insufficient parameters."
</span><span style="color:#323232;"> echo "Usage: read_latest_log [[«name_filter» = *] [«extension» = log] «separator» «timestamp_field_number»"
</span><span style="color:#323232;"> return 1
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # Supposing only two parameters are provided
</span><span style="color:#323232;"> # «name_filter» parameter is "*"
</span><span style="color:#323232;"> # «extension» parameter is "log"
</span><span style="color:#323232;"> if [[ $# -eq 2 ]]; then
</span><span style="color:#323232;"> local name_filter="*"
</span><span style="color:#323232;"> local extension="log"
</span><span style="color:#323232;"> local separator="$1"
</span><span style="color:#323232;"> local field="$2"
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # Supposing only three parameters are provided,
</span><span style="color:#323232;"> # assume that the «name_filter» parameter is "*"
</span><span style="color:#323232;"> if [[ $# -eq 3 ]]; then
</span><span style="color:#323232;"> local name_filter="*"
</span><span style="color:#323232;"> local extension="$1"
</span><span style="color:#323232;"> local separator="$2"
</span><span style="color:#323232;"> local field="$3"
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # If all parameters are provided, assign them accordingly
</span><span style="color:#323232;"> if [[ $# -eq 4 ]]; then
</span><span style="color:#323232;"> local name_filter="$1"
</span><span style="color:#323232;"> local extension="$2"
</span><span style="color:#323232;"> local separator="$3"
</span><span style="color:#323232;"> local field="$4"
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # Find all log files with the specified extension, sort them based on the separator and field
</span><span style="color:#323232;"> local log_files=$(find . -type f -name "${name_filter}.${extension}" | sort -n -t "${separator}" -k "${field}")
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # If no log files are found, display a message and return
</span><span style="color:#323232;"> if [[ -z "$log_files" ]]; then
</span><span style="color:#323232;"> echo "No log files found."
</span><span style="color:#323232;"> return 0
</span><span style="color:#323232;"> fi
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # Get the latest log file and its full path
</span><span style="color:#323232;"> local latest_log_file=$(echo "$log_files" | tail -1)
</span><span style="color:#323232;"> local full_path=$(realpath "$latest_log_file")
</span><span style="color:#323232;">
</span><span style="color:#323232;"> # Define the strings for the separator line and
</span><span style="color:#323232;"> # calculate the appropriate length of the separator line
</span><span style="color:#323232;"> local contents_str=" Contents "
</span><span style="color:#323232;"> local separator_char="—"
</span><span style="color:#323232;">
</span><span style="color:#323232;"> separator_line_with_text ${separator_char} ""
</span><span style="color:#323232;"> separator_line_with_text " " ${full_path}
</span><span style="color:#323232;"> separator_line_with_text ${separator_char} ${contents_str}
</span><span style="color:#323232;"> cat "$(echo "$log_files" | tail -1)"
</span><span style="color:#323232;">}
</span>
Sorry for all the edits, for some reason anything that looks like an HTML tag gets erased.