I'm running a bash script to output logs under a log directory. Initially I want to know how many logs are generated, simply do the following:
ls logs | wc -l
And as the bash script is running in real time, I can further use:
watch -n 5 'ls logs | wc -l'
to display the number of log files concurrently.
But I also know the total number of log files that should be generated by checking how many lines under a file called file.txt
by:
cat file.txt | wc -l
Now I want to display how many more logs need to be generated by substracting the two numbers, I searched, there are several ways to do math in shell, e.g. using expr
or use double brackets.
For example, assuming my total number of lines in file.txt
are 1000.
If I hard code this into the command, the final command would be like
`watch -n 5 'ls logs | wc -l | xargs expr 1000 - '
But I want to substitute 1000
with the result coming from cat file.txt | wc -l
command. I tried the following but not working. I'm not sure if xargs
can accept two arguments from 2 commands, or this is not how we use it here.
watch -n 5 'ls logs | wc -l | xargs expr "cat file.txt | wc -l" + '
(Note: what if I only want to count number of lines in file.txt
that not begin with a #
letter and not an empty line? How do I do that here?) Much appreciated.