0

Can you tell me how to get the line count in a script?

PID_COUNT = 'cat /david/file1/test.sh | wc -l '

is not working, shows an error "illegal -l".

terdon
  • 242,166
Baba
  • 51
  • Piping to wc -l works here. Which Linux distro or UNIX system is this? Do you use wc from coreutils? Do you use a dash (-) and the letter l (l)? – Alexander Feb 16 '17 at 10:02

1 Answers1

1

If you want to count the number of lines in /david/file1/test.sh use

wc -l </david/file1/test.sh

If you want to count the number of lines that /david/file1/test.sh produces when run:

/david/file1/test.sh | wc -l

If you want to store that in a variable:

line_count=$( wc -l </david/file1/test.sh )

or

line_count=$( /david/file1/test.sh | wc -l )

depending on whether you want to count the number of lines in the file or the number of lines in the output of the script.

There are two things wrong with your command PID_COUNT = 'cat /david/file1/test.sh | wc -l'

  1. Assignments are required to have no space around =.
  2. The variable PID_COUNT will be a string, not to output of the command. This is due to the single quotes. To capture the output of a command, use $( ... ) as above.

In either case, the error is most likely "PID_COUNT: command not found" rather than "illegal -l".

Kusalananda
  • 333,661