0

I am trying to create a shell script that does the following

  1. Checks if a block device has a file system on it.
  2. Mount filesystem.

I have something like this right now

ls -ltrh /dev/vdb
brw-rw---- 1 root disk 254, 16 Dec 15 21:09 /dev/vdb

So /dev/vdb is my block device. And my script is something along the lines of

if TEST-COMMAND-TO-CHECK-IF-VDB-ALREADY-HAS-FILESYSTEM-FORMATTED
   then
     sudo mkfs.ext4 /dev/vdb
   fi 
fi

mount | grep /mntpoint > /dev/null && exit 0 || sudo mount /dev/vdb /mntpoint

I am trying to find out a good command for testing if dev/vdb has filesystem formatted.

thanasisp
  • 8,122
  • Probably you can find many different ideas for how to do this, into these posts: https://unix.stackexchange.com/questions/53542/how-to-determine-the-filesystem-of-an-unmounted-device https://unix.stackexchange.com/questions/43237/find-filesystem-of-an-unmounted-partition-from-a-script https://unix.stackexchange.com/questions/53313/how-to-show-the-filesystem-type-via-the-terminal – thanasisp Dec 15 '20 at 22:04

1 Answers1

0

This seems to do the trick

fs=$(lsblk --output NAME,FSTYPE,LABEL,UUID,MODE |grep vdb |  awk '{print $2}')
if [ $fs == "ext4" ]; 
then
     sudo mkfs.ext4 /dev/vdb
else 
     echo "/dev/vdb is already formatted to ext4"
fi