I am trying to pull logs and doing few others things directly. written a small line BASH but not working. I am sure thats with IF Condition.

sed -rne '/21:25:07/,/21:50:07/ p'  server.log.2015-04-21 > /tmp/filename.log ;
du -sh  /tmp/filename.log ; 
if [`du -sh  /tmp/filename.log` -gt 0] then gzip /tmp/filename.log ;
  • Also in your sed command you don't need -r and -e....also if you have any line containing 21:50:07 before your desired one, this will give you wrong output.. – heemayl Apr 21 '15 at 18:27

You missed couple of points, the correct (Only syntatically) form would be:

if [ `du -sh  /tmp/filename.log` -gt 0 ]; then gzip /tmp/filename.log; fi
  • There must be space after test ([) and before ]

  • You need to put a ; (synonymous to newline) after first if condition

  • You need to close the if condition using fi at last portion

Also you should use $() instead of `` as command substitution as the latter one is deprecated in favor of $().

So, literally you command can be made more robust:

if [ $(du -sh  /tmp/filename.log) -gt 0 ]; then gzip /tmp/filename.log; fi

Most importantly, there is another major problem in your if condition, you are comparing a string (output of du -sh /tmp/filename.log) with an integer (0), which is wrong.

You can do the following:

if [ $(du -s ./test.txt | cut -f1) -gt 0 ]; then gzip /tmp/filename.log; fi

Or simply:

[ $(du -s ./test.txt | cut -f1) -gt 0 ] && gzip /tmp/filename.log

In the last command, the command after && will be run only if the previous command returns an exit status 0 i.e. success. So, you don't need an if condition after all.

Also if you are using bash, try to use the bash keyword [[ instead of test [ as it provides lots of features that test does not. So your command could take the following final form:

[[ $(du -s ./test.txt | cut -f1) -gt 0 ]] && gzip /tmp/filename.log

Or the simplest way:

[[ -s /tmp/filename.log ]] && gzip /tmp/filename.log

The -s indicates that if the file is greater than 0 in size then will return true.

  • remove the h in -sh – A.B. Apr 21 '15 at 15:51
  • if [ $(du -s foo2 | awk '{print $1}') -gt 0 ]; then gzip foo2; fi – A.B. Apr 21 '15 at 15:52
  • @A.B.: awk is an overkill.. – heemayl Apr 21 '15 at 15:54
  • Hmpf, I like it. =) – A.B. Apr 21 '15 at 15:55
  • With the if condition, if you are trying to check if the file is larger than 0 then you should be able to use the -s expression - there is a useful list here – Wilf Apr 21 '15 at 16:05

A good idea is to check his scripts with this tool.

Here is an annotated version of your error:

   1  #!/bin/bash
   2  sed -rne '/21:25:07/,/21:50:07/ p'  server.log.2015-04-21 > /tmp/filename.log ;
   3  du -sh  /tmp/filename.log ; 
   4  if [`du -sh  /tmp/filename.log` -gt 0] then gzip /tmp/filename.log ;
      ^––SC1009 The mentioned parser error was in this if expression.
         ^––SC1073 Couldn't parse this test expression.
          ^––SC1035 You need a space after the [ and before the ].
                                            ^––SC1020 You need a space before the ].
                                            ^––SC1072 Missing space before ]. Fix any mentioned problems and try again.


  • Ooh... I'm going to use this tool too from now on! – Fabby Aug 19 '15 at 11:43
if [ `du -sh  /tmp/filename.log` -gt 0 ]; then gzip /tmp/filename.log; fi

The spaces in if condition and fi at the end to close the if

  • if [ $(du -s /tmp/filename.log | awk '{print $1}') -gt 0 ]; then gzip /tmp/filename.log; fi – A.B. Apr 21 '15 at 15:53
  • Or use cut -f1 instead of awk. Replace the backticks with $(..) ;) – A.B. Apr 21 '15 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.