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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-04-2013, 04:06 PM
|
#1
|
|
Member
Registered: Jan 2011
Posts: 85
Rep:
|
shell script indexing an array
I've got an array CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
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.
|
|
|
|
01-04-2013, 04:23 PM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
It should work, except that arrays indexes are numbered from zero:
Code:
$ CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
$ for INDEX in {0..11}
> do
> echo ${CALENDER[$INDEX]}
> done
31
28
31
30
31
30
31
31
30
31
30
31
$
|
|
|
|
01-04-2013, 05:00 PM
|
#3
|
|
Member
Registered: Jan 2011
Posts: 85
Original Poster
Rep:
|
Getting the right index from an array
This looks like a simple loop although I'M new to bash. In my case the index number stored in FIRSTDIGIT & SECONDDIGIT will be bases on user input.
Quote:
Originally Posted by colucix
It should work, except that arrays indexes are numbered from zero:
Code:
$ CALENDER=(31 28 31 30 31 30 31 31 30 31 30 31)
$ for INDEX in {0..11}
> do
> echo ${CALENDER[$INDEX]}
> done
31
28
31
30
31
30
31
31
30
31
30
31
$
|
|
|
|
|
01-04-2013, 05:16 PM
|
#4
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by Garrett85
In my case the index number stored in FIRSTDIGIT & SECONDDIGIT will be bases on user input.
|
How exactly the two variables are assigned? I mean, what is the difference between the two from the user point of view?
By the way, if the input is a number from 1 to 12, you can subtract 1 from it:
Code:
FIRSTDIGIT=$((FIRSTDIGIT - 1))
or simply
or even
Code:
let FIRSTDIGIT=FIRSTDIGIT-1
Another way is to add a dummy value at the beginning of the array so that index 0 can be safely ignored:
Code:
CALENDER=(99 31 28 31 30 31 30 31 31 30 31 30 31)
|
|
|
|
01-04-2013, 05:47 PM
|
#5
|
|
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,938
|
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.
|
|
|
|
01-04-2013, 07:23 PM
|
#6
|
|
Member
Registered: Jan 2011
Posts: 85
Original Poster
Rep:
|
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)
#############################
### FUNCTION-is_leap_year ###
#############################
is_leap_year()
{
PART1=`expr ${YY} % 4`
PART2=`expr ${YY} % 100`
if [[ 0 -eq ${PART1} ]] && [[ 0 -ne ${PART2} ]] || [[ 0 -eq `expr ${YY} % 400` ]]; then #{
result=true
else
result=false
fi #}
}
####################
### END FUNCTION ###
####################
########################
### FUNCTION-get_day ###
########################
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
|
|
|
|
|
01-05-2013, 08:42 AM
|
#7
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
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.
|
|
|
1 members found this post helpful.
|
01-05-2013, 01:01 PM
|
#8
|
|
Member
Registered: Jan 2011
Posts: 85
Original Poster
Rep:
|
Thanks grail
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.
|
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:10 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|