LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2012, 11:16 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
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.
 
Old 02-16-2012, 11:21 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
1 members found this post helpful.
Old 02-16-2012, 11:40 PM   #3
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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...

Last edited by sysmicuser; 02-16-2012 at 11:46 PM.
 
Old 02-16-2012, 11:47 PM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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;

Last edited by sysmicuser; 02-16-2012 at 11:52 PM.
 
Old 02-16-2012, 11:56 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 ))
then
    echo yep
else
    echo nup
fi
 
1 members found this post helpful.
Old 02-17-2012, 12:17 AM   #6
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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
 
Old 02-17-2012, 12:25 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have a close look between your df ... statement and mine (hint: look before df command)
 
Old 02-17-2012, 08:01 AM   #8
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
That is my gross mistake, "$" sign is the answer
 
Old 02-17-2012, 08:49 AM   #9
uhelp
Member
 
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
(( )) 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.
 
1 members found this post helpful.
Old 02-17-2012, 09:02 AM   #10
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
to get the disk usage of the system wagmare Linux - General 1 09-03-2010 03:08 AM
disk usage by unknown file Ammad Linux - General 5 07-31-2010 05:52 AM
disk usage by file age michael.barnes Linux - General 1 06-30-2010 02:29 PM
Lots of disk usage for no reason, system locks up gimpy530 Ubuntu 16 04-15-2008 05:44 PM
Removed a large file but disk usage not updated. LucL Red Hat 2 03-21-2007 07:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration