user53029
user53029

Reputation: 695

Script to count totals from commands and output to screen

I am looking for assistance in creating a bash script that will run several similar commands, sum up the totals and output that total to the screen. I want to run the following commands:

find /var/log/audit -xdev -type f -printf '%i\n' | sort -u | wc -l

find /boot -xdev -type f -printf '%i\n' | sort -u | wc -l

find /home -xdev -type f -printf '%i\n' | sort -u | wc -l

And so on. I have a few others. What I am basically doing is counting up all of the files in each mount point on my system, then I need the script to sum up all of the output from each commands "wc -l" and output the grand total to the screen. Any help is greatly appreciated.

Upvotes: 0

Views: 130

Answers (4)

Markus Hartwig
Markus Hartwig

Reputation: 11

I would redirect each commands output to a file

your_command >> results.txt

and sum them up

awk '{ sum += $1 } END { print sum }' results.txt

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246847

This might be a good place for dc

{
    for mnt in /var/log/audit /boot /home; do
        find "$mnt" -xdev -type f -printf '%i\n' | sort -u | wc -l
    done
    echo "+"
    echo "+"
    echo "p"
} | dc

You need one less "+" than your number of mountpoints.

Upvotes: 1

karakfa
karakfa

Reputation: 67507

this will work without naming names, change the echo n with your scripts

awk '{sum+=$1} END{print "total: "sum}' < <(echo 4; echo 5; echo 6)

alternatively if the individual counts are not required you can pass more than one path to find

find path1 path2 path3 ...

Upvotes: 1

downtheroad
downtheroad

Reputation: 419

this should work:

a=$(find /var/log/audit -xdev -type f -printf '%i\n' | sort -u | wc -l)

b=$(find /boot -xdev -type f -printf '%i\n' | sort -u | wc -l)

c=$(find /home -xdev -type f -printf '%i\n' | sort -u | wc -l)

final=$(($a+$b+$c))

echo $final

Upvotes: 1

Related Questions