I'd like to pipe data into an interactive command, and have the output of the interactive command be received as input to another command.
For example, I'd like to be able to do something like the following:
echo "Zaphod" | hello.sh | goodbye.sh
and have the output be:
BYE HELLO Zaphod
Here's my initial crack at this, but I'm missing something ;-) I'd actually like the hello.sh to select from a list of things.
hello.sh
echo Please supply your name
read NAME
echo "HELLO $NAME"
goodbye.sh
MSG=$*
if [ -z "$1" ]
then
MSG=$(cat /dev/stdin)
fi
echo "BYE $MSG"
EDIT: By "select from a list of things", I guess i'm implying my real use case, which is taking anything from stdout, and letting me choose one option, and pass it on to the stdin of something else... For example:
ls /tmp | select_from_list | xargs cat
would allow me to list the files in /tmp/, interactively pick one, then cat the contents of the file.
So my "select_from_list" script actually looks like this:
#!/bin/bash
prompt="Please select an option:"
options=( $* )
if [ -z "$1" ]
then
options=$(cat /dev/stdin)
fi
PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
exit
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
break
else
echo "Invalid option. Try another one."
fi
done
echo $opt