1

I have a command that (potentially) prints several lines and then I want to execute them one by one (using xargs for example, or anything else):

find . -name "*string*" -print | awk '{ print "echo " $1 " >> file.txt" }' | xargs -L 1

Execution of each of the echo ... >> file.txt commands will give a prompt Enter a value: where the user has to explicitly type yes for confirmation or no to cancel further execution of the command.

Is it possible to pipe this into one line to always enter yes for example? Or halt until user enters yes?

So far I'm thinking of 2 possible solutions:

  1. Using expect script.
  2. Capturing stdout of the executed command line by line, and looping over those lines until I reach the prompt, then either waiting for user input or adding yes or no automatically.

Is there an easier way to do this and piping an extra command after xargs? (Let say if I always want to enter yes?). Which of the above solutions would be better. I would assume the second one would be preferable to ensure cross-platform compatibility and avoiding external dependencies like expect.

1

You want to add the pathname of all files whose names contain the string string to a file file.txt, with confirmation for each one.

This is how you do that with find:

find . -name '*string*' -ok printf '%s\n' {} \; >>file.txt

The -ok option will execute the given utility with the found pathname inserted in place of {} if the user replies y (or yes). The find command will wait until a reply is given.

To always reply y, use

yes | find ... >>file.txt

(or change -ok to -exec).

To always reply n, use

yes n | find ... >>file.txt

Note that your original command is unsafe as it creates code which includes filenames as-is. A specially crafted filename could therefore possibly be used to create code to execute something not meant to be executed.

| improve this answer | |
  • Thanks for the input and for the warning :) I'll try it out and see how this can be modified. – SergioLeone Oct 30 '19 at 9:45

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.