Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Lexical v Arithemetic testing, Bash and Perl

If you tell a story against someone, best to be telling it against yourself!

One of our web servers (running standard, not our own software behind the scenes) has been having a problem with handling denial of service attacks which are coming in from time to time ... and I have a monitoring script running in bash which notices an overloaded system within a short time period, and takes appropriate action. That's done using the uptime command, and was originally written as follows:

buzz=`uptime`
busy=`echo $buzz | sed 's/.*load//' | awk '{print $2}' | tr , " "`
if [[ $busy > 4 ]] ; then


In other words .... get the statistics for the server loading, and if it's had more that 4 jobs in the queue for the last minute on average, take appropriate action.

The script has been running well - tripped occasionally when we've had an attack and had the server back quickly. But this morning I found the server dead and unreachable ... ("Don't Panic" comes to mind!).

Turns out that I have a coding error in my bash - did you notice? I used a > check which tests whether the loading is LEXICALLY greater than 4. Alas - my loadings had jumped in such a way that they were always lexically less than 4, even though they were arithmetically much higher:

06:44:00 up 7 days, 12:16, 1 user, load average: 0.13, 0.54, 0.35
06:45:00 up 7 days, 12:17, 1 user, load average: 1.75, 0.80, 0.45
06:46:05 up 7 days, 12:18, 1 user, load average: 26.47, 7.81, 2.86
06:48:44 up 7 days, 12:21, 1 user, load average: 123.63, 54.70, 21.22


So my trip had never been activated .... and once the loading gets up to that level the system is pretty well screwed!

Solution - change code into ...

buzz=`uptime`
busy=`echo $buzz | sed 's/.*load//' | awk '{print $2}' | tr , " " | tr . 0`
if [[ $busy -gt 4000 ]] ; then


and I now await the next attack to see how it copes. (Shell arithmetic is integer, so we have converted the floating loading!)

This is a tip for users of bash. Note that in Perl, the operators worth the other way around with gt being lexical and > being arithmetic. I'll use that, as I'm predominantly a Perl programmer, as my excuse!
(written 2007-12-11, updated 2007-12-12)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P204 - Perl - Conditionals and Loops
  [353] Wimbledon Neck - (2005-06-20)
  [930] -> , >= and => in Perl - (2006-11-18)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1607] Learning to program in Perl - (2008-04-11)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [4031] Showing what programming errors look like - web site pitfall - (2013-03-06)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)

A167 - Web Application Deployment - Shell Programming (bash)
  [63] Almost like old times - (2004-09-26)
  [64] Shell Script for CGI on the web - (2004-09-26)
  [749] Cottage industry or production line data handling methods - (2006-06-07)
  [827] No news is good news with Unix and Linux - (2006-08-10)
  [1287] Work and play at Well House Manor - Football and Shell Shortcuts - (2007-08-02)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [1527] Selecting file names in a shell - one word or another - (2008-02-02)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [3791] The Kernel, Shells and Daemons. Greek Gods in computing - (2012-07-01)
  [4400] Commenting out an echo killed my bash backup script - (2015-01-19)
  [4487] Starting MySQL. ERROR! The server quit without updating PID file - how we fixed it. - (2015-05-06)
  [4584] Bash ... some new scripts to - handling user input - (2015-11-27)
  [4586] Extending your bash shell with aliases, functions and extra commands - (2015-11-28)
  [4587] shell - bash. Writing conditional tests and statements - the options available - (2015-11-28)


Back to
stdout v stderr (Tcl, Perl, Shell)
Previous and next
or
Horse's mouth home
Forward to
Curley brackets v double quotes - Tcl, Tk, Expect
Some other Articles
The Horse goes on and on
Cliff Lift simulator- Lynton to Lynmouth - in Tcl/Tk
fill and expand on Tcl/Tk pack command
Curley brackets v double quotes - Tcl, Tk, Expect
Lexical v Arithemetic testing, Bash and Perl
stdout v stderr (Tcl, Perl, Shell)
Effective Java training - the bootcamp approach
Perl, PHP, Python, Tcl, Linux, MySQL, Ruby courses ...
Python Script - easy examples of lots of basics
All the special characters in HTML ...
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1468_Lex ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb