Reputation: 147
I am working on a linux bash script that takes a disk partition name as user input. It then needs to check if the drive is using GPT or not, for which I use the following if statement:
# Must be run as root
if [[ $(fdisk -l $DISKNAME | grep -i 'Disklabel type') = "Disklabel type: gpt" ]]; then
echo "Yes, its GPT"
fi
The variable DISKNAME
has to store the name of the disk drive and not the partition. I could just remove the number at the end of the partition name but that is not consistent with SSDs and NVMes. For example NVMe disk name can be /dev/nvme0
and drive name can be /dev/nvme0p1
.
I am searching for a reliable way (not string manipulation) to know the drive name when a partition name is already known.
EDIT: I could also ask the user for the drive name but then there is space for user errors.
Upvotes: 1
Views: 967
Reputation: 21
The PKNAME column from the 'lsblk' command will give you the device name on which the partition resides, e.g.:
lsblk --list --noheadings --paths --output PKNAME /dev/sda2
The '--paths' option ensures that the complete device path (i.e., '/dev/sda' in the example) will be shown, instead of just the device name ('sda').
Upvotes: 2