It is required to count the total number of lines in txt files lying in the directory whose name is specified by the script parameter.

 #! /bin/bash A1=cat/A1 | grep '\.txt$'| wc -l echo $A1 

What to do to make it work?

  • cat * .txt | wc -l If you want to save in a variable, then a1 = cat *.txt | wc -l cat *.txt | wc -l ; echo $ a1 - avp
  • one
    @avp missed a little cat $1/*.txt | wc -l cat $1/*.txt | wc -l Otherwise, it is possible #! / bin / sh cat $ 1 / *. txt | sed -ne '$ =' - alexlz
  • @alexlz is also correct, but then add cat $ {1: -. /} / *. txt | wc -l - avp 2:07 pm
  • @avp under the terms of the problem, the first parameter should be. - alexlz pm
  • And in fact, I did not really read the question myself. Then check the number of arguments if [$ # -ne 1]; then echo Invalid arguments && exit 1 fi cat $ 1 / *. txt | wc -l @alexlz, directly a small shell tutorial for @ nick, we have depicted you. - avp 2:29 pm

2 answers 2

#! /bin/bash

if you are using shebang , its syntax does not provide spaces in this place.

and the script might look something like this:

 #!/bin/bash cat $1/*.txt | wc -l 

or, if you want to save (and then print) the number of lines in a variable:

 #!/bin/bash k=$(cat $1/*.txt | wc -l) echo $k 

Zache through the conveyor immediately, you can directly:

 ~$ wc -l ./* 3 ./abc.txt 0 ./big-file 1 ./test1 4 total