Skip to content

Instantly share code, notes, and snippets.

@olastor
Last active August 8, 2020 13:55
Show Gist options
  • Save olastor/3cd024defc4fcdc19e38daf9acbe555e to your computer and use it in GitHub Desktop.
Save olastor/3cd024defc4fcdc19e38daf9acbe555e to your computer and use it in GitHub Desktop.
#/bin/bash
# comment
SAVE_DIR="$HOME/.cat-logs-once-store"
mkdir -p "${SAVE_DIR}"
target_file=$1
session_prefix=$2
if [ ! -f "${target_file}" ]; then
echo "ERROR: File not found."
exit
fi
file_last_line="last-line-${target_file//\//_}_${session_prefix}"
# read previous last line
marker_start_read=""
if [ -f "$file_last_line" ]; then
marker_start_read=`cat "$file_last_line"`
fi
# check if file includes previous last line
includes_marker=""
if [ ! -z "$marker_start_read" ]; then
includes_marker=`grep -F "${marker_start_read}" "${target_file}"`
fi
# save last line of file for next execution
marker_start_read_new=`tail -n1 "${target_file}"`
if [ ! -z "${marker_start_read_new}" ]; then
echo "${marker_start_read_new}" > "$file_last_line"
fi
# just cat if no previous last line was found
if [ -z "$includes_marker" ]; then
cat "$target_file"
exit
fi
# echo lines after previous last line otherwise
approached_marker=false
while read line; do
if [[ $approached_marker == false && "${line}" != "${marker_stop}" ]]; then
continue
else
approached_marker=true
fi
echo $line
done < "${target_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment