LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting issue - input is always invalid (https://www.linuxquestions.org/questions/programming-9/bash-scripting-issue-input-is-always-invalid-4175591038/)

VolumetricSteve 10-08-2016 08:43 PM

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.

grail 10-09-2016 01:45 AM

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??

VolumetricSteve 10-09-2016 10:10 AM

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.

goumba 10-09-2016 10:36 AM

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


VolumetricSteve 10-09-2016 11:48 AM

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.

keefaz 10-10-2016 04:20 PM

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



All times are GMT -5. The time now is 05:41 PM.