Posts Tagged shell

Shell Script Version Compare (vercmp)

Here is something useful. A very short shell script to compare standard version numbers of the form ##.##.## with up to four segments. It returns the difference of the first differing segment just like strcmp.

#!/bin/sh
 
expr '(' "$1" : '\([^.]*\)' ')' '-' '(' "$2" : '\([^.]*\)' ')' '|' \
    '(' "$1.0" : '[^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0" : '[^.]*[.]\([^.]*\)' ')' '|' \
    '(' "$1.0.0" : '[^.]*[.][^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0.0" : '[^.]*[.][^.]*[.]\([^.]*\)' ')' '|' \
    '(' "$1.0.0.0" : '[^.]*[.][^.]*[.][^.]*[.]\([^.]*\)' ')' '-' '(' "$2.0.0.0" : '[^.]*[.][^.]*[.][^.]*[.]\([^.]*\)' ')'

Update: Added zeros to deal with unbalanced version compares

, ,

5 Comments

Shell Tricks #3 – Disk Usage Alarm

This is a one-liner to “alarm” if the disk usage of a filesystem has exceeded a certain threshold. I put the directory to monitor as “~” (home) and the threshold as 40%.

test `df -h ~ | grep -o '[0-9]*%' | grep -o '[0-9]*'` -ge 40 && echo high

, , , , , , ,

No Comments

Bad Behavior has blocked 123 access attempts in the last 7 days.