Created
February 14, 2013 16:58
-
-
Save hypersoft/4954246 to your computer and use it in GitHub Desktop.
Slurp each file to a var (with optional append) or standard output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lib.file.slurp.each () | |
{ | |
# slurp each file to optional var or output with optional append to var | |
declare data="REPLY" record='' file=''; | |
declare -i echo=1 append=0; | |
while [[ ${1:0:1} == - ]]; do | |
[[ $1 == -- ]] && { shift; break; }; | |
[[ $1 == -a ]] && { append=1; shift; continue; }; | |
[[ $1 =~ ^(-v|--var)$ ]] && { data=$2; shift 2; continue; }; | |
[[ $1 =~ ^(-n|--no-echo)$ ]] && { shift; echo=0; continue; }; | |
"USAGE: $FUNCNAME [-n] or [-v VARLABEL] FILE ..." || return; | |
done | |
(( append == 1 )) || printf -v $data %s ''; | |
for file; do | |
IFS='' read -rN0 record < "$file"; | |
IFS='' printf -v $data %s "${!data}${record}"; | |
done | |
(( echo == 1 )) && [[ $data == REPLY ]] && { | |
declare IFS='' | |
printf %s "${!data}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment