- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Bash Conditionals
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 05:06 AM
тАО08-05-2009 05:06 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 05:42 AM
тАО08-05-2009 05:42 AM
Solutionthen
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 05:55 AM
тАО08-05-2009 05:55 AM
Re: Bash Conditionals
(( 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 07:36 AM
тАО08-05-2009 07:36 AM
Re: Bash Conditionals
Many thanks for your helpful explaination...
Thanks
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-05-2009 11:39 PM
тАО08-05-2009 11:39 PM
Re: Bash Conditionals
--Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-07-2009 04:13 PM
тАО08-07-2009 04:13 PM
Re: Bash Conditionals
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1358125