Add ability to source catmd.sh itself (providing catmd() also as function)

Outsourced line reading function to linefeeder
This commit is contained in:
2022-03-09 00:22:03 +01:00
parent 39b184e40e
commit ebb7a3d10a
2 changed files with 64 additions and 29 deletions

32
linefeeder/linefeeder.sh Executable file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
# define parseline() in the sourcing script
linefeeder() {
for arg in "$@"; do
case "$1" in
-)
# treat - as input stream
break;;
-*)
# Ignore options for now
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"
while IFS='' read -r line; do
parseline "$line"
done <"${file}"
# If last line in a file has no newline, read ends but still populates $line
[[ $line ]] && parseline "$line"
done
}