Option to detect newlines at line endings

More function documentation
This commit is contained in:
2022-03-10 13:57:40 +01:00
parent c073aa13b9
commit 1e007d436e

View File

@@ -1,15 +1,39 @@
#!/bin/bash #!/bin/bash
# define parseline() in the sourcing script ## parseline [-n|-e] <LINE>
## 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() { linefeeder() {
local newLine=
for arg in "$@"; do for arg in "$@"; do
case "$1" in case "$1" in
-) -) # treat - as input stream
# treat - as input stream
break;; break;;
-*) --) # treat -- as end of parameter
# Ignore options for now shift
break;;
-n) # Newline detection
newLine="-n"
shift;;
-*) # Ignore other options
shift;; shift;;
esac esac
done done
@@ -23,10 +47,11 @@ linefeeder() {
for file in "${args[@]}"; do for file in "${args[@]}"; do
[ "$file" == "-" ] && file="/dev/stdin" [ "$file" == "-" ] && file="/dev/stdin"
while IFS='' read -r line; do newLine=${newLine/"-e"/"-n"}
parseline "$line" while IFS= read -r line; do
parseline $newLine "$line"
done <"${file}" done <"${file}"
# If last line in a file has no newline, read ends but still populates $line # 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 done
} }