Skip to content

Instantly share code, notes, and snippets.

@hypersoft
Created February 14, 2013 16:58
Show Gist options
  • Save hypersoft/4954246 to your computer and use it in GitHub Desktop.
Save hypersoft/4954246 to your computer and use it in GitHub Desktop.
Slurp each file to a var (with optional append) or standard output.
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