0

Background

The yes command is a means to automatically hit yes when dealing with multiple prompts that you know you will hit yes to. An example of yes in action is depicted below from How to Geek:

yes | sudo apt-get install fortune-mod

This will automatically install everything in the package and hit yes to continue at each prompt. This example is excellent for describing an example of piping the output of yes to one command that will present several "y/n" outputs.

Problem

I have a string of commands I want to execute in tandem, and the last one will bring up several "y/n" prompts. Below is the draft version of my command:

yes | cat alistOfDirectories.txt | xargs -I{} cleartool rmname {}/bad_file.txt

Each time rmname is invoked, a "y/n" prompt still pops up and then a whole host of other errors appear that derail the entire process.

Question

How do I properly pipe the output of yes to the last command through several pipes?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
isakbob
  • 1,439
  • 2
  • 17
  • 39
  • Added link to `rmname` man page. It is a clearcase command. All clearcase commands normally need to be invoked by prefacing the command with `cleartool` or `ct`. – isakbob Jul 26 '19 at 15:42
  • 2
    If `rmname` doesn't allow any kind of `--force` option, it's being a bad UNIX citizen (from a perspective of easy interoperability). (`apt-get` **definitely** has such an option, and thus there's no reason to ever use it with `yes`). – Charles Duffy Jul 26 '19 at 15:52
  • ...and indeed, reading the man page you linked to, `rmname` does indeed have a `-f` flag to suppress the prompt. – Charles Duffy Jul 26 '19 at 15:54
  • Also, `yes` doesn't help if the command reads from the terminal directly (not stdin). Unfortunately this doesn't seem to be documented for `rmname`. – melpomene Jul 26 '19 at 15:54
  • @isakbob, ...to be clear, "pipe content directly from the beginning to the end of a pipeline" doesn't even conceptually make sense: Each program has only one stdin, and if you're piping to it, its stdin is the thing directly one stage left on the pipe; performing that redirection is exactly what a pipeline *exists to do*. Which is to say, if you connected the output of `yes` to `xargs`, then `xargs` would no longer have its stdin connected to `cat`, because there's only one stdin descriptor per process, so it can only go to one place. – Charles Duffy Jul 26 '19 at 15:57
  • I'm confused, why not using `apt-get install -y fortune-mod` directly? – Itachi Jul 29 '19 at 13:01
  • @Itachi I'm working on a secure system not connected to the internet. – isakbob Jul 29 '19 at 14:46

1 Answers1

2

This isn't really a question about yes and multiple pipes, but about xargs: your question is how to send something to the standard input of the command that xargs runs.

The answer (thanks to https://stackoverflow.com/a/19963123/978917) is that you can tell xargs to take its input from somewhere other than standard input, and just forward its standard input to the command it runs. That looks like this:

yes | xargs -a alistOfDirectories.txt -I{} cleartool rmname {}/bad_file.txt
ruakh
  • 175,680
  • 26
  • 273
  • 307