Option to detect newlines at line endings
More function documentation
This commit is contained in:
@@ -1,15 +1,39 @@
|
||||
#!/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() {
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user