user3179298
user3179298

Reputation: 23

How To Eliminate Duplicate Results?

I've wrote a bash script to check the /etc/fstab, fsck Option (Last column)

0 - Disable

1 - Enable**

#!/bin/bash
for i in $(cat /etc/fstab | grep -v 'proc\|sda\|rootvg\|sysfs\|debugfs\|fd0\|.host\|nfshome:\|devpts' | awk '{print $6}') ; do
for n in $(cat /etc/fstab | grep -v 'proc\|sda\|rootvg\|sysfs\|debugfs\|fd0\|.host\|nfshome:\|devpts' | awk '{print $1}') ; do
if [[ "$i" != "0" ]] ; then
        echo "$n = FSCK check on NON-OS Filesystem ERROR !!!"
else
        echo "The Non OS Partiton FSCK Complted Successfully !!!"
fi
done
done

Output:

 /dev/repovg/lvol1 = FSCK check on NON-OS Filesystem ERROR !!!
/dev/appvg/appvol-os = FSCK check on NON-OS Filesystem ERROR !!!
/dev/TEST12/TEST12 = FSCK check on NON-OS Filesystem ERROR !!!
/dev/repovg/lvol1 = FSCK check on NON-OS Filesystem ERROR !!!
/dev/appvg/appvol-os = FSCK check on NON-OS Filesystem ERROR !!!
/dev/TEST12/TEST12 = FSCK check on NON-OS Filesystem ERROR !!!
/dev/repovg/lvol1 = FSCK check on NON-OS Filesystem ERROR !!!
/dev/appvg/appvol-os = FSCK check on NON-OS Filesystem ERROR !!!
/dev/TEST12/TEST12 = FSCK check on NON-OS Filesystem ERROR !!!

Is there any way i can eliminate the Duplicate Values.

I can try

sh +x script.sh | Sort -u

But, is there anything I can do in the script itself ?

Upvotes: 0

Views: 52

Answers (4)

Ulrich Schwarz
Ulrich Schwarz

Reputation: 7727

You're combining every value in the last column with every mountpoint, that's not only giving you too many messages, but also wrong ones.

gawk '($NF=="0"){ print $1 }' /etc/fstab

should print only the 1st column of entries with a 0 in the last column, whcih puts you halfway there (with the grep you already have), without needing another loop.

Upvotes: 0

Tom Fenech
Tom Fenech

Reputation: 74695

You shouldn't be using a nested loop at all. Try something like this in awk:

awk '!/proc|sda|rootvg|sysfs|debugfs|fd0|.host|nfshome:|devpts/ {
    if ($6 != 0) print $1, "= FSCK check on NON-OS Filesystem ERROR !!!"
    else print "The Non OS Partiton FSCK Complted Successfully !!!"
}' /etc/fstab

This reads through /etc/fstab line by line, skipping lines that match the regular expression (as in your grep). For lines that don't match, it checks the 6th column and prints one of the two messages, depending on the value.

As an aside, the . in .host means "any character", so you should either escape it \. if it should mean a literal . or consider removing it entirely.

Upvotes: 1

Rocky Pulley
Rocky Pulley

Reputation: 23321

If you want to eliminate all duplicates in your script you have to keep track of the values and only print out values you haven't printed already. Piping to "sort -u" should be good enough though.

Upvotes: 0

mjgpy3
mjgpy3

Reputation: 8957

I think you can pipe into uniq after sorting

Upvotes: 0

Related Questions