Skip to content

Instantly share code, notes, and snippets.

@mjf
Created July 21, 2011 11:50
Show Gist options
  • Save mjf/1097036 to your computer and use it in GitHub Desktop.
Save mjf/1097036 to your computer and use it in GitHub Desktop.
Posix shell script to connect two shell commands by bi-directional pipe
#! /bin/sh
# Posix shell script to connect two shell commands by bi-directional pipe
# Copyright (C) 2011 Matous J. Fialka, <http://mjf.cz/>
# Released under the terms of The MIT License
if [ $# -ne 2 ]
then
printf -- 'Usage: bip COMMAND COMMAND\n'
exit 0
fi
if ! stdout=`mktemp -u /tmp/XXXXXXXX.1.fifo 2>/dev/null`
then
printf -- 'Could not get temporary file name for STDOUT FIFO\n' >&2
exit 1
fi
if ! stderr=`mktemp -u /tmp/XXXXXXXX.2.fifo 2>/dev/null`
then
printf -- 'Could not get temporary file name for STDOUT FIFO\n' >&2
exit 1
fi
interrupt()
{
if ! rm "$stdout" 2>/dev/null
then
printf -- 'Could not remove STDOUT FIFO %s\n' "$stdout" >&2
exit 1
fi
if ! rm "$stderr" 2>/dev/null
then
printf -- 'Could not remove STDERR FIFO %s\n' "$stderr" >&2
exit 1
fi
}
trap interrupt RETURN SIGINT
if ! mkfifo "$stdout" 2>/dev/null
then
printf -- 'Could not create STDOUT FIFO %s\n' "$stdout" >&2
exit 1
fi
if ! mkfifo "$stderr" 2>/dev/null
then
printf -- 'Could not create STDERR FIFO %s\n' "$stderr" >&2
exit 1
fi
$2 < "$stdout" 2< "$stderr" | $1 > "$stdout" 2> "$stderr" &
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment