Script shell with errors during tests

If none of the more specific forums is the right place to ask

Script shell with errors during tests

Postby CédricF » 2018-12-06 09:49

Hi,

We have a license check script for Nagios that makes us go wrong during testing.

Here is the piece in error:

Code: Select all
if [ "$EXPIRY" = "UNKNOWN" ] ; then
                 STATE="UNKNOWN"
         elif [ "$EXPIRY" = "∞" ] || [ "$EXPIRY" -gt "7" ];then
                 STATE="OK"
         elif [ "$EXPIRY" = "7" ] && [ "$EXPIRY" -gt "1" ] ; then
                 STATE="WARNING"
         elif [ "$EXPIRY" = "1" ] ; then
                 STATE="CRITICAL"
fi
 
echo $STATE -- "La licence expire dans $EXPIRY jours"


and the error return when testing the script:

Code: Select all
./check_zend_license: ligne 50 : [:                                                     <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> : nombre entier attendu comme expression
-- La licence expire dans                                                       <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> jours


Line 50 corresponds to:

Code: Select all
elif [ "$EXPIRY" = "∞" ] || [ "$EXPIRY" -gt "7" ];then


I tried with just :

Code: Select all
[ "$EXPIRY" -gt "7" ]


but I get the same result.

The script works very well on Red Hat but not on Debian (8 and 9). It appears from many errors that I corrected but as I am still weak on shell, I block for the last one.

Thank you for any feedback on this subject.
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby CédricF » 2018-12-06 09:58

the original script was:

Code: Select all
if [ $EXPIRY = "UNKNOWN" ] ; then
                 STATE="UNKNOWN"
         elif [ "$EXPIRY" = "∞" ] || [ "$EXPIRY" -gt "7" ];then
                 STATE="OK"
         elif [ "$EXPIRY" -le "7" ] && [ "$EXPIRY" -gt "1" ] ; then
                 STATE="WARNING"
         elif [ "$EXPIRY" -le "1" ] ; then
                 STATE="CRITICAL"
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby CédricF » 2018-12-06 14:04

I changed the end of the script on the failed part as follows:

Code: Select all
if [ "$EXPIRY" = "UNKNOWN" ] ; then
                 STATE="UNKNOWN"
         elif [[ "$EXPIRY" = "∞" ]] || [[ "$EXPIRY" -gt "7" ]] ; then
                 STATE="OK"
         elif [[ "$EXPIRY" -le "7" ]] && [[ "$EXPIRY" -gt "1" ]] ; then
                 STATE="WARNING"
         elif [[ "$EXPIRY" -le "1" ]] ; then
                 STATE="CRITICAL"
fi
 
echo $STATE -- "La licence expire dans $EXPIRY jours"


and in this case, I get :

Code: Select all
./check_zend_license: ligne 50: [[: <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> : syntax error: expected operand (the erroneous symbol is "<span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div>")
./check_zend_license: ligne 52: [[: <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> : syntax error: expected operand (the erroneous symbol is "<span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div>")
./check_zend_license: ligne 54: [[: <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> : syntax error: expected operand (the erroneous symbol is "<span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div>")
-- La licence expire dans                                                       <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> jours
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby bw123 » 2018-12-06 14:06

I get an error in dash or bash when EXPIRY is not an integer:

dash: Illegal number:
bash: integer expression expected
User avatar
bw123
 
Posts: 3583
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Script shell with errors during tests

Postby CédricF » 2018-12-06 14:32

Hi,

This is the line concerning EXPIRY :

Code: Select all
EXPIRY=$(( ($(date -d "${DATE:3:2}/${DATE:0:2}/${DATE:6}" +%s)-$(date +%s)) / 86400 ))
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby bw123 » 2018-12-06 14:38

Code: Select all
$ date -d "${DATE:3:2}/${DATE:0:2}/${DATE:6}"
date: invalid date ‘//’


It's the problem with globals and $DEFINES, you never know how they are assigned. Something in the code is probably doing a bad thing. EXPIRY should be an integer for the comparison, but it's coded where it could be either string or int. I think there is abuiltin to compare strings as number, try looking up "builtin string comparison" or something like that. Have you tried > instead of -gt?
User avatar
bw123
 
Posts: 3583
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Script shell with errors during tests

Postby CédricF » 2018-12-06 14:46

No, i trying.
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby CédricF » 2018-12-06 15:00

Thank you,

It works, and i only have one error message:

Code: Select all
OK -- La licence expire dans                                                    <span ng-if="loginData.license.daysToExpired === 0">Your license expires today</span>
                                                        <span ng-if="loginData.license.daysToExpired > 0">Your license will expire in {{loginData.license.daysToExpired}} day{{loginData.license.daysToExpired > 1 ? 's' : ''}}</span>
                                        <div ng-if="! (loginData.license.isOk && loginData.license.daysToExpired === false)"><a href="http://www.zend.com/go/trial/server-6-upgrade-login" target="_blank">Contact us to get a license</a></div> jours


which concerns the display of the result coming from this first line :

Code: Select all
echo $STATE -- "La licence expire dans $EXPIRY jours"

if [ "${STATE}" == "WARNING" ];then
                        exit ${STATUS_WARNING}
        elif [ "${STATE}" == "OK" ];then
                        exit ${STATUS_OK}
        elif [ "${STATE}" == "UNKNOWN" ];then
                        exit ${STATUS_UNKNOWN}
        else
                        exit ${STATUS_CRITICAL}
fi
CédricF
 
Posts: 6
Joined: 2018-12-06 09:40
Location: Paris, France

Re: Script shell with errors during tests

Postby bw123 » 2018-12-09 03:41

For several reasons, bash is probably not what you want for this type of thing. C might be the way to go?
https://en.wikipedia.org/wiki/Strong_and_weak_typing

To me, as a layman, not a programmer, that code looks like this to me:

If some define matches another define
then do some (maybe)defined thing.

You're not using the language, you're just making a lot of global defines and using a sloppy if( ) block to try and figure out if you have a coherent set of several globals to proceed to some logical result. I know that defines and globals are all the thing now, but the old fashioned procedural rules probably will still give you a reliable fallback.

Consider getting rid of all the globals and defines, and giving EXPIRY and STATE a definite numerical value directly before the if( ) block, or at least commenting the values of WARNING etc... before the block for future revisions of the code.

IIRC, c has a "const" declaration that is really great at preventing reassignment that can lead to errors in this type of program.
User avatar
bw123
 
Posts: 3583
Joined: 2011-05-09 06:02
Location: TN_USA

Re: Script shell with errors during tests

Postby xepan » 2018-12-09 10:39

Some notes, which don't really solve the actual problem:

I am not that sure by the output (how do you get it? btw) you parse if you may have run in something like this:
http://mywiki.wooledge.org/BashFAQ/113? ... %28lynx%29
Which boils down to the question, if a bash/shell script is the right tool for the job, as bw123 already said.

You use sometimes [ and sometimes this [[.
From your snippet it isn't clear if you got a bash script or a posix like script. If you use bash, just stick to [[ , if not make sure that posix [ supports the syntax you use ( mainly = and ==, which in general is rather confusing to me. Sure something worth looking at)
http://mywiki.wooledge.org/Bashism?acti ... ct=bashism

Also try:
https://www.shellcheck.net/
and if the content is too private, you could also use the "shellcheck" script from the debian repos (i was told preferably to use the website, as distros versions tend to be outdated).
There is also "checkbashism" in the debian repos (but shellcheck should point bashisms out, in case you got the according shebang).
I would do that first of all.
xepan
 
Posts: 18
Joined: 2018-11-28 06:38


Return to General Questions

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable