Files
catmd/linefeeder/linefeeder.sh
2022-03-10 13:57:40 +01:00

58 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
## 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
break;;
--) # treat -- as end of parameter
shift
break;;
-n) # Newline detection
newLine="-n"
shift;;
-*) # Ignore other options
shift;;
esac
done
local file=
local line=
local args=("$@")
# Use standard input if no files are present
[ $# -eq 0 ] && args[0]="/dev/stdin"
for file in "${args[@]}"; do
[ "$file" == "-" ] && file="/dev/stdin"
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
[ -n "$line" ] && parseline ${newLine/"-n"/"-e"} "$line"
done
}