ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Ok so I was editing a script I had made to check the users birth date but I went wrong somewhere and can't seem to fix the problem. It's not completely finished so I commented out what isn't done. The problem I'm having is that it keeps telling me that the user has entered non numerical values even if u enter 8 numbers. If you enter 8 characters (other than numbers) it gives me this dumb error:
./age.bash: line 21: [: aaaaaaaa: integer expression expected
This happened to me twice today, it's like the script has a mind of its own. It just stopped working, so I deleted a line, re-typed it and vwalla she worked again. Unfortunately it's not working this time, any help would be greatly appreciated thanks.
Here's the script:
#!/bin/bash
echo "Please enter your date of birth in the YYYYMMDD format: "
read BIRTH
varcheck=`echo $BIRTH | grep '[!0-9]'` #checks to see if any non numerical values are entered
if [ ${#BIRTH} != 8 ] ; then #checks to see if 8 characters were entered.
echo "You entered ${#BIRTH} characters, please enter your birthdate in the form YYYYMMDD."
elif [ $varcheck ] ; then #supposed to check if any characters entered were non numerical, this is where it stops.
echo "Your entry contains non numeric characters, please enter your birthdate in the form YYYYMMDD."
#elif [ ] ; then
# echo "You entered $BIRTH, which is an invalid date, please enter your birthdate in the form YYYYMMDD."
elif [ $BIRTH -gt $today ] ; then #checks to see if the date entered is in the future.
echo "You entered $BIRTH, which is a future date, please enter your birthdate in the form YYYYMMDD."
Welcome to LQ. This smells like a homework to me... We are not here to do your homework (please read the forum rules).
However I and many others don't mind pointing you in the right direction... The error message means that you are doing some operation which expects to see an integer operand, but somehow it is not getting one.
A handy feature of bash is the -x option, which will print out what it is executing as it does it. This is after interpolation of variables and so on... You activate this by adding -x to the shabang line of your script, i.e. it should start:
Code:
#!/bin/bash -x
Run the script with that, and you will see some useful output which should help you understand what is going on.
Oh, one last thing, please put code in [CODE] tags. This puts it in a fixed width font and preserves indentation, making it much easier to read.
Last edited by matthewg42; 10-09-2007 at 09:04 PM.
Welcome to LQ. This smells like a homework to me... We are not here to do your homework (please read the forum rules).
However I and many others don't mind pointing you in the right direction... The error message means that you are doing some operation which expects to see an integer operand, but somehow it is not getting one.
A handy feature of bash is the -x option, which will print out what it is executing as it does it. This is after interpolation of variables and so on... You activate this by adding -x to the shabang line of your script, i.e. it should start:
Code:
#!/bin/bash -x
Run the script with that, and you will see some useful output which should help you understand what is going on.
Oh, one last thing, please put code in [CODE] tags. This puts it in a fixed width font and preserves indentation, making it much easier to read.
I appreciate u helpin me with it but I already tried that, thats exactly what my prof told me to do and I still can't figure out why the 'varcheck' variable isn't grabbing non-numeric, its grabbing everything and I dont understand why. If that's the most help you can offer me than I guess I'm on my own, but I still appreciate it. Thanks again.
ok well I figured it out..... I had to remove the hard brackets from the grep command when declaring the 'varcheck' variable.... hehehe, thanks once more.
You can also add the -v option to see what the command looks like before variable substitution and the like, starting the script like:
Code:
#!/bin/bash -vx
When you get the error, you should be able to identify which variable is being expanded to a non-integer value where one is expected...? Do that, and when you know that the variable is called, look back to see where it was set, and try to understand why the value which was assigned to it was not an integer. That is what you need to find out.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.