Sample Header Ad - 728x90

rm prompt not working when invoked from a piped loop

1 vote
1 answer
101 views
I am trying to remove files from a directory by pasting their names into a while loop that will simply rm each item from the list of files given to it.
$ sponge | while read file; do echo "rm $file"; rm "$file"; done;
background.bundle.js
964.bundle.js
background.bundle.js.LICENSE.txt
...
META-INF/manifest.mf
META-INF/mozilla.sf
META-INF/mozilla.rsa
^D
rm background.bundle.js
rm: remove regular file ‘background.bundle.js’? $
$
(Notice the ^D at the end) As shown, after issuing the ^D (end-of-file), the sponge command sends the entire output to the while loop, and rm is invoked for each file separately. However, what happens is that it gets invoked once for background.bundle.js, asks for confirmation, and without allowing me the opportunity to enter anything, the loop exits prematurely. This leaves the prompt character ($) without the necessary newline that should follow it, indicating an abrupt end to the loop. I am looking to fix this issue without resorting to temporary files. I want to manage file input using exec so that the loop reads from a different input stream, leaving standard input available for rm to read the user’s confirmation. How can I redirect sponge output and read input to a specific file descriptor for this purpose?
Asked by ychaouche (1033 rep)
Nov 14, 2024, 05:56 PM
Last activity: Nov 18, 2024, 11:25 AM