LinuxQuestions.org
Register a domain and help support LQ
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 10-09-2007, 07:44 PM   #1
2Pac_Ashoor
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
Bash Script: Not working anymore?!


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

today=$(date +%Y%m%d)
#year=${BIRTH:0:4}
#month=${BIRTH:4:2}
#day=${BIRTH:6:2}

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."

fi
 
Old 10-09-2007, 09:03 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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.
 
Old 10-09-2007, 09:09 PM   #3
2Pac_Ashoor
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 10-09-2007, 09:21 PM   #4
2Pac_Ashoor
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Talking

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.
 
Old 10-09-2007, 09:23 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
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.

I hope that helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Linux Bash Script Not Working?? helptonewbie Programming 12 08-15-2006 12:57 PM
bash script not working :'( aesahaettr Linux - Software 5 05-12-2004 01:08 AM
simple bash script not working. e1000 Programming 4 01-14-2004 12:09 AM
Bash script not working AMMullan Linux - General 2 09-28-2003 07:10 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 12:04 PM


All times are GMT -5. The time now is 04:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration