-3

I an writing simple script in Bash, to check Linux file system disk usage on root file system and display a warning message if system is greater than 6%.

The commands are running in a terminal, however when I tried the if statement, I am getting this error on line 16 [: missing]'`

  1 #!/bin/bash
  2 
  3
  4  
  5 clear
  6 #checking for usage on the system and saving in a file usage1
  7 df -h / >usage1
  8
  9 #display current use to the screen
 10 clear
 11 echo
 12 awk '$5>6' usage1
 13
 14 #Creating variable to use in the awk if statemant
 15 usage2= "awk '{print $5}'/home/peters/usage1 | tail -n1 |cut -c1"
 16 #Building statemant
 17 if ($usage2>6)
 18 print "Warning file system greater than 6% !!"
 19
 20 exit 0
Braiam
  • 35,991
  • 1
    Line 15 should read: usage2=$(awk blah blah), and line 17 (( usage2 > 6 )) and if you open an if you require a fi to close it... – jasonwryan Jul 27 '15 at 02:11
  • Are the files usage1 and /home/peters/usage1 the same file? I assumer they are (but I'm not sure.. it is a bit odd either way) -- It seems that you can check it in one line: [[ -n $(df -h / | awk 'int($5)>6{print $5}') ]] && echo "Warning file system greater than 6%" – Peter.O Jul 27 '15 at 04:29
  • Yes usage1 and /home/peters/usage1 is the same file. I did experience problem on the so I am trying full path to the file. – Peter Roberts Jul 27 '15 at 04:50
  • @Peter.O Yes this line works -- [[ -n $(df -h / | awk 'int($5)>1 {print $5}') ]] && echo "Warning file system greater than 6%" ---- Thank you very mutch :) – Peter Roberts Jul 27 '15 at 05:09
  • Asked twice by the same guy http://unix.stackexchange.com/q/218542/50557 – Basile Starynkevitch Jul 27 '15 at 05:24
  • There are so many errors .... 1.-Line 12 change to usage="$( awk '($5+0>6){print $5}' usage1 )" Force numerical value by adding 0. 2.- Change line 13 to echo "$usage". 3.- Comment line 15. 4.- Change line 17 to if [[ $usage -gt 6 ]]; then. 5.- The command print does not print values, echo does. change line 18 to echo "Warning file system greater than 6% !!". Add line 19: fi. –  Jul 28 '15 at 04:15
  • See also https://unix.stackexchange.com/questions/134472/ . – JdeBP Mar 05 '19 at 09:19

4 Answers4

3

Your line 17 should be:

if [ $usage2 -gt 6 ]

You need to put a space after [ and before ]. Also, when comparing numbers, you should be using -eq, -ne, -gt, -lt, -ge, and -le rather than the familiar symbols.

unxnut
  • 6,008
  • Don't use [ ] use [[ ]]. http://unix.stackexchange.com/questions/32210/using-single-or-double-bracket-bash –  Jul 27 '15 at 04:38
2

You want (without any spaces around the =)

usage2=$(awk '{print $5}' /home/peters/usage1 | tail -n1 |cut -c1)

to put into the usage2 variable the output of your awk...|cut -c1 command. You need a space between the awk braced command and its input file name (but no spaces around =). BTW, you could simply use cut(1) as cut -f5 (instead of awk '{print $5}'...) to get the 5th field.

BTW, you don't need any usage1, you could probably just code

usage2=$(df | cut -f5| tail -n1 | cut -c2)

But I did not test that.

Then you should use

if [ "$usage2" -gt 6 ] ; then
   echo "Warning file system greater than 6% !!" > /dev/stderr
fi

to test(1) - it is often a shell builtin - and deal with empty variables value. Since it is an error message it should go to stderr, not stdout. And you don't want to use printf but echo (because printf(1) deals with % specially). Perhaps you want logger(1) ...

But your approach (asking here, perhaps twice) is brittle (and asking twice is rude!). You need to learn more about shells. Read the advanced bash scripting guide, you won't loose your time. Also, use a good editor for writing your shell script. Some editors (emacs) have quite good modes for shells - with syntax coloring that helps! ...

BTW, starting temporarily your script with #!/bin/bash -vx would help during the debugging phase. And remove the clear commands, you don't want them...

At last, did you consider using disk quotas?

  • Thank you for your help. I am learning shell scripting and desperate some times. I will definitely read your suggestion -- advanced bash scripting guide-- good stuff. Posting twice wose a simple error. Did not mean any harm, sorry. – Peter Roberts Jul 27 '15 at 05:46
  • Then delete your second question... Asking twice is rude... – Basile Starynkevitch Jul 27 '15 at 05:49
1

Try writing your if statement like this:

if [ condition ]; then
      Stuff here
fi
hildred
  • 5,829
  • 3
  • 31
  • 43
  • Thank you for all your help guys, I did fix my scrip howewer same error same line nr 16. Pleas look. – Peter Roberts Jul 27 '15 at 03:28
  • 1 #!/bin/bash 5 clear 6 #checking for usage on the system and saving in a file usage1 7 df -h / >usage1 8 9 #display current use to the screan 10 clear 11 echo 12 awk '$5>6' usage1 13 14 #Creating variable to use in the awk if statemant 15 usage2=$ " awk '{print $5}'/home/peters/usage1 | tail -n1 |cut -c1 " 16 17 #Building statemant 18 if [ $usage2 -gt 6 ] ; then 19 echo "Warning file system greater than 6% !!" 20 fi 21 exit 0 – Peter Roberts Jul 27 '15 at 03:45
  • @PeterRoberts please edit your question to add more information. Comments are hard to read and easy to miss. That said, did you actually change your script following the suggestions here? I see the same syntax errors in your comment. – terdon Jul 27 '15 at 08:39
0
[[ -n $(df -h / | awk 'int($5)>6{print $5}') ]] && echo "Warning file system greater than 6%"
Peter.O
  • 32,916
  • Yes your code works with no problems @Peter.O. I did change my script according to you suggestion and works fine. Thanks so much for your help, and professionalism. I did receive a lot of heat for posting my question twice. I did not mean any harm or trying being disrespectful to anyone who help me. I am simply not familiar with this site and Linux community. I am relay surprise how easy people will discard some one, or quick to post negative remark. Any help or comment are welcome here and greatly appreciated. – Peter Roberts Jul 27 '15 at 22:22