LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-11-2015, 11:58 AM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Rep: Reputation: 46
conditional test against array fail


Ladies & Gents

Thanks again for your patients with me. Your help is truly appreciated.

I have an array problem. I am trying to get away from checking data files to see if they contain a string. That said I have put the data in an array in the script as an array. But I am having trouble figuring out how to make the check against the array. As in "if string_value is in array then; do something" I don't care what the array index number is in this instance, only that one of them has the string_value in it.

This is the code section that I am having issues with.
Code:
CDATA=("Pesach I" "Pesach II" "Pesach VII" "Pesach VIII" "Shavuot" "Rosh Hashanah I" "Rosh Hashanah II" "Yom Kippur" "Sukkot I" "Sukkot II" "Shemini Atzeret")
  # Check if a YOMTOVE, reading set else ware.
  if [[ "${CDATA[*]}" == *"$YOMTOVE"* ]]; then
    echo "Is Yom Tove. The reading are handled by that function."
    return
  fi
It seams that the conditional check is failing and I have no idea why.

I wish I had know about the app called shellcheck http://www.shellcheck.net/ a long time ago. It has really helped me catch a lot of syntax errors. That doesn't make my scripts work any better but at least you all don't have to correct my simple errors anymore.

This is one of the smallest arrays I need to test against. The largest has 75 entries.

Thanks
 
Old 03-11-2015, 01:27 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by rbees View Post
I am trying to get away from checking data files to see if they contain a string.
Why? grep is so easy and so fast. grep is made to do this task. I won't quarrel with your choice of language or your personal coding style... but must urge you to avoid doing easy things the hard way!

Daniel B. Martin
 
1 members found this post helpful.
Old 03-11-2015, 01:41 PM   #3
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks danielbmartin,

I guess I was under the impression that making the check without having to read disk would be faster and better. If that is not the case then I have no problem returning to reading disk to make such checks. Of coarse I am now most of a day into trying to get this figured out and I want to win this round.
 
Old 03-11-2015, 01:58 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
My knowledge of bash is scant so I don't understand your code.

Maybe you will find something of interest here:
http://askubuntu.com/questions/29971...nother-in-bash

Daniel B. Martin
 
Old 03-11-2015, 02:16 PM   #5
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by rbees View Post
Ladies & Gents

Thanks again for your patients with me. Your help is truly appreciated.

I have an array problem. I am trying to get away from checking data files to see if they contain a string. That said I have put the data in an array in the script as an array. But I am having trouble figuring out how to make the check against the array. As in "if string_value is in array then; do something" I don't care what the array index number is in this instance, only that one of them has the string_value in it.

This is the code section that I am having issues with.
Code:
CDATA=("Pesach I" "Pesach II" "Pesach VII" "Pesach VIII" "Shavuot" "Rosh Hashanah I" "Rosh Hashanah II" "Yom Kippur" "Sukkot I" "Sukkot II" "Shemini Atzeret")
  # Check if a YOMTOVE, reading set else ware.
  if [[ "${CDATA[*]}" == *"$YOMTOVE"* ]]; then
    echo "Is Yom Tove. The reading are handled by that function."
    return
  fi
It seams that the conditional check is failing and I have no idea why.

I wish I had know about the app called shellcheck http://www.shellcheck.net/ a long time ago. It has really helped me catch a lot of syntax errors. That doesn't make my scripts work any better but at least you all don't have to correct my simple errors anymore.

This is one of the smallest arrays I need to test against. The largest has 75 entries.

Thanks
What does the data in YOMTOVE look like?
 
Old 03-11-2015, 02:24 PM   #6
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks SoftSprocket,

It contains just a word or two, I don't think ever three. I am getting close. Give me another hour or so and I think I'll have it.

Thanks danielbmartin,

I will look that over when I get a chance.

I still have to build the data files that this code will act on. That may take me a week, so.......

Last edited by rbees; 03-11-2015 at 02:26 PM.
 
Old 03-11-2015, 02:35 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by rbees View Post
It contains just a word or two, I don't think ever three.
Are you looking for a match on any word in YOMTOVE or a match on the entire string?

Daniel B. Martin
 
Old 03-11-2015, 02:50 PM   #8
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
This:
Code:
if [[ "${CDATA[*]}" == *"$YOMTOVE"* ]]
looks to see if a match is possible. As an example:

Code:
$ a=(1 2 3 4); b="1"; if [[ "${a[*]}" == *"$b"* ]]; then echo 'match'; else echo 'no match'; fi
match
$ a=(1 2 3 4); b="1 4"; if [[ "${a[*]}" == *"$b"* ]]; then echo 'match'; else echo 'no match'; fi
no match
$ a=(1 2 3 4); b="1 2"; if [[ "${a[*]}" == *"$b"* ]]; then echo 'match'; else echo 'no match'; fi
match
 
Old 03-11-2015, 02:55 PM   #9
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks,

I just couldn't get it to work for me. I managed to get it working up to needing the data files. What you think?
Code:
#!/bin/bash

STORDIR="${HOME}/bin/shabbat/data"
TMPDIR="${HOME}/bin/shabbat/tmp"
DAY=08
MONTH=03
YEAR=2015
LAT=44
LONG=-85
TZ="$( date +%-:z )"
YOMTOVE=()
#set -x

# Check if a value exists in an array
# @param $1 mixed  Needle  
# @param $2 array  Haystack
# @return  Success (0) if value exists, Failure (1) otherwise
# Usage: in_array "$needle" "${haystack[@]}"
# See: http://fvue.nl/wiki/Bash:_Check_if_array_element_exists
in_array() {
    local hay needle=$1
    shift
    for hay; do
        [[ $hay == $needle ]] && return 0
    done
    return 1
}

# Shabbat Weekly Reading
# needs YOMTOVE DAY MONTH YEAR LONG LAT TZ passed in
function Shabbat_Reading {
  
  # Set up conditionl check arrays. Should be moved out of the local function
  SREADING=("Bereshit" "Noach" "Lech-Lecha" "Vayera" "Chayei Sara" "Toldot" "Vayetzei" "Vayishlach" "Vayeshev" "Miketz" "Vayigash" "Vayechi" "Shemot" "Vaera" "Bo" "Beshalach" "Yitro" "Mishpatim" "Terumah" "Tetzaveh" "Ki Tisa" "Vayakhel" "Pekudei" "Vayikra" "Tzav" "Shmini" "Tazria" "Metzora" "Achrei Mot" "Kedoshim" "Emor" "Behar" "Bechukotai" "Bamidbar" "Nasso" "Beha'alotcha" "Sh'lach" "Korach" "Chukat" "Balak" "Pinchas" "Matot" "Masei" "Devarim" "Vaetchanan" "Eikev" "Re'eh" "Shoftim" "Ki Teitzei" "Ki Tavo" "Nitzavim" "Vayeilech" "Ha'Azinu" "Vezot Habracha" "Vayakhel-Pekudei" "Tazria-Metzora" "Achrei Mot-Kedoshim" "Behar-Bechukotai" "Chukat-Balak" "Matot-Masei" "Nitzavim-Vayeilech" "Tishrei" "Cheshvan" "Kislev" "Tevet" "Sh'vat" "Adar" "Nisan" "Iyyar" "Sivan" "Tammuz" "Av" "Elul" "Adar I" "Adar II")
  CDATA=("Pesach I" "Pesach II" "Pesach VII" "Pesach VIII" "Shavuot" "Rosh Hashanah I" "Rosh Hashanah II" "Yom Kippur" "Sukkot I" "Sukkot II" "Shemini Atzeret")
  
  # test for the correct day
  if [[ $(date -d "$YEAR-$MONTH-$DAY" +%a) != "Sat" ]]; then
    echo "Not Shabbat"
    return
  fi
set -x
  # Check if a YOMTOVE, reading set elseware.
  if in_array "$YOMTOVE" "${CDATA[@]}"; then
    echo "Is Yom Tove. The reading are handled by that function."
    return
  fi
set +x  

  # Load the reading data
  PARSHA="$(hdate -rq "$DAY" "$MONTH" "$YEAR"|awk '{print $2;}'|awk 'NR==3')"
  echo "Looking for $PARSHA"
  if in_array "$PARSHA" "${SREADING[@]}"; then
    echo "Yes $PARSHA is valid"
    READTIME="$(hdate -t -L"$LONG" -l"$LAT" -z"$TZ" "$DAY" "$MONTH" "$YEAR" | awk '/sunrise/{print $2}')"
    READING=$(grep ^"$PARSHA" "$STORDIR"/Readings.csv)
    IFS=, read -a arr <<<"$READING"
    for ALIYAH in "${arr[@]:3:11}";do
      ARRAY=( 95 100 105 110 115 120 125 )
      SPEED=$(shuf -n1 - < <(printf "%s\n" "${ARRAY[@]}"))
      ARRAY2=( m1 m2 m3 m4 m5 m6 m7 f1 f2 f3 f4 f5 f6 f7 )
      SPEAKER=$(shuf -n1 - < <(printf "%s\n" "${ARRAY2[@]}"))
      COMMAND="espeak -s $SPEED -ven-us+$SPEAKER"
    
      # Generate AND append to action file
      echo "$COMMAND -f $STORDIR/$ALIYAH &" >> "$TMPDIR/$PARSHA"
    done 
  fi
  at "$READTIME" "$MONTH/$DAY/$YEAR" -f "$TMPDIR/$PARSHA"
    echo "The Parsha for "$PARSHA""
}

################# 
# # Main Code # #
#################

for (( i = 1; i < 8; i++)); do
    Shabbat_Reading
    read DAY MONTH YEAR < <(date -d "$YEAR-$MONTH-$DAY + 1 day" "+%d %m %Y")
    
done
exit
 
Old 03-11-2015, 03:15 PM   #10
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
I suspect iterating over the array is the only way it will work for you since you're using phrases. i.e.
Code:
a=("one two" "three four"); b="two three"; if [[ "${a[*]}" == *"$b"* ]]; then echo 'match'; else echo 'no match'; fi
will match.

I'm not familiar with that loop syntax - I'll have to check it out.
 
Old 03-11-2015, 04:49 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by rbees View Post
I guess I was under the impression that making the check without having to read disk would be faster and better.
If you read the same file multiple times, the kernel will cache the data in RAM so subsequent reads will be much faster. Regardless, your data is so small that I doubt you could notice the difference in speed. I would stick with grep since it makes the code so much shorter and easier to understand.

If you're really after speed, don't use a shell script.
 
1 members found this post helpful.
  


Reply



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
RAID5 fail to start array after power failure anuragccsu Linux - Software 5 04-29-2010 09:32 AM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 12:20 AM
Fail to test amavisd-new satimis *BSD 0 05-23-2007 11:26 PM
Fedora FC3 integety test fail GlennsPref Fedora 9 11-12-2004 02:01 AM
Yarrow isos fail media test dubya Fedora 3 04-25-2004 09:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration