Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -n
- [[ $1 =~ ^(-h|--help|-\?)$ ]] && {
- cat | less <<MANUAL
- NAME
- bashpipe
- SYNOPSIS
- bashpipe [options] IDENTIFIER
- DESCRIPTION
- Creates a pipe with two auto-selected file descriptors and stores,
- information about the pipe into a bash array. The function also,
- creates convenience functions for working with the pipe, and managing
- its lifecycle.
- OPTIONS
- -p print information about a bashpipe identifer
- -E sets function local variable EXTEND
- --print same as -p
- ENVIRONMENT VARIABLES: EXTEND
- ENVIRONMENT VARIABLE EXTEND
- This variable, when set, provides the \`dot prefix\` for all
- convenience functions. When this variable is not set,
- IDENTIFIER is the prefix.
- EXAMPLE
- source bashpipe;
- bashpipe "io"; declare -p io; ls /dev/fd;
- io.write "Hello world"; io.read -u ${io[0]};
- echo $REPLY; io.close;
- declare -p io; ls /dev/fd;
- The above example performs a write to the pipe input, and reads from,
- the same descriptor using the convenience function assigned to the
- output descriptor.
- AUTHOR
- Written by Triston J. Taylor.
- REPORTING BUGS
- Report bashpipe bugs to pc.wiz.tt@gmail.com
- bash-masters home page: <https://www.facebook.com/alt.bash>
- bash-masters pastebin: <http://pastebin.com/u/bash-masters>
- COPYRIGHT
- Copyright © 2012 Triston J. Taylor - bash-masters
- License: GPLV3; <http://gnu.org/licenses/gpl.html>
- MANUAL
- return;
- }
- [[ $BASH_VERSION < 4.2 ]] && {
- echo error: module \`bashpipe\' requires bash 4.2 or newer >&2;
- return 1;
- }
- [[ -d /tmp ]] || {
- echo bashpipe: error: cannot find /tmp directory
- return 1;
- }
- function bashpipe() {
- while [[ ${1:0:1} = - ]]; do
- [[ $1 =~ ^(-p|--print)$ ]] && {
- local in=$2[0] out=$2[1];
- [[ -z ${!in} ]] && {
- echo bashpipe: error: print: invalid identifier >&2;
- return 1;
- }
- echo "/dev/fd/${!in} /dev/fd/${!out}";
- return;
- };
- [[ $1 == '-E' ]] && {
- local EXTEND=$2; shift 2; continue;
- }
- echo bashpipe: error: unidentified option: \`$1\' >&2; return 1;
- done;
- (( $# != 1 )) && {
- echo bashpipe: error: invalid arguments;
- return 1;
- } >&2;
- local ID=$1;
- declare -p $ID 2>&- > /dev/null && {
- local in=$ID[0] out=$ID[1];
- [[ -e /dev/fd/${!in} && -e /dev/fd/${!out} ]] && {
- echo bashpipe: error: \`$ID\' appears to be a valid bashpipe;
- return 1;
- } >&2;
- }
- local -i FD=${FD:-69}; # start from env. / foxbridge callback
- local -i pFDIn=$(
- while [[ -e /dev/fd/$(( ++FD )) ]]; do :; done; echo -n $FD
- );
- local -i pFDOut=$(
- FD=$pFDIn;
- while [[ -e /dev/fd/$(( ++FD )) ]]; do :; done; echo -n $FD
- );
- local bashpipe=/tmp/$$.bashpipe; local -i v=2;
- [[ -e $bashpipe.sh ]] && {
- while [[ -e /tmp/$bashpipe.$v.sh ]]; do (( v++ )); done
- bashpipe+=.$v;
- }
- bashpipe+=.sh;
- local EXTEND=${EXTEND:-$ID};
- local subscript=$(echo "declare -ag $ID[0]=$pFDIn $ID[1]=$pFDOut; # record endpoints
- exec $pFDIn<> <(:) $pFDOut<> <(:); # open endpoints
- function $EXTEND.write() {
- (( \$# > 0 )) && {
- echo \"\$@\" > '/dev/fd/$pFDIn'
- } || cat > '/dev/fd/$pFDIn';
- }
- function $EXTEND.read() {
- read \"\$@\" < /dev/fd/$pFDOut;
- }
- function $EXTEND.close() {
- exec ${pFDIn}>&- ${pFDOut}>&-;
- unset -f $EXTEND.close $EXTEND.read $EXTEND.write; unset $ID; rm '$bashpipe'
- }"
- );
- echo "$subscript" > "$bashpipe";
- source "$bashpipe";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement