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 01-28-2010, 10:23 AM   #1
spartiati
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
Bash script - Test variable against a range


I am fairly new to bash scripting, and I am trying to test a variable against a number range (1-3).

This is what I have used so far:
The user enters a value, then

while [ "$PROV" != "1" ] && [ "$PROV" != "2" ] && [ "$PROV" != "3" ] && [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do
clear
echo
echo "You did not enter an appropriate choice. Please enter 1, 2, or 3."
echo "1)PROV1"
echo "2)Prov2"
echo "3)Prov3"
echo
echo "Enter Choice:"
read -e PROV
echo
let COUNTER++
done

Is there a cleaner way to do this?
Something like:
while [ "$PROV" != "1-3" ]&& [ $COUNTER -lt 4 ] || [ -z "$PROV" ] && [ $COUNTER -lt 4 ]; do

Thank you.
 
Old 01-28-2010, 10:37 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello and Welcome to LinuxQuestions,

Why not use a case statement? That's a lot easier to read and understand.
Code:
read PROV
case $PROV in
[1-3])
   echo "You entered a correct value"
   exit
;;
*)
   echo "You entered an incorrect value"
   exit
;;
esac
Here's a great Bash guide: Bash Guide for Beginners

Kind regards,

Eric

Last edited by EricTRA; 01-28-2010 at 10:39 AM. Reason: link
 
Old 01-29-2010, 12:26 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Also, you're using the wrong comparison operator http://tldp.org/LDP/abs/html/comparison-ops.html
 
Old 01-29-2010, 01:35 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Following EricTRA's suggestion, here's my version:
Code:
#!/bin/bash

for (( COUNTER=0;; )); do
    read PROV

    if [[ $PROV == [1-3] ]]; then
        echo "You entered a correct value."
        break
    fi

    (( ++COUNTER ))

    if [[ COUNTER -eq 3 ]]; then
        echo "You entered too many incorrect values."
        break
    else
        echo "You entered an incorrect value."
    fi
done
 
  


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
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
bash script: use the directory of the script file as variable? phling Linux - Newbie 12 01-16-2010 07:16 PM
Problem with bash script - variable name within variable name steven.c.banks Linux - Newbie 3 03-10-2009 03:08 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
BASH : how to act based on test against a range of numbers hollywoodb Programming 4 07-06-2006 06:09 PM

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

All times are GMT -5. The time now is 11:40 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