press y to continue, any other key to exit (Shell script) - shell

I've written below code, but it won't work for SPACE,special symbols etc.
I want my script to exit if any key pressed on keyboard apart from Y/y.
It should handle SPACEBAR,special symbols
echo "enter y, any other key to exit "
read input
if [ $input != "Y" -o $input = "y" ]; then
echo "Exiting"
fi
if [ $input == "Y" -o $input == "y" ]; then
echo "Working"
fi

See http://wiki.bash-hackers.org/commands/builtin/read?s[]=read
Normally you always want to use the -r flag. -n 1 tells read to only read 1 character.
asksure() {
echo -n "Are you sure (Y/N)? "
while read -r -n 1 -s answer; do
if [[ $answer = [YyNn] ]]; then
[[ $answer = [Yy] ]] && retval=0
[[ $answer = [Nn] ]] && retval=1
break
fi
done
echo # just a final linefeed, optics...
return $retval
}
### using it
if asksure; then
echo "Okay, performing rm -rf / then, master...."
else
echo "Pfff..."
fi

First, you need to quote the parameter expansions to make sure certain inputs are not removed altogether during word splitting and quote removal.
You are using the wrong operator. No matter what key you type, one of not Y and not y will be true. For example, "Y" != "y". You want to use and instead of or.
if [ "$input" != "Y" ] && [ "$input" != "y" ]; then
The boolean operators -o and -a are no longer recommended, due to ambiguities than can arise from their use. Use separate test commands joined by || and && (respectively) instead.

You need to put $input in quotes, so the statement parses properly. If your response is a space, what the script sees is
if [ != "Y" -o != "y" ]; then
Which it obviously can't parse properly.
Use
if [ "$input" != "Y" -o "$input" = "y" ]; then
and you should be OK.
By the way, you don't actually exit if the condition is met. Add a return after the echo "Exiting" line.

Related

Prompt to continue if no argument passed

I want to prompt to do something or not. And if a specific argument such as "-y" or "--yes" is passed I want to make the script non-interactive (force user answer).
if [ $# = 1 ] && [ "$1" = "-y" ]; then
# my code here
else
read -n 1 -p "¿Install this? [y/N] "
if [[ $REPLY =~ ^([Yy])$ ]]; then
# same code here
fi
fi
If I had to use a function I would like it to be something not to do with the code but with the test as I have a lot of this tests in the script.
function(argument)
{
if [ $# = 1 ] && [ "$1" = "-y" ]; then
return true
else
read -n 1 -p "$argument [y/N] "
if [[ $REPLY =~ ^([Yy])$ ]]; then
return true
fi
fi
}
if function("¿Install this?"); then
# my code here
fi
This function is wrong because it overrides the script's argument with the function call's argument.
install_maybe () {
echo $# $1
if [ $# = 1 ] && [ "$1" = "-y" ]; then
return 0
else
read -n 1 -p "$1 [y/N] "
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
fi
fi
return 1
}
if install_maybe "Install everything?"; then
source "$DOTFILES/install/esential" "-y"
else source "$DOTFILES/install/esential"
fi
Simply define a flag in a variable that indicates the presence / absence of -y, the auto-confirmation option, among the command-line arguments, and pass that flag to your helper function as a 2nd argument:
#!/bin/bash
# Helper function to ensure that the user has confirmed the intent to proceed.
assertConfirmation() {
local promptMsg=$1 autoConfirm=$2
if (( autoConfirm )); then
return
else
read -n 1 -p "$promptMsg [y/N] "
printf '\n' # Output a newline, because none was appended to the user's keypress.
if [[ $REPLY =~ ^([Yy])$ ]]; then
return
fi
fi
# Getting here means: confirmation was not given - abort the script as a whole.
echo "Aborted." >&2 # Note how the message is sent to *stderr*.
exit 2 # Use a dedicated exit code to signal this condition.
}
# ... Code that performs proper parsing of command-line arguments,
# such as with getopts or GNU getopt, omitted for brevity.
# Here, I assume that the option to signal automatic confirmation
# is in $1, if provided.
[[ $1 == '-y' ]] && autoConfirm=1 || autoConfirm=0
# Call the helper function, which only returns if confirmation is
# either implied by the relevant command-line option or, in its absence,
# by the user confirming the intent to proceed interactively.
assertConfirmation "Install everything?" "$autoConfirm"
# Proceed...
echo "Installing..."

How to do character comparison in bash scripts?

Here is my code
#! /bin/bash
read var
if [ $var="Y" -o $var="y" ]
then
echo "YES"
else
echo "NO"
fi
I want to print YES if the user presses y or Y, otherwise I want to print NO. Why doesn't this code work?
Basically, your Condition is wrong. Quote your variables and leave spaces between operators (like shellter wrote). So it should look like:
#! /bin/bash
read var
if [ "$var" = "Y" ] || [ "$var" = "y" ]
then
echo "YES"
else
echo "NO"
fi
Edit: for POSIX ccompatibility
Replaced == with = - see comments
Replaced -o syntax with || syntax - see comments
With Bash, you can also use regular expression in your test with the =~ operator:
read var
[[ "$var" =~ [Yy] ]] && echo "YES" || echo "NO"
Or as Benjamin W. mentionned, simply use character range with the == operator:
read var
[[ "$var" == [Yy] ]] && echo "YES" || echo "NO"
There is minor syntax error in your code.
Correction : There should be a white space between operators and variables
read var
if [ $var = "Y" -o $var = "y" ]
then
echo "YES"
else
echo "NO"
fi
Try the above bash script.
Hope it would work fine.
Happy Coding!
If all you require is a upper/lowercase comparison, use the ,, operator on the variable being compared ( note the ${var,,} ):
#!/bin/bash
read var
if [ ${var,,} = "y" ]
then
echo "YES"
else
echo "NO"
fi
or more succinctly:
#!/bin/bash
read var
[ ${var,,} = 'y' ] && echo 'YES' || echo 'NO'
or the way I might actually do it:
#!/bin/bash
read var
[[ "${var,,}" == 'y' ]] && echo 'YES' || echo 'NO'
Below is the code that I tried.
#! /bin/bash
read -p "Are you Sure?(Y/N) " answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
echo "Do your stuff."
else
echo "Do your other stuff"
fi
Add whitespace around '=' and your code will run fine.
#! /bin/bash
read var
if [ $var = "Y" -o $var = "y" ]
then
echo "YES"
else
echo "NO"
fi
Try this code.
#! /bin/bash
read var
echo -e "YES\nNO\n" | grep -i $var

IF condition in bash

I have just started learning to write bash scripts. This a simplified form of what I want to write.
The problem is despite of the input, it prints only "YES".
#! /usr/bin/bash
read input
if (("$input"== "y" || "$input" == "Y"))
then
echo "YES";
elif (("$input" == "n" || "$input" == "N"))
then
echo "NO";
else
echo "Not a valid input!!"
fi
Use [[ instead of (( like,
if [[ "$input" == "y" || "$input" == "Y" ]]
and also there must be a space exists before == operator.
ie,
input="n"
if [[ "$input" == "y" || "$input" == "Y" ]]
then
echo "YES";
elif [[ "$input" == "n" || "$input" == "N" ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
You could use regular expression also for condition checking purpose.
if [[ "$input" =~ ^[yY]$ ]]
then
echo "YES";
elif [[ "$input" =~ ^[nN]$ ]]
then
echo "NO";
else
echo "Not a valid input!!"
fi
When you automaticly convert the input to lowercase (using typeset), you do not have to bother with the the uppercases.
When you use an elif, always think 10 seconds for another solution. In this case you might want to use a "switch", in shell written as a case-statement:
#!/usr/bin/bash
typeset -l input
read input
case ${input} in
"y") echo "Yes";;
"n") echo "NO";;
*) echo "Not a valid input!!";;
esac

zsh - read user input with default value - empty input not empty?

I wrote a function that asks for user input like this:
function is_confirmed {
read -rs -k 1 ans
if [[ "${ans}" == "n" || "${ans}" == "N" ]]; then
printf "No\n"
return 1
fi
if [[ "${ans}" == "y" || "${ans}" == "Y" ]]; then
printf "Yes\n"
return 0
fi
# here is my actual problem!!! this doesnt work when user input is blank!
if [[ "${ans}" == "" ]]; then
printf "Yes!\n"
return 1
fi
# Output is Damn!
printf "Damn"
return 1
}
works great so far, however, I want to set "yes" as the default answear, so when the user inputs nothing and just presses enter, it should fall back to "yes", so I tried it with || "$ans" == "" but that still falls back to "Damn"
how come? When I echo $ans at the end of the function it is empty...
EDIT 1:
This is what happens:
e_ask "Are you sure you want to install?\nWarning: This may override some files in your home directory."
if is_confirmed; then
echo "Great!"
else
e_error "Aborting..."
fi
here are the functions:
function e_ask {
printf "\n$1\n"
printf "(Y/n): "
}
function e_warn {
printf "Warning: $1\n"
}
function e_error {
printf "Error: $1\n"
exit 1
}
A case is better when you want to consider many different values and a default one
function is_confirmed {
read -rs -k 1 ans
case "${ans}" in
y|Y|$'\n')
printf "Yes\n"
return 0
;;
*) # This is the default
printf "No\n"
return 1
esac
}
Just add default variable in the beginning of function:
function is_confirmed {
ans="y"
echo "Put your choice:"
read $ans
}
If nothing is typed in, default value will remain.
The problem lies in how you wrote your conditions using the [[ builtin.
You need to change your conditions to:
function is_confirmed {
read -rs -k 1 ans
if [[ "${ans}" == "n" ]] || [[ "${ans}" == "N" ]]; then
printf "No\n"
return 1
else if [[ "${ans}" == "y" ]] || [[ "${ans}" == "Y" ]]; then
printf "Yes\n"
return 0
else if [[ -z "${ans}" ]]; then
printf "Empty\n"
else
printf "Damn\n"
fi
return 1
}
To explain, the [[ builtin tests just one condition, but you can chain multiple instances of [[ with && and ||. Your code, in contrast, tried to test 2 conditions inside [[ and used || as it's C/C++ usage.
More details available in help [[ or man bash

How do I prompt a user for confirmation in bash script? [duplicate]

This question already has answers here:
How do I prompt for Yes/No/Cancel input in a Linux shell script?
(37 answers)
Closed 9 years ago.
I want to put a quick "are you sure?" prompt for confirmation at the top of a potentially dangerous bash script, what's the easiest/best way to do this?
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# do dangerous stuff
fi
I incorporated levislevis85's suggestion (thanks!) and added the -n option to read to accept one character without the need to press Enter. You can use one or both of these.
Also, the negated form might look like this:
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell
fi
However, as pointed out by Erich, under some circumstances such as a syntax error caused by the script being run in the wrong shell, the negated form could allow the script to continue to the "dangerous stuff". The failure mode should favor the safest outcome so only the first, non-negated if should be used.
Explanation:
The read command outputs the prompt (-p "prompt") then accepts one character (-n 1) and accepts backslashes literally (-r) (otherwise read would see the backslash as an escape and wait for a second character). The default variable for read to store the result in is $REPLY if you don't supply a name like this: read -p "my prompt" -n 1 -r my_var
The if statement uses a regular expression to check if the character in $REPLY matches (=~) an upper or lower case "Y". The regular expression used here says "a string starting (^) and consisting solely of one of a list of characters in a bracket expression ([Yy]) and ending ($)". The anchors (^ and $) prevent matching longer strings. In this case they help reinforce the one-character limit set in the read command.
The negated form uses the logical "not" operator (!) to match (=~) any character that is not "Y" or "y". An alternative way to express this is less readable and doesn't as clearly express the intent in my opinion in this instance. However, this is what it would look like: if [[ $REPLY =~ ^[^Yy]$ ]]
use case/esac.
read -p "Continue (y/n)?" choice
case "$choice" in
y|Y ) echo "yes";;
n|N ) echo "no";;
* ) echo "invalid";;
esac
advantage:
neater
can use "OR" condition easier
can use character range, eg [yY][eE][sS] to accept word "yes", where any of its characters may be in lowercase or in uppercase.
Try the read shell builtin:
read -p "Continue (y/n)?" CONT
if [ "$CONT" = "y" ]; then
echo "yaaa";
else
echo "booo";
fi
This way you get 'y' 'yes' or 'Enter'
read -r -p "Are you sure? [Y/n]" response
response=${response,,} # tolower
if [[ $response =~ ^(y| ) ]] || [[ -z $response ]]; then
your-action-here
fi
If you are using zsh try this:
read "response?Are you sure ? [Y/n] "
response=${response:l} #tolower
if [[ $response =~ ^(y| ) ]] || [[ -z $response ]]; then
your-action-here
fi
Here's the function I use :
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
And an example using it:
if [[ "no" == $(ask_yes_or_no "Are you sure?") || \
"no" == $(ask_yes_or_no "Are you *really* sure?") ]]
then
echo "Skipped."
exit 0
fi
# Do something really dangerous...
The output is always "yes" or "no"
It's "no" by default
Everything except "y" or "yes" returns "no", so it's pretty safe for a dangerous bash script
And it's case insensitive, "Y", "Yes", or "YES" work as "yes".
I hope you like it,
Cheers!
This what I found elsewhere, is there a better possible version?
read -p "Are you sure you wish to continue?"
if [ "$REPLY" != "yes" ]; then
exit
fi
[[ -f ./${sname} ]] && read -p "File exists. Are you sure? " -n 1
[[ ! $REPLY =~ ^[Yy]$ ]] && exit 1
used this in a function to look for an existing file and prompt before overwriting.
echo are you sure?
read x
if [ "$x" = "yes" ]
then
# do the dangerous stuff
fi
#!/bin/bash
echo Please, enter your name
read NAME
echo "Hi $NAME!"
if [ "x$NAME" = "xyes" ] ; then
# do something
fi
I s a short script to read in bash and echo back results.
qnd: use
read VARNAME
echo $VARNAME
for a one line response without readline support. Then test $VARNAME however you want.

Resources