Force Input in User Defined Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Force Input in User Defined Variable
# 1  
Old 06-19-2007
Force Input in User Defined Variable

In a line such as:

Code:
echo -n "How many days back would you like to check? "; read days

How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over?

I think I figured out my first question while typing. I have:
EDIT: I didnt figure it out at all Smilie
Code:
if [ "$days" != [1-30] ]; then  #this doesnt work
echo "Please specify a number between 1 and 30."
echo -n "How many days back would you like to check? "; read days
else
...
fi

If they enter it wrong again, it errors. Is there a better way to do this?

Last edited by earnstaf; 06-19-2007 at 03:24 PM..
# 2  
Old 06-19-2007
It should be something like:

Code:
echo -n "How many days back would you like to check? "; read days

while [ "$days" -lt 1 -o "$days" -gt 30 ]
do
  echo "Please specify a number between 1 and 30."
  echo -n "How many days back would you like to check? "; read days
done

Regards
# 3  
Old 06-19-2007
Quote:
Originally Posted by Franklin52
It should be something like:

Code:
echo -n "How many days back would you like to check? "; read days

while [ "$days" -lt 1 -o "$days" -gt 30 ]
do
  echo "Please specify a number between 1 and 30."
  echo -n "How many days back would you like to check? "; read days
done

Regards
EDIT: Just reread your advice. Sounds good... I'll give it a try.

EDIT2: Works well Franklin. Thanks for the input... I guess I need to get out of the mindset of only using if as the test construct?

Last edited by earnstaf; 06-19-2007 at 04:52 PM..
# 4  
Old 06-19-2007
Earnstaf,
You already have a solution for your problem with the loop offered
by Franklin.
The loop assures you that only numbers between 1 and 30 are entered.
# 5  
Old 06-19-2007
Also, you could test for the condition you actually want...
while [ ${days} -gt 1 ]; do
# 6  
Old 06-19-2007
To assure the input isn't an empty string replace the while statement with:

Code:
while [ "$days" == "" ] || [ "$days" -lt 1 -o "$days" -gt 30 ]

BTW The use of the testoperator -o fails with:

Code:
while [ "$days" == "" ] -o [ "$days" -lt 1 -o "$days" -gt 30 ]

Does somebody have an explanation of that?
I'm home now and using Bash version 3.1.17, does this fail in ksh too?

Regards
# 7  
Old 06-19-2007
Quote:
Originally Posted by earnstaf
In a line such as:

Code:
echo -n "How many days back would you like to check? "; read days

How can I ensure that the user has a.) entered a number between 1-30 (not 0 or 31+) and b.) has not just hit enter (ie set it to "") and if it's entered wrong, how do I start the if statement over?

I think I figured out my first question while typing. I have:
EDIT: I didnt figure it out at all Smilie
Code:
if [ "$days" != [1-30] ]; then  #this doesnt work
echo "Please specify a number between 1 and 30."
echo -n "How many days back would you like to check? "; read days
else
...
fi

If they enter it wrong again, it errors. Is there a better way to do this?

Code:
while :
do
  printf "Enter day of month (1 - 30): "
  read day
  case $day in
    *[^0-9]* | "" | ???* )
       printf "Invalid number: \"%s\"\nPlease try again.\n\n" "$day"
       continue
       ;;
  esac
  if [ $day -gt 30 -o $day -lt 1 ]
  then
     printf "\nInvalid number: \"%s\"\nPlease try again.\n\n" "$day"
     continue
  fi
  break
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

2. Shell Programming and Scripting

Question about awk - create a user-defined variable

Hi, guys, The content of file is below (from <UNIX® Shells by Example Fourth Edition>): My code is below: gawk -F'' ' { OFS = "****"; $3 = "(904)"; $8 = $5 + $6 + $7; print } ' lab3.data The result is below: So, where is the $1? Why is the variable $8 located at the wired position? (3 Replies)
Discussion started by: franksunnn
3 Replies

3. UNIX for Dummies Questions & Answers

print user variable input

this may be basic thing for everyone here, but i cant push awk to print the variable user input which is INS, please help. code: INS=$1 printf '\n' symdg list | grep $INS-clone | awk -v i=$INS '{ID=substr($4,9,4)}{print "Device Group: "$1,"at Array "ID,i}' output: $ ./test.sh patty ... (5 Replies)
Discussion started by: prodigy06
5 Replies

4. UNIX for Dummies Questions & Answers

user defined commands

Hi, i would like to create user defined commands. e,g: if an user executes , mkdircd test then a directory called test should be created and it should be cd to test. How i can create the command mkdircd with below action: mkdir $1 && cd $1. Please help me in achieving this (7 Replies)
Discussion started by: pandeesh
7 Replies

5. Shell Programming and Scripting

Unable to add user defined variable

Hi, I have a user defined variable _TIME1=xxx I am using awk command for pattern matching. cat $_LOCATION/catalina.txt | awk '/^`$_TIME1`:??:??/' It not taking the value of $_TIME! eg:I am using the command to get all the patter from 12:00:00 to 12:59:59 The user defined variable... (2 Replies)
Discussion started by: ahamed
2 Replies

6. Shell Programming and Scripting

need help with User Defined Function

Dear Friends, I need a help regarding User defined function in shell script. My problem is as follows: my_func.sh my_funcI(){ grep 'mystring' I.dat } my_funcQ(){ grep 'mystring' Q.dat } myfuncI myfuncQ But As both the function has same function only the... (11 Replies)
Discussion started by: user_prady
11 Replies

7. Shell Programming and Scripting

Replace variable with a user defined variable

I have a file that has a list of entries in a column x z z z x y z The column can have any length and any number of any strings. I need to replace each unique string with a user defined number. I can filter the unique entries out using awk '{if (NF==5) print $2}' file | uniq | nl >... (1 Reply)
Discussion started by: ce124
1 Replies

8. Linux

environment variable is not defined

moved to correct thread (0 Replies)
Discussion started by: alien12
0 Replies

9. UNIX for Dummies Questions & Answers

User defined service

I want to add a new IP service which executes a script on SCO OS5. I have amended /etc/services and added to port number (3333) I have amended /etc/inetd.conf and added a line for this service but I can't get it to execute my own shell script When I telnet to the IP address on port 3333 I... (1 Reply)
Discussion started by: markdrury
1 Replies

10. AIX

User defined signal 1

Hi, I am just running a incremental back-up on one of my server. But these days It abrubtly fails with below error. ========== User defined signal 1 =========== When I rerun the back-up, It completed successfully.Earlier this was not happening. Any Idea, what could be the problem... (0 Replies)
Discussion started by: nitesh_raj
0 Replies
Login or Register to Ask a Question