LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-25-2016, 07:01 AM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Rep: Reputation: 46
Confused by bash script fail


Ladies & Gents,

I have a script that processes a daily count up to fifty days and generates a text-to-speach file which it then schedules to run at a specific time. Last year this script worked fine.

Did thy make a change to bash that would cause this code to fail?
Code:
if [[ "$CURENTMONTH" == "Nisan" ]] && (( "$CURENTDAY" > 15 ));then
In my debug output it processes the first condition but skips the second.

Shellcheck.net detects no error.

Thanks
 
Old 04-25-2016, 07:13 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,623

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Need More Data.

That line is not a script.
Can you provide more of the script please?
At the very least, the lines where the variables referenced in that command are set.

It makes sense that the second condition will not be evaluated IF th first condition returns false. That is expected behavior, sine the value of the second condition is not NEEDED if the first one is false.

Also, the LANG and TIMEZONE settings may help.

Does your script do any logging so you can check the values of the variables when execution reaches that point? IF not, you may want to add some logging to make troubleshooting easier.

FYI: While it is not invalid, I would never structure the condition quite that way. I use the number of the month, rather than the name, so textual changes in the way the name string is presented will not break the script. That may, or may not apply, depending upon how you are setting the month in the Jewish calendar into the environment.

Also, in the second condition you compare a string to a number. The shell is smart enough that this will generally work as you would expect, but there are more correct techniques. ( This last is not an issue with the current case, if the second is not being evaluated anyway. I just wanted to mention it. )

Last edited by wpeckham; 04-25-2016 at 07:19 AM.
 
Old 04-25-2016, 07:46 AM   #3
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks wpeckham,

The actual code section
Code:
function Count_Omer {
  local hour
  local min
  CURENTDAY=$( hdate -q --not-sunset-aware $DAY $MONTH $YEAR|awk '{print $5;}' )
  CURENTMONTH=$( hdate -q --not-sunset-aware $DAY $MONTH $YEAR|awk '{print $6;}' )
  MORNOMER="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/sunrise/{print $2}')"
  read DAY MONTH YEAR hour min < <(date -d "$YEAR-$MONTH-$DAY $MORNOMER$TZ -5minutes" "+%d %m %Y %H %M")
  if [[ "$CURENTMONTH" == "Nisan" ]] && (( "$CURENTDAY" > 15 ));then
      OMER=$(hdate -oq $DAY $MONTH $YEAR | awk 'NR==3'|awk '{print $4}')
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Morning"
      at "$hour:$min" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Morning
      EVEOMER="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/first_stars/{print $2}')"
      ((OMER ++))
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Evening"
      at "$EVEOMER" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Evening
      unset MORNOMER
      unset EVEOMER
      return
    elif [[ "$CURENTMONTH" == "Iyyar" ]];then
      OMER=$(hdate -oq $DAY $MONTH $YEAR | awk 'NR==3'|awk '{print $4}')
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Morning"
      at "$hour:$min" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Morning
      EVEOMER="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/first_stars/{print $2}')"
      ((OMER ++))
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Evening"
      at "$EVEOMER" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Evening
      unset EVEOMER
      unset MORNOMER
      return
    elif [[ "$CURENTMONTH" == "Sivan" ]] && (( "$CURENTDAY" < 6 ));then
      OMER=$(hdate -oq $DAY $MONTH $YEAR | awk 'NR==3'|awk '{print $4}')
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Morning"
      at "$hour:$min" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Morning
      EVEOMER="$(hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/first_stars/{print $2}')"
      ((OMER ++))
      Omer_Blessing "$EVEOMER" "$OMER" "${OMER}_Omer_Evening"
      at "$EVEOMER" "$MONTH/$DAY/$YEAR" -f "$TMPDIR"/${OMER}_Omer_Evening
      unset EVEOMER
      unset MORNOMER
      return
    fi
}
The actual debug on that part
Code:
+ Count_Omer
+ local hour
+ local min
 hdate -q --not-sunset-aware $DAY $MONTH $YEAR|awk '{print $5;}' 
++ hdate -q --not-sunset-aware 25 04 2016
++ awk '{print $5;}'
+ CURENTDAY='
17'
 hdate -q --not-sunset-aware $DAY $MONTH $YEAR|awk '{print $6;}' 
++ hdate -q --not-sunset-aware 25 04 2016
++ awk '{print $6;}'
+ CURENTMONTH='
Nisan'
hdate -t -L$LONG -l$LAT -z$TZ $DAY $MONTH $YEAR | awk '/sunrise/{print $2}'
++ hdate -t -L(=removed) -l(=removed) -z-4:00 25 04 2016
++ awk '/sunrise/{print $2}'
+ MORNOMER=06:38
+ read DAY MONTH YEAR hour min
date -d "$YEAR-$MONTH-$DAY $MORNOMER$TZ -5minutes" "+%d %m %Y %H %M"
++ date -d '2016-04-25 06:38-4:00 -5minutes' '+%d %m %Y %H %M'
+ [[ 
Nisan == \N\i\s\a\n ]]
 ***** here it never processes the next condition *******

+ [[ 
Nisan == \I\y\y\a\r ]]
+ [[ 
Nisan == \S\i\v\a\n ]]
+ Havdalah
 
Old 04-25-2016, 08:53 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
It looks like both CURRENTMONTH and CURRENTDAY have a leading newline character, and the string "\nNisan" doesn't match "Nisan". Since you're using the numeric conversion of CURRENTDAY, that leading whitespace character probably doesn't matter, but for the string comparison it does. It is not apparent to me where that newline character came from. It must have something to do with your locale settings.
 
Old 04-25-2016, 10:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
We would probably need to see the output from hdate to see why awk is returning an additional newline prior to its value, but based on the presented code, I also see no reason for the additional character.
The locale settings would also be worth a check ... but one would think you know if you changed those?
 
Old 04-25-2016, 10:59 AM   #6
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Untested, but does the following work?
Code:
if (( "$CURENTMONTH" == "Nisan"   &&  "$CURENTDAY" > 15 ));then
 
Old 04-25-2016, 11:59 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@norobro - Your example will fail for multiple reasons, the first being that (()) only tests numeric values so the first test will throw an error and secondly for the reasons mentioned that the leading
newline will still make the text comparison fail (even if it could work)
 
Old 04-25-2016, 12:28 PM   #8
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
As you can tell, I don't do much bash scripting.


Edit:

From the screenshots on the libhdate webpage (http://libhdate.sourceforge.net/?q=node/3) it looks like the line feed is a feature. They pipe output to column to get rid of it but you can test for an empty field in awk:
Code:
awk '$6!="" {print $6}'

Last edited by norobro; 04-25-2016 at 01:52 PM.
 
Old 04-25-2016, 06:41 PM   #9
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
No I have not changes the local settings. This is running on Debian Testing.

The output from hdate.

Code:
:~$ hdate -q --not-sunset-aware 22 04 2016|awk '{print $5;}'

14
:~$
And incorporating norobro test
Code:
:~$ hdate -q --not-sunset-aware 22 04 2016|awk '$5!="" {print $5;}'

14
:~$
Yields the same result as far as I can tell.
 
Old 04-25-2016, 06:45 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Please post the output from these commands:
Code:
locale
hdate -q --not-sunset-aware 22 04 2016 | hexdump -C
 
Old 04-25-2016, 07:00 PM   #11
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Code:
:~$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

:~$ hdate -q --not-sunset-aware 22 04 2016 | hexdump -C
00000000  0a 46 72 69 64 61 79 2c  20 32 32 20 41 70 72 69  |.Friday, 22 Apri|
00000010  6c 20 32 30 31 36 2c 20  31 34 20 4e 69 73 61 6e  |l 2016, 14 Nisan|
00000020  20 35 37 37 36 0a                                 | 5776.|
00000026
:~$
 
Old 04-25-2016, 07:09 PM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Does not look like a locale problem. If hdate does not have any latitude longitude or UTC offset inputs it adds a linefeed regardless of the -q (suppress warnings) or sending stderr to /dev/null

You could either strip the linefeed using any sort of string function or include the missing options.

Quote:
~/$ hdate
hdate: ALERT: time zone not entered, using system local time zone: DST, -X:0 UTC
hdate: ALERT: guessing... will use co-ordinates for

Monday, 25 April 2016, eve of 18 Nisan 5776

Last edited by michaelk; 04-25-2016 at 07:10 PM.
 
Old 04-25-2016, 07:14 PM   #13
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Strange. It gets rid of the LF on my machine:
Code:
$ hdate -q --not-sunset-aware 22 04 2016

Friday, 22 April 2016, 14 Nisan 5776
$ hdate -q --not-sunset-aware 22 04 2016|awk '$5!="" {print $5;}'
14
$ hdate -q --not-sunset-aware 22 04 2016|awk '$5 {print $5;}'
14
 
Old 04-25-2016, 07:20 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Difference in versions?
 
Old 04-25-2016, 07:23 PM   #15
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks All

norobro, not sure why it did not remove it the first time I ran it, but after I ran the commands to supply the requested info it started working.

michaelk, you are correct, adding in the long & lat and time zone data caused it to not generate the line feed.

So which is the better option?
 
  


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
[SOLVED] another bash script fail rbees Programming 12 11-29-2015 07:09 AM
[SOLVED] Bash script setxkbmap: why fail? Weapon S Linux - Software 6 01-18-2013 04:29 AM
[SOLVED] bash script syntax fail as root goncalopp Linux - General 6 04-14-2011 05:25 AM
[SOLVED] [BASH] confused with test. RaptorX Programming 4 08-24-2009 03:21 PM
Confused over a BASH command Couling Linux - Newbie 7 11-12-2007 08:18 AM

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

All times are GMT -5. The time now is 06:37 AM.

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