Bash: flush stdin (standard input)

I have a bash script that I use mostly interactively. However, sometimes I enter some input in a script. After processing stdin in a loop, I copy the file using "-i" (interactive). However, this is never done (in handset mode), because (I think) the standard input has not been reset. To simplify the example:

#!/bin/bash
while read line
do
    echo $line
done
# the next line does not execute 
cp -i afile bfile

Put this in t.sh and run with: ls | ./t.sh

Reading is not performed. I need to clear stdin before reading. How can I do that?

+3
source share
2 answers

. stdin - ls, while, read EOF . - , :

#!/bin/bash
while read line
do
    echo $line
done
# the next line does execute 
read -p "y/n" x < /dev/tty
echo "got $x"
+6

, , (.. read , ls). , script , . , "" , .

- ls script, :

#!/bin/bash

ls | while read line
do
    echo $line
done

read -p "y/n" x
echo "got $x"
0

Source: https://habr.com/ru/post/1750630/


All Articles