LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-09-2015, 05:33 AM   #1
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Rep: Reputation: Disabled
Bash user input valid then continue


I have a script which reads a controller ID and continues. I am trying to add some checks for user input "ctrl".

Valid responses would be whole number 1 thur 5. If value is between 1-5 (single numeric value) then continue with the rest of the script. If any other value then Exit script or continue to ask user input until valid.

I tried a couple of while loops do with esac, but can not seem to get a handle

Code:
read -p "Controller ID#: " ctrl

Any help would be greatly appreciated
 
Old 02-09-2015, 05:49 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,475

Rep: Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424
So there are only 5 possible answers. You can do a simple case to check it or you can do arithmetic check too. But first here is an example: http://www.thegeekstuff.com/2010/07/...ase-statement/
 
Old 02-09-2015, 05:50 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Here it is:

Code:
read -p "Controller ID#: " ctrl
if [ $ctrl -le 5 ]; then
true
else
exit
fi
 
Old 02-09-2015, 06:06 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
This is one way
Code:
#!/bin/bash
#set -xv

while true
do
    read -p "Controller ID#: " ctrl
    match=0
    idx=5
    for i in $(seq $idx)
    do
        if [[ $i -eq $ctrl ]]
        then
            echo match
            match=1
            break
        fi
    done
done

# continue here if 1 - 5
The set cmd will help you debug in future
 
Old 02-09-2015, 10:49 AM   #5
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
WOW thanks all for you suggestions. I will try it out this afternoon and let mark the ticket solved.
 
Old 02-09-2015, 11:24 AM   #6
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
@veerain your suugestion works good, the only problem is when the user enters an invalid entry it errors out. Also since this you used the
Code:
if [ $ctrl -le 5 ]; then
is accepts the 0 value. I would like more of a graceful exit.
Code:
./test.sh: line 89: [: a: integer expression expected
@chrism01: your solution is working, however when the user selects the correct value. it just says "match" and exits. In my script if the ctrl value is valid I would like it to continue the script.

@pan64 thanks for your suggestion however I tried this in the past but do not know how to set up the regex.

Code:
case expression in $ctrl
    *^[1-5]* )
        "Continueing with script ;;
    *[a-Z,0,6-9]* )
        "Please Enter a whole number from 1-5 ONLY" ;;

esac
Basically ctrl is a varible I set-up on a more elobrate script.
 
Old 02-09-2015, 11:30 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,475

Rep: Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424
you can do it without regexp:
Code:
case exp in $var
 1) do something;;
 2) do something else;;
 3) and so on;;
 4) ...
 ...
 *) something went wrong ...;;
ecac
(or something similar)

Last edited by pan64; 02-10-2015 at 01:59 AM.
 
Old 02-09-2015, 01:13 PM   #8
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
@pan64

thanks your help I got the case function to correctly.

Code:
# Validate User Input
    case $ctrl in
        1) echo -e ${grnw} ${open}"You have choosen controller 1"${end};;
        2) echo -e ${grnw} ${open}"You have choosen controller 2"${end} else;;
        3) echo -e ${grnw} ${open}"You have choosen controller 3"${end} else;;
        4) echo -e ${grnw} ${open}"You have choosen controller 4"${end} else;; 
        5) echo -e ${grnw} ${open}"You have choosen controller 5"${end} else;;
        *) echo -e ${redw} ${open}"This is not a valid choice, please try again"${end}
        exit
        ;;
    esac
#END Validate
Item marked solved. thanks again for all your input.
 
Old 02-10-2015, 01:59 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,475

Rep: Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424Reputation: 2424
glad to help you.
if you really want to say thanks just press YES
 
1 members found this post helpful.
Old 02-10-2015, 05:56 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
That's why I put "continue here" - that's where your code would continue ....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 03:57 PM
[SOLVED] Bash user input huntaz556 Linux - Newbie 14 10-24-2011 12:13 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 07:40 PM
Bash Y/N user input zcrxsir88 Programming 11 04-16-2008 12:35 PM
User input using a BASH script... causticmtl Programming 5 07-13-2003 10:59 PM


All times are GMT -5. The time now is 07:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration