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?
sponge? What is the need of it here?rmtorm -fv, but I'm also curious aboutspongein this case.while ...; do xxx; doneloop is being executed in a sub-shell whose stdin is piped from thespongecommand rather than from the keyboard.sponge, the output ofrm,echoand my own pasting would be all intertwined.