One liners for File system disk usage.
Hi,
I am trying to develop a script 1 liner to check disk usage on file system, if it is more than 80% it should send email if it is less than than it should echo say "Do nothing" However for both of them I am having some syntax problems:(:( 1)if [[ (df -P|egrep -i 'test'|awk '{print$5}'|sed 's/%//') >= 80 ]]; then mailx -s "test FULL" testme@testmail.com else echo "do nothing" fi I am getting error message like. -bash: conditional binary operator expected -bash: expected `)' -bash: syntax error near `|' If I do with single square brackets. 2) if [ (df -P|egrep -i 'test' |awk '{print$5}'|sed 's/%//') >= 80 ]; then mailx -s "test is FULL" testme@testmail.com else echo "do nothing" fi -bash: syntax error near unexpected token `df' What the heck is difference between if with single and double square brackets Guru's? Please assist in my understanding. Many thanks. |
|
Thanks for that Chris. I got a good understanding that [[ ]] are fairly strong than that of [ ] and that when it says conditional binary operator expected so I changed >= to -ge but still that error prevails...
|
Lastly I have now introduced " " and " " within if conditional statement but still no good and it just hangs...
Here we go this is code. if [[ "(df -P|egrep -i 'test' |awk '{print$5}'|sed 's/%//')" -ge "80" ]]; then mailx -s "test" test@testmail.com else echo "do nothing" fi; |
Personally when doing math comparisons I like to keep with the familiar operators, hence it is often just easier to use (()), eg.
Code:
if (( $(df -P | awk 'tolower($0) ~ /test/{print strtonum($5)}') >= 80 )) |
grail mate:) you come up with some sensational command ! :):) need some time to digest, your stuff is working but I would love to understand where I am making mistake and where I do need to improve. weekend is going to be with shell scripting :):)
|
Have a close look between your df ... statement and mine (hint: look before df command)
|
That is my gross mistake, :( "$" sign is the answer :)
|
(( )) denotes an calculation
$( cmd ) substitutes the expression with the output of cmd. This is called a "parameter expansion" ( cmd; cmd2 | cmd3 ) just groups all comamnds avoiding a subshell <( cmd ) executes cmd and connects stdout of cmd to a filedescriptor from which the calling script reads. [someTest] is the portable POSIX way of testing. Available in nearly each and every shell. [[ someTest ]] is the modern bash way. Has a lot more features and makes script easier to read. |
Cooking !
Getting closer but still not that close :( Now the one liner is working but still i am having issues using mailx if [[ $(df -P|egrep -i '/test'|awk '{print$5}'|sed 's/%//') -ge 80 ]]; then mailx -s "test is FULL" email@removed </tmp/$$; else echo "do nothing"; fi The above one liner remains in hang state unless and until I perform Ctl-C operation (twice). How can get work around with it satisfying as it becomes truly one liner. Say for example echo "test is more than 80% FULL" > /tmp/$$;if [[ $(df -P|egrep -i '/test'|awk '{print$5}'|sed 's/%//') -ge 80 ]]; then mailx -s "test is FULL" email@removed </tmp/$$; else echo "do nothing"; fi This works but it doesn't have charm of being a one liner. But say if this comes the only one way then what should we do to. 1 echo statement reflects right value of %percentage used rather than just 80? |
All times are GMT -5. The time now is 05:56 AM. |