From 1e007d436e7005065391a9c5cce88812deece90b Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Thu, 10 Mar 2022 13:57:40 +0100 Subject: [PATCH] Option to detect newlines at line endings More function documentation --- linefeeder/linefeeder.sh | 41 ++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/linefeeder/linefeeder.sh b/linefeeder/linefeeder.sh index 086eef9..c48085a 100755 --- a/linefeeder/linefeeder.sh +++ b/linefeeder/linefeeder.sh @@ -1,15 +1,39 @@ #!/bin/bash -# define parseline() in the sourcing script +## parseline [-n|-e] +## This function must be defined in the sourcing script +## +## [-n|-e] : If linefeeder is called with -n every line is passed +## to parseline with either -n or -e as first parameter +## -n Line has a newline +## -e Line has no newline + +## linefeeder [OPTION] [FILE...] +## Read input line by line and behaves parameter-wise like cat +## +## [OPTION] +## -n : Provide information to parseline if current line +## has a newline at the end (parseline -n "$line") +## or no newline at the end (parseline -e "$line") +## +## [FILE] +## - : Is treadted as /dev/stdin +## linefeeder() { + local newLine= + for arg in "$@"; do case "$1" in - -) - # treat - as input stream + -) # treat - as input stream break;; - -*) - # Ignore options for now + --) # treat -- as end of parameter + shift + break;; + -n) # Newline detection + newLine="-n" + shift;; + -*) # Ignore other options shift;; esac done @@ -23,10 +47,11 @@ linefeeder() { for file in "${args[@]}"; do [ "$file" == "-" ] && file="/dev/stdin" - while IFS='' read -r line; do - parseline "$line" + newLine=${newLine/"-e"/"-n"} + while IFS= read -r line; do + parseline $newLine "$line" done <"${file}" # If last line in a file has no newline, read ends but still populates $line - [[ $line ]] && parseline "$line" + [ -n "$line" ] && parseline ${newLine/"-n"/"-e"} "$line" done }