Problems with valid input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with valid input
# 1  
Old 09-22-2009
Problems with valid input

Hello all

I am having problems with a part of my script. Basically it asks for the user to enter a new id number for them. The catches are:-

* It cannot already be existing (it will check a file)
* It has to be four characters
* It can only be numbers
* It cannot call back into the function

The first three parts are simple but not calling itself is causing me problems. I was thinking about using nested while loops and heres what i have got so far. (It feels like i have lost the plot!!)

Code:
read input

while [ $input < 9999 ] 
do
           while [ 'echo $input | wc -c' < 4 ]
           do
                      while [ "$input" = 'grep $input file' ] 
                      do
                      read input
                      done
           read input
           done
read input
done

Any ideas would save me a lot of stress Smilie
# 2  
Old 09-22-2009
hmm.. I dont think I would go with that many nested loops..

Code:
while true
do
  echo "Enter you number"
  read input
  if [ $input -lt 9999 -a ${#input} -eq 4 -a "$input" != `grep $input $file` ]
  then
    break
  else
    echo "number doesnt meet requirements... these are the requirements: blah"
  fi
done

This examples performs all your checks with one if statement... if it all checks out the loop breaks and $input is good to go for the rest of your script... if not the loop starts over. I didnt test this... so there might be some syntax errors on the test statements.
# 3  
Old 09-22-2009
I am not sure what you mean with "It cannot call back into the function", but perhaps along the lines of this is what you are looking for:

Code:
while read input; do
  if (( input > 999 && input < 10000  )) &&\
     [[ $(grep -c "$input" file) -eq 0 ]]; then
    break
  fi
done

or

Code:
while read input; do
  case $input in
    [0-9][0-9][0-9][0-9])
      if [[ $(grep -c "$input" file) -eq 0 ]]; then
        break
      fi
  esac
done

or

Code:
while read input; do
  case $input in
    [0-9][0-9][0-9][0-9])
      grep "$input" file >/dev/null || break ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

2. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

3. Shell Programming and Scripting

Valid input compared with input

Hi everyone, I cant seem to figure this out for the life of me. Basically, I have two arrays.. sub init { use Getopt::Long; use Data::Dumper; my @selections = (); my @valid_options = ( "vaild1", "valid2" ); GetOptions( "input=s" =>... (2 Replies)
Discussion started by: ckozler
2 Replies

4. UNIX for Dummies Questions & Answers

Testing for valid DC's?

Ok so this is sort of a unix question. I am frequently logging into customers boxes and our product integrates with Active directory. I deal with a great deal of people who have no business being admins but are and they don't know squat about their network or their DC's. So a recurring problem... (4 Replies)
Discussion started by: MrEddy
4 Replies

5. Homework & Coursework Questions

Valid Name

Could someone help me by midnight tonight!!! Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Insert a reference to the Bourne shell as the command... (0 Replies)
Discussion started by: cody007
0 Replies

6. Shell Programming and Scripting

Specified substitution not valid for

Experts, In a script i get the following error: The specified substitution is not valid for this command Do you have any idea what is wrong with it? TITLE="Code Checker" # Script Titel # EXT="_UA99" # Eind van dirnaam # FILE="job.dat" # Zoekbestandsnaam # SEARCH="returncode 0" #... (1 Reply)
Discussion started by: klaasjan
1 Replies

7. Programming

is it valid output ?

#include <iostream> #include<stdio.h> using namespace std; class a { public: int xx; a() { cout << "in CONS a \n"; } ~a() { cout << "in DES a \n"; } }; (1 Reply)
Discussion started by: crackthehit007
1 Replies

8. Shell Programming and Scripting

Expect script with file input problems

Hello, I am trying to write an expect script that will ssh into a large number of Cisco routers and add some commands via the cli. The script I wrote works great for one host however I have over 350 routers that this needs to be added to. The problem is I cannot get the script to read the file of... (1 Reply)
Discussion started by: meberline
1 Replies

9. Shell Programming and Scripting

How to check for a valid numeric input

Hi Folks, I'm using bash script. I would like to check whether input is a number or not.(Only positive numbers).. if space or non numeric is entered, it should say "invalid input". pls help.. thanks in adv. Br/// Vijay. (1 Reply)
Discussion started by: Vijayakumarpc
1 Replies

10. Programming

valid code?

hey , everyone. I have a few questions about pieces of code and was wondering if someone could tell me what exactly they did or if they are even valid. bool1 && bool2 || bool3 <---in what order do these get processed? if (! isdigit(c)) <---What does this do? i = j % 3; <---what does this do?... (4 Replies)
Discussion started by: bebop1111116
4 Replies
Login or Register to Ask a Question