tags:

views:

153

answers:

4

I have a program that runs and asks users certain questions. I want to automate it so that every question is responded to with No.

+7  A: 
yes no | <command>

Where <command> is the command you want to answer no to.

(or yes n if you actually need to just output an n)

The yes command, by default, outputs a continuous stream of y, in order to answer yes to every prompt. But you can pass in any other string as the argument, in order for it to repeat that to every prompt.

As pointed out by "just somebody", yes isn't actually standardized. While it's available on every system I've ever used (various BSDs, Mac OS X, Linux, Solaris, Cygwin), if you somehow manage to find one in which it doesn't, the following should work:

while true; do echo no; done | <command>

Or as a full-fledged shell script implementation of yes, you can use the following:

#!/bin/sh

if [ $# -ge 1 ]
then
    while true; do echo "$1"; done
else
    while true; do echo y; done
fi
Brian Campbell
+4  A: 

actually, it looks funny ...

$ yes no

manpages excerpt:

$ man yes 

YES(1)                    BSD General Commands Manual                   YES(1)

NAME
     yes -- be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     yes outputs expletive, or, by default, ``y'', forever.

...
The MYYN
sry, edited the wrong answer. edit rolled back.
just somebody
'Expletive', as I understand, is usually a profanity of some sort. I wonder if that was some sort of joke by the authors.
paxdiablo
@paxdiablo: not necessarily: http://www.merriam-webster.com/dictionary/expletive anyway there's tons of "politically incorrect" jokes in unices: the `ttys` file in `/etc` is one of the better known (it's pronounced "titties", as in "boobs").
just somebody
A: 

for systems with no such command, just a simple echo should work

echo "no" | command

for repetitions , not that hard to make a while/for loop that goes on forever.

ghostdog74
+1  A: 

just in case you might be interested in some portability: yes(1) is nonstandard in that it's not described in the Single Unix Specification (POSIX by another name). but it's quite portable anyway (see the HISTORY paragraph; pity The MYYN didn't quote the whole thing):

YES(1)                  FreeBSD General Commands Manual                 YES(1)

NAME
     yes — be repetitively affirmative

SYNOPSIS
     yes [expletive]

DESCRIPTION
     The yes utility outputs expletive, or, by default, “y”, forever.

HISTORY
     The yes command appeared in Version 32V AT&T UNIX.

FreeBSD 9.0                      June 6, 1993                      FreeBSD 9.0

edit

in case you hit an odd system that does not implement this command, it's trivial to provide it yourself. this from FreeBSD-9:

int
main(int argc, char **argv)
{
 if (argc > 1)
  while (puts(argv[1]) != EOF)
   ;
 else
  while (puts("y") != EOF)
   ;
 err(1, "stdout");
 /*NOTREACHED*/
}
just somebody
Weird. You're right, it's not ins SUS. That's unexpected; are there any extant systems that don't include it?
Brian Campbell
no idea, but see edited answer.
just somebody