0

I've written a short bash script to monitor the line count growth (wc -l) in a file which is receiving results from a loop.

Thus:

printf "Name of file to monitor\n"
read file

printf "How long to monitor file growth in minutes\n"
read time

printf "Interval between loops\n"
read s

a=$((time * 60))                                   # works out overall time in seconds
b=$((a / s))                                       # no of loops

for i in $( eval echo {0..$b} )
do
    printf "Loop number: %-10.2d Interval: %-10.2d Line Count: %-10.2d.\n" $i  $s  $'wc -l $file'   
    sleep $s 
done
printf "finished\n"

I'm having problems with the last argument of the printf line. I'm not sure how to correctly state the wc function within the printf function.

1
  • 1
    To get wc to output the number use $(wc -l $file). And for good programming practices, you should use quotes (either single or double) around variables. Take a look at this post. Commented Jan 19, 2023 at 14:45

1 Answer 1

0

When you call wc -l filename, it outputs filename next to the number of lines in it. And when a command substitution is not wrapped in double-quotes, it undergoes word-splitting, i.e expands to multiple words if it has any spaces in it. And, lastly, printf uses the format over and over until it consumes all given arguments (see here).

The correct way is:

printf "Loop number: %-10.2d Interval: %-10.2d Line Count: %-10.2d \n" $i  $s  $l "$(wc -l <"$file")"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.