LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Confused by bash script fail (https://www.linuxquestions.org/questions/programming-9/confused-by-bash-script-fail-4175578270/)

rbees 04-25-2016 07:01 AM

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

wpeckham 04-25-2016 07:13 AM

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

rbees 04-25-2016 07:46 AM

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


rknichols 04-25-2016 08:53 AM

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.

grail 04-25-2016 10:47 AM

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?

norobro 04-25-2016 10:59 AM

Untested, but does the following work?
Code:

if (( "$CURENTMONTH" == "Nisan"  &&  "$CURENTDAY" > 15 ));then

grail 04-25-2016 11:59 AM

@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)

norobro 04-25-2016 12:28 PM

:redface: 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}'

rbees 04-25-2016 06:41 PM

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.

rknichols 04-25-2016 06:45 PM

Please post the output from these commands:
Code:

locale
hdate -q --not-sunset-aware 22 04 2016 | hexdump -C


rbees 04-25-2016 07:00 PM

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
:~$


michaelk 04-25-2016 07:09 PM

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

norobro 04-25-2016 07:14 PM

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


michaelk 04-25-2016 07:20 PM

Difference in versions?

rbees 04-25-2016 07:23 PM

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?


All times are GMT -5. The time now is 12:16 AM.