I would like to pass the data to an interactive command and get the output of the interactive command as input to another command.
For example, I would like to do something like the following:
echo "Zaphod" | hello.sh | goodbye.sh
and have an exit:
BYE HELLO Zaphod
Here is my initial crack in this, but I'm missing something ;-) I would like hello.sh to choose 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: βFrom a list of things,β I suppose I mean my real use case, which takes something from stdout and allows me to select one option and pass it to stdin for something else ... For example:
ls /tmp | select_from_list | xargs cat
will allow me to list the files in / tmp /, interactively select one, and then cat the contents of the file.
So my "select_from_list" script 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
bash zsh
Brad Parks Dec 11 '15 at 15:16 2015-12-11 15:16
source share