Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I always answer No to any prompt with a bash script?

Tags:

bash

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.

like image 481
gpow Avatar asked Dec 25 '09 23:12

gpow


2 Answers

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 
like image 174
Brian Campbell Avatar answered Sep 22 '22 02:09

Brian Campbell


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.

...
like image 39
miku Avatar answered Sep 22 '22 02:09

miku