I'm trying to get a list of devices on my linux box from normal bash commands in a script. I've used the following however it isn't working. Ideally, I'd like to get all sd* and nvme devices into an array. This is what I have tried:
lsblk --nodeps | sed -n '/sd\.*/p'
lsblk --nodeps | sed '/(sd | nv)\.*/p'
lsblk --nodeps | sed -n 's/^([a-z0-9]+?).*/\1/'
lsblk --nodeps | sed -n '/^.*?/p'
lsblk --nodeps | sed '/^(.+?)\s*/p'
lsblk --nodeps | sed '/^(s.+?|n.+?)?)\s*/p'
This is the starting syntax:
$ lsblk --nodeps
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 119.2G 0 disk
sdb 8:16 1 57.8G 0 disk
nvme0n1 259:0 0 232.9G 0 disk
So I want to regex for sd* and nvme, without the trailing spaces.
Any insight appreciated how I can get drives into an array.
/dev? You don't needlsblkfor the job at all.shopt -s nullglob; cd /dev; disks=( sd* nvme* )and there you are, you get an array nameddiskswith, in your case, three entries.lsblk, you can do something likereadarray -t disks < <(lsblk -nr --output NAME | egrep '^(sd|nvme)'). No reason to fight to parse a table when you can just ask for a format that's easier-to-parse than a table.sedcould parse and return items.