I have a simple bash script I have written to count the number of lines in a collection of text files, and I store each number of lines as a variable using a for loop. I would like to print each variable to the same text file, so that I may access all the line counts at once, from the same file.
My code is:
for f in *Daily.txt; do
lines=$(cat $f | wc -l);
lines=$(($num_lines -1));
echo $lines > /destdrive/linesTally2014.txt;
done
When I run this, the only output I receive is of the final file, not all the other files.
If anyone could help me with this I would really appreciate it. I am new to bash scripting, so please excuse this novice question.
lines=$(cat ...)a typo fornum_lines=$(cat ...)?lines=$(( $(wc -l < "$f") - 1))would be the proper way to get that value in one line.lines=$(($num_lines - 1))is a typo forlines=$(($lines - 1)). But there is a problem with name consistency.