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 ;
Best Answer
You missed couple of points, the correct (Only syntatically) form would be:
There must be space after
test
([
) and before]
You need to put a
;
(synonymous to newline) after firstif
conditionYou need to close the
if
condition usingfi
at last portionAlso 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:
Most importantly, there is another major problem in your
if
condition, you are comparing a string (output ofdu -sh /tmp/filename.log
) with an integer (0
), which is wrong.You can do the following:
Or simply:
In the last command, the command after
&&
will be run only if the previous command returns an exit status0
i.e. success. So, you don't need anif
condition after all.Also if you are using
bash
, try to use thebash
keyword[[
instead oftest
[
as it provides lots of features thattest
does not. So your command could take the following final form:Or the simplest way:
The
-s
indicates that if the file is greater than0
in size then will return true.