Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Automatically generated I/O parser. Methods can be custom defined by delimiter or length. char, chunk and line are popular methods. Demonstration of line method follows.
- The parser tries to keep track of bytes read, and records read, attempting to read source from a list of file parameters or stdin. If an output file is not specified, a temporary file is created, and the data is sourced from that file, otherwise, an -o option specifies a new script resource exhibiting its own behavior. lib.file.read.each bootstraps itself by performing a regular expression match and replace on auto-exported source environment variables. The %ish variable names are special to the function shortcuts for the declared environment.
- $ echo hello | lib.file.read.each line '(( %- )) && echo $LINE || echo -n $LINE;'
- hello
- $ lib.file.read.each line '(( %- )) && echo -E $%_ || echo -En $%_;' -o /dev/stdout;
- #!/bin/bash
- declare -- RS=$'\n';
- declare -- RL="1";
- declare -- LINE="";
- declare -- FILE="";
- declare -i FPOS="0";
- declare -i EOF="0";
- declare -i RC="0";
- builtin trap 'for trap in "${TRAP[@]}"; do eval $trap; done; unset trap' RETURN;
- new.shared.return.trap()
- {
- TRAP[${#TRAP[@]}]="${*}";
- }
- _auto_return_trap() {
- unset -f /dev/stdout new.shared.return.trap _auto_return_trap;
- unset -v TRAP;
- }
- # custom init section for autotrap ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # new.shared.return.trap 'echo user trap'
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- new.shared.return.trap _auto_return_trap;
- /dev/stdout ()
- {
- (( EOF )) && echo -E $LINE || echo -En $LINE;
- }
- for FILE in "${@:-/dev/stdin}"; do
- EOF == 0;
- while (( ! EOF )); do
- IFS='' builtin read -rd"$RS" LINE || EOF=1;
- let FPOS+=${#LINE} RC++;
- [[ "$LINE" == $'\0' ]] && let FPOS++;
- /dev/stdout || { return $? 2>/dev/null || exit $?; }
- done < "$FILE";
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement