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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have two if statements that Need to get an index from the array above but when I feed a value into the brackets e.g.
${CALENDER[${SECONDDIGIT}]}
or
${CALENDER[${FIRSTDIGIT}]}
it seems to only be getting the first inded of the CALENDER array, 31, even though FIRSTVALUE will have a number from 1-9 in it and SECONDDIGIT will have 10, 11, or 12. Any ideas by I am unable to get the correct index with my FIRST and SECONDDIGIT values? Thanks.
Can you post your code? I suspect that you want to use associative arrays, not numeric indexed ones. Look at the discussion of different array types in pinfo bash for more information.
I main problem I'M having is that the value in the index varibles FIRSTDIGIT & SECONDDIGIT are not getting back the correct index from the CALENDER array. I believe that they are always getting the first index in the array. When I run the script with #! /bin/bash -x I see that the indexing varaible is always being checked agaisnt 31, never 28 or 30. If I add a +1 to the FRISTDIGIT it will then be run again 28 which to me is further proof that it the script doesnt seem to care what the value of the indexing variable is but is always going to the first position in the array.
If I run date 04301998 it fails and I don't know why. Below is my code:
Quote:
#! /bin/bash -x
# This is the calender array for the DateValidation program
CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
get_day()
{
DAY="BAD"
FIRSTDIGIT=${MM:0:1}
SECONDDIGIT=${MM:1:1}
if [[ ${FIRSTDIGIT} -eq 0 ]]; then #{
if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${SECONDDIGIT+1}]} ]]; then #{
DAY="GOOD"
fi #}
else
if [[ ${DD} -gt 0 ]] && [[ ${DD} -le ${CALENDER[${MM}]} ]]; then #{
DAY="GOOD"
fi #}
fi #}
}
####################
### END FUNCTION ###
####################
KEEPGOING=1
while [[ ${KEEPGOING} != "exit" ]]; do
read -p "Enter a date for validation: " DATE
# establish the varible LEN to hold the number of characters in Date, 8 is the only valid number
LEN=$(echo ${#DATE})
if [[ "${DATE} == exit" ]]; then
${KEEPGOING}=0
elif [ $LEN -eq 8 ]; then #{
# set date dariables MM, DD, & YY
MM=${DATE:0:2}
DD=${DATE:2:2}
YY=${DATE:4:4}
if [ ${YY} -gt 0 ]; then #{
if [ ${MM} -gt 0 ] && [ ${MM} -lt 13 ]; then
if [ ${MM} -eq 02 ]; then
is_leap_year ${YY}
if [ $result == true ]; then
if [ ${DD} -gt 0 ] && [ ${DD} -le 29 ]; then
echo "${DATE} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
else
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${Date} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
fi #}
else
get_day ${DD}
if [ ${DAY} == GOOD ]; then
echo "${DATE} is a valid date!"
else
echo "${DD} is invalid for this date!"
fi #}
fi #}
else
echo "${MM} is invalid for this date!"
fi #}
else
echo "${YY} is invalid for this date!"
fi #}
else
echo "Invalid number of digits for a date!"
fi #}
done
Thanks grail. ${CALENDER[SECONDDIGIT-1]} did it rather than my old ${CALENDER[${SECONDDIGIT-1}]} script. That's been driving me mad for days now. Thanks again.
Quote:
Originally Posted by grail
Well there are several other errors with the code, but the error with the array is as follows:
Code:
${CALENDER[${SECONDDIGIT+1}]}
Here you have included the increment inside the variable call and hence it is being set to 0 and using the first value.
You can simplify this to:
Code:
${CALENDER[SECONDDIGIT-1]}
I would also urge you to look at (()) for testing numbers and review your exit process which does not work.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.