0

some times we noticed about conflicts in /etc/fstab file as the following example

/dev/sdg appears twice !

/data/sdb appears twice !

#
# /etc/fstab
# Created by anaconda on Wed Nov  9 13:26:03 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg00-OS-linux_root /                       xfs     defaults        
0 0
UUID=cc749f07-ad72-49e8-ab19-ec6532f5e9 /boot                   xfs     
defaults        0 0
/dev/mapper/vg00-OS-linux_var /var                    xfs     defaults        
0 0
/dev/mapper/vg00-OS-linux_swap swap                    swap    defaults        
0 0


/dev/sdc /data/sdc ext4 defaults,noatime 0 0
/dev/sdb /data/sdb ext4 defaults,noatime 0 0
/dev/sde /data/sde ext4 defaults,noatime 0 0
/dev/sdf /data/sdf ext4 defaults,noatime 0 0
/dev/sdd /data/sdd ext4 defaults,noatime 0 0
/dev/sdg /data/sdb ext4 defaults,noatime 0 0
/dev/sdg /data/sdg ext4 defaults,noatime 0 0
/dev/sdh /data/sdh ext4 defaults,noatime 0 0
/dev/sdi /data/sdi ext4 defaults,noatime 0 0
/dev/sdj /data/sdj ext4 defaults,noatime 0 0
/dev/sdk /data/sdk ext4 defaults,noatime 0 0
/dev/sdl /data/sdl ext4 defaults,noatime 0 0

we want to create simple verification to find conflicts on the first field or the second field in fstab file

what is the best syntax for this purpose ?

verification should find duplicate words in the first field or in the second field , ( syntax line should be short as possible )

expected output - fail / ok

& ( should print all duplicated word from the first field / second field in case of fail )

3
awk ' !/^#/ { if (seendev[$1]++) { print; ++rc; } if (seenmnt[$2]++) { print; ++rc; } } 
     END { exit rc }' < /etc/fstab

The above awk one-liner will print any lines that duplicated column 1 (device) or column 2 (mount-point), and will also exit with a non-zero return code if the above occurs.

| improve this answer | |
  • +1. this would be more useful if it also printed the line number of duplicates. e.g. awk ' !/^#|^[[:space:]]*$/ { if (seendev[$1]++) { print NR ":\t" $0; ++rc; } if (seenmnt[$2]++) { print NR ":\t" $0; ++rc; } } END { exit rc }' /etc/fstab (this version also avoids printing duplicate blank lines. actually, it avoids the false-positive of detecting blank lines as duplicates of themselves) – cas Jan 9 '18 at 4:09
0
#!/bin/sh
# Filter out comments and blank lines from /etc/fstab, pick 1st/2nd field,
# then sort and find duplicates.
DUP_DEVS="$(grep -v -e '^#' -e '^$' /etc/fstab | awk '{ print $1;}' | sort | uniq -d)"
DUP_MOUNTS="$(grep -v -e '^#' -e '^$' /etc/fstab | awk '{ print $2;}' | sort | uniq -d)"

if [ -z "$DUP_DEVS" ] && [ -z "$DUP_MOUNTS" ]; then
    echo "ok"
    exit 0
else
    echo "fail"
    if [ ! -z "$DUP_DEVS" ]; then
        echo "Duplicate devices:"
        echo "$DUP_DEVS"
    else
        echo "Duplicate mount points:"
        echo "$DUP_MOUNTS"
    fi
    exit 1
fi
| improve this answer | |
0

Complete solution with awk:

awk '/^\/dev\//{ 
         if (devs[$1]++) dups[$1];
         if (data[$2]++) dups[$2] 
     }
     END{ 
         res="Ok"; 
         for (d in dups) { res="Fail\nDuplicates: "; break } 
         print "Result: "res; 
         for (d in dups) print d 
     }' /etc/fstab

The output:

Result: Fail
Duplicates: 
/dev/sdg
/data/sdb
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.