Skip to main content
incorporated Kusalananda's extension of grepping directly
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

One option: tell case to look for a 0, in which case: do nothing; otherwise, execute your command:

case X in
  (0) : ## do nothing
      ;;
  (*) truncate ...
      ;;
esac

Another option: tell case to look for any non-zero digit:

case X in 
  ([123456789]) truncate ...
esac

... but instead of trying to match numbers with case, I would use test:

if [ "$(grep -c ERROR /root/test/wayfile.log)" -gt 0 ]
then
  truncate ...
fi

I've simplified grep ... | wc -w to just grep -c, which asks grep to do the counting instead of involving wc.

Further, since you're really just interested in whether the word ERROR exists at all in the file, you can just ask grep if it's there:

if grep -F -q ERROR /root/test/wayfile.log
then
   truncate ...
fi

The -F flag tells grep that our search text is plain ("Fixed") text -- no regular expressions; the -q flag tells grep to be "quiet" -- don't count or output matching lines, just set its exit status based on whether it found the search text at all (or not). A successful exit code (of 0) indicates that the search text was found at least once in the file; failure indicates that the search text was not found at all in the file.

One option: tell case to look for a 0, in which case: do nothing; otherwise, execute your command:

case X in
  (0) : ## do nothing
      ;;
  (*) truncate ...
      ;;
esac

Another option: tell case to look for any non-zero digit:

case X in 
  ([123456789]) truncate ...
esac

... but instead of trying to match numbers with case, I would use test:

if [ "$(grep -c ERROR /root/test/wayfile.log)" -gt 0 ]
then
  truncate ...
fi

I've simplified grep ... | wc -w to just grep -c, which asks grep to do the counting instead of involving wc.

One option: tell case to look for a 0, in which case: do nothing; otherwise, execute your command:

case X in
  (0) : ## do nothing
      ;;
  (*) truncate ...
      ;;
esac

Another option: tell case to look for any non-zero digit:

case X in 
  ([123456789]) truncate ...
esac

... but instead of trying to match numbers with case, I would use test:

if [ "$(grep -c ERROR /root/test/wayfile.log)" -gt 0 ]
then
  truncate ...
fi

I've simplified grep ... | wc -w to just grep -c, which asks grep to do the counting instead of involving wc.

Further, since you're really just interested in whether the word ERROR exists at all in the file, you can just ask grep if it's there:

if grep -F -q ERROR /root/test/wayfile.log
then
   truncate ...
fi

The -F flag tells grep that our search text is plain ("Fixed") text -- no regular expressions; the -q flag tells grep to be "quiet" -- don't count or output matching lines, just set its exit status based on whether it found the search text at all (or not). A successful exit code (of 0) indicates that the search text was found at least once in the file; failure indicates that the search text was not found at all in the file.

Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 267

One option: tell case to look for a 0, in which case: do nothing; otherwise, execute your command:

case X in
  (0) : ## do nothing
      ;;
  (*) truncate ...
      ;;
esac

Another option: tell case to look for any non-zero digit:

case X in 
  ([123456789]) truncate ...
esac

... but instead of trying to match numbers with case, I would use test:

if [ "$(grep -c ERROR /root/test/wayfile.log)" -gt 0 ]
then
  truncate ...
fi

I've simplified grep ... | wc -w to just grep -c, which asks grep to do the counting instead of involving wc.