1

I was doing

echo -n "  - This directory is not empty, purge? (Y/N):   " | tee -a $mylogs
read choice
echo "$choice" | tee -a $mylogs

but I wanted to try to condense it to:

read -p "  - This directory is not empty, purge? (Y/N): " choice | tee -a $mylogs

This prints to stdout but not to $mylogs. Also, nothing is saved in $choice. Is there an explanation as to why this is? Any 1 or 2 line alternatives that

  • print the prompt
  • print user input
  • save the input in $choice

I would like to log only this part of the script and be able to reuse $choice

5
  • 1
    read does not print anything to stdout. You will need to do that yourself with something like the echo statement above.
    – 111---
    Oct 21 '19 at 17:28
  • 2
    Do you want the prompt string to appear in the log, or just the user input? if the former, then remember that the prompt is printed via stderr not stdout Oct 21 '19 at 17:58
  • Ideally, I'd like to get both, but thanks for pointing out the stderr point about read -p. Still working on getting the user input printed and the user input saved to $choice. Oct 21 '19 at 19:11
  • I don't know how to flag a duplicate question, but this is possibly a dupe of stackoverflow.com/questions/33233811/duplicate-stdin-to-stdout "Duplicate stdin to stdout"
    – Rich
    Oct 21 '19 at 21:20
  • ** it should be something like this but I'm getting nothing in $mylogs ... read -p " - This directory is not empty, purge? (Y/N): " choice | tee >(echo "$(</dev/stdin)") |tee -a $mylogs
    – Rich
    Oct 21 '19 at 21:25
1

After a bit of reading around I found this courtesy of @lesmana which may help

Run your script with

script -q -a -f logfile -c 'sh ./yourscriptname'

To hide this logging from the 'user' I just put this one line script as a wrapper for the target script and then invoked the target script from the wrapper.

6
  • Thank for your time, but weird things happened for me. I think it's because my script doesn't support -c. So it would only print the prompt after I typed exit. Oct 21 '19 at 19:33
  • Which shell do you use?
    – bu5hman
    Oct 21 '19 at 19:38
  • My bad, my shell does support the -c option. I had mistakenly put sript inside my "yourscriptname" and then called it with script again, so it was like script inception and was causing weird behavior. Cool stuff, but this code is just a small part, and I'd like to use the $choice further down, and using this script call doesn't allow me to use the variable that is read. Oct 21 '19 at 19:46
  • Strange. My main script echoed $choice after the read -p perfectly fine. I did check before posting
    – bu5hman
    Oct 21 '19 at 20:15
  • 1
    I had rather thought that you wanted your entire script logged and would therefore invoke your entire script through the wrapper, Seems you are after a 'one-shot' affair I will think on some more....If I come up with anything then I will let you know.
    – bu5hman
    Oct 22 '19 at 16:01
1

EDIT: I realized that tee can read from the original stdin if nothing is piped to it, so I thought I'd add another solution in case it helps anyone who stumbles upon this later.

Thanks to @steeldriver for pointing out that read -p prints to stderr not stdout. So closest I've got is:

read -p "  - This directory is not empty, purge? (Y/N): " choice 2> >(tee -a $mylogs)
echo $choice >> $mylogs

Source and explanation for what's going on can be found here. For this specific example, the first line is able to redirect stderr to tee to print the prompt in $mylogs and to stdout.

However, read seems to be doing something to make it impossible to intercept the user input and send it to $mylogs and not just $choice.

The other solution is simpler and shorter, but less user friendly and readable (unless many people know that read isn't the only command to read input so simply):

echo -n "  - This directory is not empty, purge? (Y/N): " | tee -a $mylogs
choice=$(tee -a $mylogs)

So in the second line tee provides a way to give the user input to both $mylogs and $choice, unlike read, but obviously there's no prompt option (-p). So still 2 lines.

Also, you have to press Enter then Ctrl+D to submit. This might be bad for my Y/N situation, but might be nice for multi-line answers since you can press Enter multiple times before pressing Ctrl+D to stop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.