Files
hpr_website/www/eps/hpr3413/hpr3413_coproc_test.sh

54 lines
839 B
Bash
Executable File

#!/bin/bash -
#
# Use bash in the coprocess but turn off buffering
#
process='stdbuf -i0 -o0 -e0 bash'
#
# Indexed array of bash commands
#
declare -a com=('date +%F' 'whoami' 'id' 'echo "$BASH_VERSION"'
'printf "Hello\nWorld\n"')
#
# Count commands in the array
#
n="${#com[@]}"
#
# Start the coprocess
#
coproc child { $process; }
#
# Loop though the commands
#
i=0
while [[ $i -lt $n ]]; do
# Show the command
echo "\$ ${com[$i]}"
# Send to coprocess
echo "${com[$i]}" >&"${child[1]}"
# Read a line from the coprocess
read -u "${child[0]}" -r results
# Show the line received
echo "$results"
((i++))
done
#
# Send an EOF to the coprocess (if needed)
#
if [[ -v child_PID ]]; then
echo "-- End --"
exec {child[1]}>&-
# Flush any remaining results
cat <&"${child[0]}"
fi