Suppose we have two scripts: one that prompts for the input and one that acts as a wrapper for the first one:
prompt:
wrapper:
Now imagine that we want to implement the behaviour for the wrapper that is usually implemented as a -y (always yes) option for programs. Just to keep it simple, let's say we want to supply prompt with "y" input if wrapper is called with one or more arguments. The obvious way to do this would be to employ yes(1) and duplicate the code:
wrapper:
It works, but I don't really like that in this case we have to duplicate the code that invokes prompt. Is there a way to do this without duplication?
Recently I came across some shell script (unfortunately I don't remember what it was and where) where I noticed (I looked through the source code really briefly) that somewhat similar was implemented using file descriptors and io redirections. I.e. finally, the command was executed with a specific file descriptor as an input, and that file descriptor either provided "y" strings or acted exactly as stdin, connected to the terminal. I may be wrong and maybe I totally misunderstood what the code was actually about, but I had an impression that it did that. I thought I would come back and examine the code later, but as I said, I forgot what is was
The question may probably be very simple, but I'm really confused for whatever reason.
I'm looking for POSIX shell solutions only.
prompt:
sh:
#!/bin/sh
printf "Enter: " 1>&2
read input
echo "You entered: ${input}"
wrapper:
sh:
#!/bin/sh
./prompt
Now imagine that we want to implement the behaviour for the wrapper that is usually implemented as a -y (always yes) option for programs. Just to keep it simple, let's say we want to supply prompt with "y" input if wrapper is called with one or more arguments. The obvious way to do this would be to employ yes(1) and duplicate the code:
wrapper:
sh:
#!/bin/sh
if [ ${#} -gt 0 ]; then
yes |./prompt
else
./prompt
fi
It works, but I don't really like that in this case we have to duplicate the code that invokes prompt. Is there a way to do this without duplication?
Recently I came across some shell script (unfortunately I don't remember what it was and where) where I noticed (I looked through the source code really briefly) that somewhat similar was implemented using file descriptors and io redirections. I.e. finally, the command was executed with a specific file descriptor as an input, and that file descriptor either provided "y" strings or acted exactly as stdin, connected to the terminal. I may be wrong and maybe I totally misunderstood what the code was actually about, but I had an impression that it did that. I thought I would come back and examine the code later, but as I said, I forgot what is was
The question may probably be very simple, but I'm really confused for whatever reason.
I'm looking for POSIX shell solutions only.