Register
It is currently Fri Apr 25, 2014 3:25 am

While loop issues


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Sat Apr 29, 2006 8:06 am   
User avatar
Hi, I've been working on a script which prompts for input until the user verifies that the value they have inputted is correct. However, it isn't working quite as I have imagined it to do so

This is a snippet of what I have so far which doesn't seem to work:


Code:
# Prompt for Course Code
echo 'Please Type your Code and Press the Enter key'
# Read in Code
read Code

# Confirm this choice
echo "Is this input correct? Please answer yes or no (Y/N) and Press the
Enter key"
read answer
while [ answer != "Y" ] && [ answer != "y" ]
do
echo "Please re-type your Code and press the Enter key"
# Read in Code
read Code
# Confirm this choice again
echo "Is this input correct? Please answer yes or no (Y/N) and Press the
Enter key"
read answer
done

# Move the input collected above into the file results.txt
echo "Code: $Code " >> results.txt



The problem is, even though you put in "Y" or "y" it still asks for re-input.

Also, I think I should have some kind of validaton on it to make sure what is input is sensible.


Help would be much appreciated.


Top
  
 PostPosted: Sat Apr 29, 2006 3:46 pm   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
I would do it like this:
Code:
#!/bin/bash
# Prompt for Course Code
read -p "please enter your code : " code
# Ask for confirmation
read -p "Is this input correct? Please answer yes or no (Y/N) and Press the
Enter key : " answer
# Check if they entered Y/y
while [ $answer != "Y" ] && [ $answer != "y" ]
do
#If not ask for code again
read -p  "Please re-type your Code and press the Enter key" code
# Confirm this choice again
read -p "Is this input correct? Please answer yes or no (Y/N) and Press the
Enter key : " answer
done

# Move the input collected above into the file results.txt
echo "$code " >> results.txt

That works fine.


Top
 Profile WWW  
 PostPosted: Sat Apr 29, 2006 10:49 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
the problem is you are checking against the name of the variable, not the variable's contents...

this is what you have...
Code:
while [ answer != "Y" ] && [ answer != "y" ]


this is what it *should* be
Code:
while [ $answer != "Y" ] && [ $answer != "y" ]



while crudball does bring up a good point about using the -p on the read command to eliminate the extra echo line... it does not solve the problem you were experiencing. [:edit:] it does not pin-point the error [:/edit:]


Last edited by jbsnake on Sun Apr 30, 2006 10:49 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Apr 30, 2006 9:36 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
What I posted worked for me...
Otherwise I wouldnt have posted it.
and jb - you posted that it should be
Code:
while [ $answer != "Y" ] && [ $answer != "y" ]

That is what I posted... :?


Top
 Profile WWW  
 PostPosted: Sun Apr 30, 2006 10:48 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
yes... you did post correct code... i was merely pointing out WHY your code worked but his did NOT.

i'm not big on putting code here for someone to copy and paste without really knowing what it's doing (atleast not when they are trying to learn)

i was just trying to show exactly what he was doing wrong. i could actually do something like what you did too... i could show him how i would do the menu generator, and allow for multiple accepted answers, or i could just say, "here is what you messed up".

for instance... i would say.. "hey try doing the read check like this"
Code:
read -p "Is this input correct? Please answer yes or no (Y/N) and Press the
Enter key : " answer
answer=`echo ${answer:0:1} | tr [:upper:] [:lower:]`
# now Yes YES y Y yEs yeS YeS are all excepted also :)
while [ $answer != "y" ]


but i figure... one step at a time... learn the variable useage... then move onto more :)


Top
 Profile  
 PostPosted: Sun Apr 30, 2006 11:38 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
Ok, makes sense now.


Top
 Profile WWW  
 PostPosted: Sun Apr 30, 2006 12:56 pm   
User avatar
Thank you both for the suggestions and clear explanations. I fully understand where I went wrong so it's not just a case of me pasting it in and not knowing the reasons why the new version works better.

I've added it to all of my input collection statements within my script, since the above was just one and it all works fine.
I also have as well as a Course code, a second type of code: Module Code, a Name input and a Registration Number input.

I think the user could still input incorrect/nonsense data, even if they think it's correct, so perhaps I should work in some character length validation statements for the Name especially to give the whole script a sense of completion.

The codes should be under 20 characters as well as the name and also above 2 characters at least, but the registration number is always 6 characters.

Shell scripting isn't my most used langauge. I usually work with Java, so if anyone is familiar with it, what is the equivilent of the character length method that I could make use of in this script to add validation?


Top
  
 PostPosted: Sun Apr 30, 2006 10:55 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
any variable can be measured in length by the # in the variable...
Code:
bob="hi, i'm an example"
echo ${#bob}

the part you would see on the terminal would be 18 (if i counted right :) )
also... you can trim the variable down quite easy... let's say i just want the word 'hi'
Code:
echo ${bob:0:2}

that is... ${variable name:start position:length}
let's say you just want the word 'example'
Code:
echo ${bob:11:${#bob}-11}

hope that helps you on the current and future variable trials :)


Top
 Profile  
 PostPosted: Mon May 01, 2006 2:09 pm   
User avatar
Thanks I wondered how to find out the lengths, that will certainly help in future.

I wasn't sure how to adapt the character length amount into an error checking while loop and I ended up with this:

Code:
# Check input is below 20 characters
while [ $Code -lt 0${Code:0:0}  ] && [ $Code -gt 20${Code:0:20} ]
do
echo "Code must be between 1-20 characters. Please re-type your Course Code and press the Enter key"
# Read in Code
read Code


And as I though, as I knew it would be wrong, but I thought it was worth a try anyway... it came up with this error:

syntax error at line 8 : `do' unmatched

If anyone's up to it could they please explain what format I can use a character length finder in a while loop as I have tried to above.

Sorry if this is asking too much, but this should be the last thing I need to make my script complete, or as complete as I need it to be at my level of knowledge, I know it could still do with more too it.


Top
  
 PostPosted: Mon May 01, 2006 7:16 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Code:
# Check input is below 20 characters
while [ $Code -lt 0${Code:0:0}  ] && [ $Code -gt 20${Code:0:20} ]
do
echo "Code must be between 1-20 characters. Please re-type your Course Code and press the Enter key"
# Read in Code
read Code


how about...
Code:
# Check input is below 20 characters
while [ ${#Code} -lt 0  ] || [ ${#Code} -gt 20 ]
do
echo "Code must be between 1-20 characters. Please re-type your Course Code and press the Enter key"
# Read in Code
read Code


Top
 Profile  
 PostPosted: Tue May 02, 2006 3:20 am   
User avatar
That works perfectly. I have added and modifyed it to suit different lengths with no problems so I can say I've learned how to do it instead of just copying it and wondering why it does.

Thank you so much for all the help and for teaching me a skill I can use without problems.


Top
  
 PostPosted: Tue May 02, 2006 11:00 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Your quite welcome...
If you haven't already, check out my tutorials here

There are also about 20+ scripts me and a few others wrote for different things (you might find some useful, atleast how to do stuff).


Top
 Profile  
 PostPosted: Wed May 03, 2006 12:30 pm   
User avatar
Thanks, I'll take a look at those.


Top
  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP