1820243 Members
2497 Online
109621 Solutions
New Discussion юеВ

Re: Bash Conditionals

 
SOLVED
Go to solution
Steve Burt_1
Advisor

Bash Conditionals

Hi Folks


I am having a problem with a script that checks for ntp drift and timezone, the ntp drift part is fine.

What I want is the script to check for both conditions and display the results accordingly


#!/bin/bash
# time-offset_check.sh
#
# Checks computer clock against specified NTP server. Issues a warning
# on stdout, if the time difference exceeds limit. Requires ntpdate, bc
# and egrep in path. Run from cron as often as you like.

### Options

TIMEZONE=`date | awk '{ print$5}'`
NTPSERVER='10.17.0.80' # i.e. random server from pool.ntp.org
LIMIT='150.000000' # i.e. 1.000000 (= 1 second)
LOCALTIMEZONE='GMT'
### Do not edit

OFFSET=`/usr/sbin/ntpdate -q $NTPSERVER \
| egrep -o -m 1 -e "offset ((-)?[0-9]+\.[0-9]+)" \
| egrep -o -e "[0-9]+\.[0-9]+"`

RESULT=`echo "$OFFSET >= $LIMIT" | bc`

if (("$RESULT" == 1)); then
echo "Warning: Local clock offset ($OFFSET) larger than limit ($LIMIT) - Check NTPD"
fi


if (("$TIMEZONE" !="GMT")); then
echo "Warning: Local Time Zone is not set to GMT - Check /etc/sysconfig/clock or /etc/localtime"
fi

exit 0
~
When I set the timezone to MET I get

[root@sburt-lap seb]# ./time-offset_check.sh
++ date
++ awk '{ print$5}'
+ TIMEZONE=MET
+ NTPSERVER=10.17.0.80
+ LIMIT=150.000000
+ LOCALTIMEZONE=GMT
++ /usr/sbin/ntpdate -q 10.17.0.80
++ egrep -o -m 1 -e 'offset ((-)?[0-9]+\.[0-9]+)'
++ egrep -o -e '[0-9]+\.[0-9]+'
+ OFFSET=18631470.639141
++ echo '18631470.639141 >= 150.000000'
++ bc
+ RESULT=1
+ (( 1 == 1 ))
+ echo 'Warning: Local clock offset (18631470.639141) larger than limit (150.000000) - Check NTPD'
Warning: Local clock offset (18631470.639141) larger than limit (150.000000) - Check NTPD
+ (( MET != GMT ))
+ exit 0
[root@sburt-lap seb]#


I am not sure where I am going wrong...

Thanking you...

--Steve
5 REPLIES 5
Ivan Ferreira
Honored Contributor
Solution

Re: Bash Conditionals

if [ condition ]
then
action
fi


This works:

#!/bin/bash
# time-offset_check.sh
#
# Checks computer clock against specified NTP server. Issues a warning
# on stdout, if the time difference exceeds limit. Requires ntpdate, bc
# and egrep in path. Run from cron as often as you like.

### Options

TIMEZONE=`date | awk '{ print$5}'`
NTPSERVER='10.129.4.123' # i.e. random server from pool.ntp.org
LIMIT='150.000000' # i.e. 1.000000 (= 1 second)
LOCALTIMEZONE='GMT'
### Do not edit

OFFSET=`/usr/sbin/ntpdate -q $NTPSERVER \
| egrep -o -m 1 -e "offset ((-)?[0-9]+\.[0-9]+)" \
| egrep -o -e "[0-9]+\.[0-9]+"`

RESULT=`echo "$OFFSET >= $LIMIT" | bc`

if [ "$RESULT" == 1 ]
then
echo "Warning: Local clock offset ($OFFSET) larger than limit ($LIMIT) - Check NTPD"
fi


if [ "$TIMEZONE" != "GMT" ]
then
echo "Warning: Local Time Zone is not set to GMT - Check /etc/sysconfig/clock or /etc/localtime"
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Matti_Kurkela
Honored Contributor

Re: Bash Conditionals

The "(( expression ))" syntax is for arithmetic expressions only. It is regarded as "false" if and only if the arithmetic expression inside evaluates to 0.

(( 1 == 1 )) evaluates to 1 just like in C, so the expression is "true" and you get your warning.

(( MET != GMT )) is non-arithmetic, so it evaluates to 0 and the expression is false.

For conditional expressions, you'll want "[ expression ]" (as Ivan already posted) or "[[ expression ]]".

MK
MK
Steve Burt_1
Advisor

Re: Bash Conditionals

Ivan,

Many thanks for your helpful explaination...

Thanks

Steve
Steve Burt_1
Advisor

Re: Bash Conditionals

I should say Matt as well... Thanking you both its very much apreciated

--Steve
Dennis Handly
Acclaimed Contributor

Re: Bash Conditionals

Here is a ksh example where I go crazy with (( )):
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1358125