0

I am trying to create a small program to count the no of lines in a set of log files based some user supplied time frame.I am currently in the last step and due to some reason when i execute the program it fails with the below error grep: |: No such file or directory grep: wc: No such file or directory

The execution happens at line 6 in the code below. Line 7 shows a sample command that i am trying to execute. If I comment line 6 and uncomment line 7, it executes. I might be missing something simple. Appreciate your help. The values on those variables are given below

fileName="' summary_* | wc -l" command="grep '" comm=ived>20161215110

1 for (( z=0; z<($interval/10); z++ ))
2 do
3       comm=$tempCommand$count
4       let "count = $count + 1"                                
5       #echo "$command$comm$fileName"
6       totalCount=`$command$comm$fileName`
7       #totalCount=`grep 'ived>20161215110' summary_* | wc -l`
8       echo $totalCount
9 done
  • grep ... | wc -l might be simplified to just grep -c ... – thrig Dec 15 '16 at 20:25
  • For starters, why grep | wc -l when you could grep -c? Also, I would throw in some debug output that shows the contents of $command, $comm, and $filename. – DopeGhoti Dec 15 '16 at 20:25
  • There are a set of files on which i am applying this grep. I just need the final total count – Shawn Dec 15 '16 at 20:33
  • I have updated the variables in the question – Shawn Dec 15 '16 at 20:38
1

The shell recognizes the | metacharacter at the very beginning of parsing. When expanding a variable value, it's already too late, so | behaves as a literal vertical bar, i.e. as if you ran

grep 'ived>20161215110' summary_* \| wc -l

You can use a function instead of a variable.

  • So essentially what change do you want me to make in the script to get it work? – Shawn Dec 15 '16 at 21:11
  • 1
    @Shawn: I can't run your script, you haven't shown enough code. See mcve. – choroba Dec 15 '16 at 21:21

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.