Skip to main content
2 of 7
added 374 characters in body
Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k

The reason you're getting a syntax error is that if you give wc -l two files, it will produce three lines of output:

$ wc -l .profile .profile
      30 .profile
      30 .profile
      60 total

This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces the syntax error. It is a bit unintuitive that the error mentions the else instead of the test condition, but that has to do with how the script is being parsed.

You probably want

WCOUNT=$( wc -l <output.txt )

Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.

To test whether a string is present in a file, use

if grep -Fq 'fixed string' filename; then

e.g.

if grep -Fq 'DHCPSTART' output.txt; then

The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.

Kusalananda
  • 355.9k
  • 42
  • 735
  • 1.1k