LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2016, 08:43 PM   #1
VolumetricSteve
Member
 
Registered: Mar 2011
Posts: 90

Rep: Reputation: 7
Bash scripting issue - input is always invalid


My input is always treated like invalid input with this script and I'm trying to work out why. I've cobbled this together from a ton of scripts I found online and I've had all kinds of mixed results, but this is the closest I've gotten to something that works...and yet...


Code:
#!/bin/bash

echo "INSTALLER"
echo "THIS IS PROVIDED WITHOUT WARRANTY"
echo "ARE YOU SURE YOU WANT TO PROCEED?"
echo "I AGREE TO CONTINUE-QUIT TO EXIT"
options=("I AGREE" "QUIT")
select opt in "${options[@]}"
do
   case $opt in
      "I AGREE")
         echo "CONTINUTING"
         break
         ;;
      "QUIT")
         echo "QUITTING"
         break
         exit;;
      *) echo invalid option
         continue
         ;;
   esac
done
Any advice on what I'm getting out of place here would be greatly appreciated.

Last edited by VolumetricSteve; 10-09-2016 at 09:56 AM.
 
Old 10-09-2016, 01:45 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, please use [code][/code] tags around code to keep formatting.

You have supplied code but not what you are entering to receive the invalid input?
Having run the code on my machine, entering 1 or 2 seems to work just fine and anything else is received as invalid input correctly??
 
1 members found this post helpful.
Old 10-09-2016, 10:10 AM   #3
VolumetricSteve
Member
 
Registered: Mar 2011
Posts: 90

Original Poster
Rep: Reputation: 7
Grail, thank you.

I fixed the formatting, sorry.

I meant to imply that any input I give it gets handled as invalid input. (or it had in previous versions, anyway)

Wow, so...I just completely misunderstood the purpose of select. I thought the way it worked was to get the user to type "I AGREE' instead of a numerical response, I see numerical responses are working now (where they hadn't been across the many previous versions of this script)

If I want to make my case statement hinge on the literal string input "I AGREE" can I still use select or do I need to use something else? Thanks for your help.
 
Old 10-09-2016, 10:36 AM   #4
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Use read and case. read reads stdin to the specified variable.

Code:
read response
case ${response} in
    "I AGREE")
        echo "CONTINUING"
        ;;
    "QUIT")
        echo "QUITTING"
        ;;
esac

Last edited by goumba; 10-09-2016 at 10:43 AM.
 
1 members found this post helpful.
Old 10-09-2016, 11:48 AM   #5
VolumetricSteve
Member
 
Registered: Mar 2011
Posts: 90

Original Poster
Rep: Reputation: 7
Goumba, awesome. I've read (ha) with read that you need to worry about data sanitation, but I guess that's the price for grabbing a string literal from stdin.
 
Old 10-10-2016, 04:20 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
I would just use a typical yes/no type answer like
Code:
ARE YOU SURE YOU WANT TO PROCEED?
1) YES
2) NO
You could add a confirmation question also
Code:
INSTALLATION SELECTED, DO YOU CONFIRM?
1) YES
2) NO
 
1 members found this post helpful.
  


Reply



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
Bash Scripting - Write user input to file tkinsella Linux - Newbie 8 02-09-2013 02:06 AM
[SOLVED] bash scripting - read command and input echo armandino Linux - General 2 01-24-2011 10:24 AM
Bash Scripting: Pipe input to script vs. $1 jhwilliams Linux - Software 3 12-21-2007 10:54 PM
Bash scripting: redirecting input to a command hal8000b Programming 2 12-10-2007 07:29 AM
Bash scripting and user input Woodsman Slackware 13 11-02-2005 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 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
Open Source Consulting | Domain Registration