Skip to content

Instantly share code, notes, and snippets.

@missinglink
Last active December 30, 2015 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save missinglink/7800267 to your computer and use it in GitHub Desktop.
Save missinglink/7800267 to your computer and use it in GitHub Desktop.
Functional programming with bash experiment.
#!/bin/bash
function stdout {
echo -e "\e[32m$@\e[0m";
}
function stderr {
echo -e "\e[31m$@\e[0m" 1>&2;
}
function stdlog {
echo "$@";
} >> /tmp/foo.log
function :pipe {
if [ $# -ne 0 ]; then
:multi $@;
else
while read -r; do
echo $REPLY;
done
fi
}
function :multi {
while read -r; do
for i in ${@:1}; do
$i $REPLY;
done
done
}
function :eval {
while read -r; do
STDIN=$REPLY;
eval $1;
done
}
function :color {
case "$1" in
'red' ) :eval 'echo -e "\e[31m$STDIN\e[0m"';;
'green' ) :eval 'echo -e "\e[32m$STDIN\e[0m"';;
'yellow' ) :eval 'echo -e "\e[33m$STDIN\e[0m"';;
'blue' ) :eval 'echo -e "\e[34m$STDIN\e[0m"';;
'purple' ) :eval 'echo -e "\e[35m$STDIN\e[0m"';;
'magenta' ) :eval 'echo -e "\e[36m$STDIN\e[0m"';;
'grey' ) :eval 'echo -e "\e[37m$STDIN\e[0m"';;
* ) :eval 'echo "$STDIN"';;
esac
}
@missinglink
Copy link
Author

#!/bin/bash
source $PWD/stream.sh;

# Create some streams
function a { echo "[ STREAM A ] $@"; }
function b { echo "[ STREAM B ] $@"; }

# Pipe all the things in to the other things
ls -la |:color 'blue' |\
  sed "s/$(whoami)/batman/g" |\
    :eval 'echo "LAYER 1: $STDIN"' |:color 'magenta' |\
      :eval 'echo "LAYER 2: $STDIN"' |:color 'green' |\
        :pipe a b |:color 'yellow';

@missinglink
Copy link
Author

# Redirect stdout

       ┌─2> stderr 2>
stdin ─┤                    ┌─2> stderr
       └─1> stdout | stdin ─┤
                            └─1> stdout
# Redirect stderr to stdout

       ┌─2> stderr 2>─┐               ┌─2> stderr
stdin ─┤              ├─2>&1 | stdin ─┤      
       └─1> stdout 1>─┘               └─1> stdout
# Redirect stdout

       ┌─2> stderr 2>
stdin ─┤                    ┌─2> stderr
       └─1> stdout | stdin ─┤
                            └─1> stdout
# Redirect stderr to stdout

       ┌─2> stderr 2>&1 ─╖
stdin ─┤                 ║                    ┌─2> stderr      
       └─1> stdout ──────╨─1> stdout | stdin ─┤
                                              └─1> stdout

@sevko
Copy link

sevko commented Jun 10, 2015

what have you done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment