Bash to exit on no reponse


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to exit on no reponse
# 1  
Old 08-31-2016
Bash to exit on no reponse

The below bash does not seem to exit if the user response is n N no No. Is the synatax incorrect? Thank you Smilie.

Bash
Code:
read -r -p "Is this correct? [y/N] " response
if [[ $response =~ ^([nN][oO)$ ]]
then
    echo "The files do not match"  && exit ;;
else
.... do something
fi

# 2  
Old 08-31-2016
Hello cmccabe,

Could you please try following.
Code:
read -r -p "Is this correct? [yes/No] " response
if [[ $response =~ ([no]||[NO]||[No]||[nO]) ]]
then
    echo "The files do not match"
        exit
else
        echo ".... do something";
fi

It sees for string entered from users like No,nO,NO,no too.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 08-31-2016
Thank you very much, works great Smilie.
# 4  
Old 08-31-2016
Use a case statement:

Code:
read -r -p "Is this correct? [yes/No] " response
case $response in
    [yY] | [yY][Ee][Ss] )
        do something
        ;;

     [nN] | [n|N][O|o] )
        echo "The files do not match"
        exit
        ;;
esac

# 5  
Old 08-31-2016
Maybe the following would give you an example to work from:
Code:
#!/bin/bash
while [ 1 ]
do
	read -r -p "Is this correct? [y/N] " response
	if [[ $response =~ ^[nN][oO]?$ ]]
	then
		echo 'match'  && exit
	else
		echo 'no match'
	fi
done

The RE ^[nN][oO]?$ matches a one or two character string anchored at the start of the string (^) where the first character is a lowercase or uppercase n ([nN]), followed by zero or one (?) lowercase or uppercase o ([oO]), and anchored to the end of the string ($).
# 6  
Old 08-31-2016
or more easily, declare response as a low-case variable: typeset -l response and you don't have to permute all the possible combinations...
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning an exit code from a bash function

How to return a exit code from a function and use it in conditional? I tried the following but it does not seem to work. tests.sh: if test ./load.sh ; then echo "0" else echo "1" fi load.sh: return 1; from command line: $ ./tests.sh 0 I was expecting it to output "1"... (17 Replies)
Discussion started by: wolfv
17 Replies

2. Red Hat

No reponse to 'service NFS start' command

Hello, I have a newly kickstarted RHEL 6.4 server that I'm trying to set-up as a kickstart server. I have done this before on other machines, but I am encountering some strange behaviour in this one. # service nfs status rpc.svcgssd is stopped rpc.mountd is stopped nfsd is stopped... (0 Replies)
Discussion started by: Chopstyx
0 Replies

3. Ubuntu

Exit user in bash script

I'm writing a bunch of scripts to automatically configure Ubuntu and I want to run the code below to remove the white dots from the login screen: sudo xhost +SI:localuser:lightdm sudo su lightdm -s /bin/bash gsettings set com.canonical.unity-greeter draw-grid false The problem is that... (3 Replies)
Discussion started by: maerlyngb
3 Replies

4. Shell Programming and Scripting

Global exit listener in bash

I have a script with a whole lot of different functions and want to set teh script so that at any point a key or series of keys can be pressed to exit out and into the main menu function. Rather this than endlessly creating, 'Return to main' menus. Something along the lines of Ctrl+q for example... (1 Reply)
Discussion started by: 3therk1ll
1 Replies

5. Shell Programming and Scripting

Bash won't exit as expected

Hi there, following code snippet should output nothing, IMHO. But the result is "THE END". #!/bin/bash if true ; thenexit fi | grep "somesearchstring" echo "THE END"using bash 4.1.9(1) Bug or feature? Hagen (5 Replies)
Discussion started by: montour
5 Replies

6. Shell Programming and Scripting

Bash Shell Script Exit Codes

Here is my daily stupid question: How can I tell a script to only execute if the other scripts exits successfully? So "script A" executes and it executes successfully (0),then "script B" will run or else "script A "executes and it exits unsucessfully (1) then "script B" will read return... (6 Replies)
Discussion started by: metallica1973
6 Replies

7. Shell Programming and Scripting

Bash script wont exit?

Solved Stupidly I didn't put brackets around the , thanks for all the help guys if ps ax | grep Cluster__check.bash | grep -v grep > /dev/null -- fails (if ps ax | grep Cluster__check.bash | grep -v grep > /dev/null) --works (3 Replies)
Discussion started by: danmc
3 Replies

8. Shell Programming and Scripting

Quitting a bash script... any alternatives to exit?

Folks, Below is a basic synopsis of the problem. I have a script that I need to check for some env vars and fail (exit the script) if they are not there. At the same time I need to set some default env vars. To do this I must run the script from the parent shell or source the script. Doing... (3 Replies)
Discussion started by: bashN00b
3 Replies

9. Shell Programming and Scripting

CGI shell script curl reponse problem

Hi, I am running a bash shell script for some simple web server CGI, the script runs as expected from the browser except the following: curl --silent --max-time 10 --output /dev/null --write-out %{http_code} http://server:port/filename This line outputs 404 when i execute the script... (0 Replies)
Discussion started by: Moxy
0 Replies

10. Shell Programming and Scripting

Functions, exit, and kill in bash

Hello Okay, for reasons related to sourcing a script from another script, I've had to put my main loop into a function, and from there I call other functions. My problem then is exiting from deep within the function call stack. I used to simply call exit, and that would accomplish what I... (1 Reply)
Discussion started by: brsett
1 Replies
Login or Register to Ask a Question