How do I script a yes response for installing programs?
The 'yes' command will echo 'y' (or whatever you ask it to) indefinitely. Use it as:
yes | command-that-asks-for-input
or, if a capital 'Y' is required:
yes Y | command-that-asks-for-input
If you want to pass 'N' you can still use yes
:
yes N | command-that-asks-for-input
How to answer yes in Bash Script
assume storm asks the question - use a here document - example:
mkdir -p $INSTALLDIR
sudo apt-get install -y git clojure leiningen
git clone git://github.com/maltoe/storm-install.git
./storm-install/storm_install.sh all `hostname` $INSTALLDIR <<-EOF
yes
EOF
The EOF can be any nonsense characters the shell will not interpret.
How do I always answer No to any prompt with a bash script?
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
How do I prompt for Yes/No/Cancel input in a Linux shell script?
The simplest and most widely available method to get user input at a shell prompt is the read
command. The best way to illustrate its use is a simple demonstration:
while true; do
read -p "Do you wish to install this program? " yn
case $yn in
[Yy]* ) make install; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Another method, pointed out by Steven Huwig, is Bash's select
command. Here is the same example using select
:
echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
case $yn in
Yes ) make install; break;;
No ) exit;;
esac
done
With select
you don't need to sanitize the input – it displays the available choices, and you type a number corresponding to your choice. It also loops automatically, so there's no need for a while true
loop to retry if they give invalid input.
Also, Léa Gris demonstrated a way to make the request language agnostic in her answer. Adapting my first example to better serve multiple languages might look like this:
set -- $(locale LC_MESSAGES)
yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4"
while true; do
read -p "Install (${yesword} / ${noword})? " yn
if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi
if [[ "$yn" =~ $noexpr ]]; then exit; fi
echo "Answer ${yesword} / ${noword}."
done
Obviously other communication strings remain untranslated here (Install, Answer) which would need to be addressed in a more fully completed translation, but even a partial translation would be helpful in many cases.
Finally, please check out the excellent answer by F. Hauri.
Have bash script answer interactive prompts
This is not "auto-completion", this is automation. One common tool for these things is called Expect.
You might also get away with just piping input from yes
.
In Bash, how to add Are you sure [Y/n] to any command or alias?
These are more compact and versatile forms of Hamish's answer. They handle any mixture of upper and lower case letters:
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
do_something
;;
*)
do_something_else
;;
esac
Or, for Bash >= version 3.2:
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
do_something
else
do_something_else
fi
Note: If $response
is an empty string, it will give an error. To fix, simply add quotation marks: "$response"
. – Always use double quotes in variables containing strings (e.g.: prefer to use "$@"
instead $@
).
Or, Bash 4.x:
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ "$response" =~ ^(yes|y)$ ]]
...
Edit:
In response to your edit, here's how you'd create and use a confirm
command based on the first version in my answer (it would work similarly with the other two):
confirm() {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
To use this function:
confirm && hg push ssh://..
or
confirm "Would you really like to do a push?" && hg push ssh://..
If statements accepting yes or no in bash script?
There are several problems with your code:
- Failure to put spaces around
[
(which is a command name) and]
(which is a mandatory but functionless argument to]
). - Treating the contents of a variable as a command; use functions instead.
yesdebug () { echo "Will run in debug mode"; }
nodebug () { echo "Will not run in debug mode"; }
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [ "$yesorno" = yes ]; then
yesdebug
elif [ "$yesorno" = no ]; then
nodebug
else
echo "Not a valid answer."
exit 1
fi
Related Topics
Powershell's Equivalent to Linux's: Ls -Al
Transpose Rows into Column in Unix
What Ncurses Frameworks Are Available for Bash
Berkeley Db Mismatch Error While Configuring Ldap
Find The Depth of The Current Path
How to Do a While Loop with a String Redirected into It
Does I2C Driver Need to Be Implemented Just Like Any Other Character Device Driver
Time Waste of Execv() and Fork()
Using "And" in Bash While Loop
Short Command to Find Total Size of Files Matching a Wild Card
Opening Default Text Editor in Bash
How to Access Raspberry Pi Qemu Vm via Network
How Can Beaglebone Black Be Used as Mass Storage Device
Any Good Tools to Solve Integer Programs on Linux