I have a bash script that I mostly use in interactive mode. However, sometimes I pipe in some input to the script. After processing stdin in a loop, I copy a file using “-i” (interactive). However, this never gets executed (in pipe mode) since (I guess) standard input has not been flushed. To simplify with an example:
#!/bin/bash
while read line
do
echo $line
done
# the next line does not execute
cp -i afile bfile
Place this in t.sh, and execute with:
ls | ./t.sh
The read is not executed.
I need to flush stdin before the read. How could it do this?
This has nothing to do with flushing. Your stdin is the output of ls, you’ve read all of it with your while loop, so
readgets EOF immediately. If you want to read something from the terminal, you can try this: