3

Let's say I have ls | xargs -n1 -p rm, how do I use yes or yes n to automatically answer the questions generated by the -p flag?

I tried yes n | (ls | xargs -n1 -p rm) but didn't work.

P.S. I tried to add the yes tag, but didn't have enough rep.

UPDATE: The question is not really about rm, it's about how to use yes properly. I have an alias or a function that uses xargs -p and I like the fact that it asks me and shows me what it's doing before doing it. When I know what it will do, I would like to be able to use yes to automatically go through all of the xargs -p in the function. So even though the example uses rm, it's not really about it.

Also just to be extra clear, I don't want to modify my alias or function to use or not use -p. I rather just input yes externally.

Tbh I thought that something like yes | some_function_asking_me_questions or some_function_asking_me_questions <( yes ) would have worked, but it didn't.

2nd EDIT: Another example: I have an alias to list AWS SNS topics in a region like:

alias delete_snstopics="list_sns | cut -f 2 | xargs -n1 -p aws sns delete-topic --topic-arn "

Then I have a function that for each region in AWS finds and prompts for deletion for those SNS topics. I want to see the aws sns delete-topic --topic-arn $1 that the xargs would run, because the id of the SNS topic is different every time and if something goes wrong I can match up the SNS id in the web console. Moreover at times I might not want to delete the SNS topic in a particular region. And that's why I want to use yes with this function, so that I can use the same function for partial deletion and full deletion, and still get useful output. Makes sense?

  • It looks like you're trying to remove a single file. Why doesn't rm $filename work? – jayhendren Aug 24 '15 at 19:12
  • 3
    either answer the xargs prompts or don't use -p? – Jeff Schaller Aug 24 '15 at 19:47
  • I updated the question to make clear that this is not about rm, but rather about how to use yes with xargs -p. – Gabriel Aug 26 '15 at 6:36
1

You can use:

ls | xargs -n1 rm -r

And this will work for directories or/and files.

  • Sorry my question apparently wasn't clear. It's not about that specific example, rather about how to use yes with xargs -p. – Gabriel Aug 26 '15 at 6:37
  • Ok got it, Could you make another specific example so I can help you with that? – VaTo Aug 26 '15 at 16:24
  • Updated with more info and the specific example I am working on. I am just surprised that the tool that is meant to answer questions from other cmd tools, doesn't do the job it's supposed to. Almost all examples I saw for yes are for rm specifically, which is rather limiting. – Gabriel Aug 27 '15 at 18:27
1

Let's analyse your command:

yes n | (ls | xargs -n1 -p rm)

The pipeline ls | xargs -n1 -p rm has ls reading standard input, but having standard output redirected to xargs, which takes it as standard input, and has its standard output unaltered.

Now you pipe the output of yes into this pipeline. Since the standard input of that pipeline goes into ls, which doesn't even read it, xargs cannot see any of it.

Now the obvious question is: How can xargs ask you whether to execute the command? After all, its standard input (where programs normally would read user interaction) is redirected to read the output of ls!

Well, the answer is that xargs bypasses the redirection mechanism for those questions by just opening the terminal file /dev/tty directly. Therefore the confirmation input is unaffected by file redirection.

To input to xargs, you need a program that can present the process with a terminal device controlled by the program. A very powerful (but also quite complicated) such program is expect. Unfortunately I don't know a simpler way to achieve what you want.

  • I appreciate the explanation. I am not familiar with expect. How much more effort would it be to to use it and make it behave like yes would? – Gabriel Aug 28 '15 at 20:04
  • would (ls | xargs -n1 -p rm) <( yes ) not work for the same reason? is that equivalent to yes n | (ls | xargs -n1 -p rm)? – Gabriel Aug 28 '15 at 20:06
  • @Gabriel: Unfortunately I'm not familiar with expect either. While answering, I actually tried to create a command with except that would do what you want, but didn't succeed; there's definitely a learning curve for it. – celtschk Aug 28 '15 at 20:43
  • 1
    @Gabriel: (ls | xargs -n1 -p rm) <( yes ) would not work either, but for a different reason: <( yes ) is replaced by the name of a device or pipe the standard output of yes is connected to. But ( … ) does not not take arguments at all. That line simply gives a syntax error. – celtschk Aug 28 '15 at 20:52

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.