-
-
Save mjf/1097036 to your computer and use it in GitHub Desktop.
Posix shell script to connect two shell commands by bi-directional pipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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