I'm trying to write a script that takes as input a set of integers representing certain /dev/sda's. For example, if the command line arguments are 3 & 5, the output will show the UUID for /dev/sda3 and /dev/sda5. My code is:
#!/bin/bash
### Shows UUID of input /dev's - REQUIRES SUDO
## Options:
## [-m] Multiple Devs - returns both dev name and UUID
## [ ] No option - returns only the UUID of the dev.
while getopts m: option
do
case "${option}"
in
m) echo -e "\nDEV\tUUID\n====\t================"
blkid | grep .*sda[\"$@\"] | sed -r 's/\/dev\/([[:alnum:]]+).* UUID="([[:alnum:]]+)".*/\1\t\2/g'
;;
esac
done
My primary problem is with the line grep .*sda[\"$@\"] which returns the following error:
$ sudo ./dUShow.sh -m 3 5
DEV UUID
==== ================
grep: Unmatched [ or [^
Now if I'm not mistaken, this means the problem occurs when I'm trying to provide as alternative options *.sda[$@] which I want RegEx to overwrite with *.sda[$1$2], equivalent to *.sda[35] for the given input.
How do I do this?
Sample Input
sudo ./dUShow.sh -m 3 5
Desired output
DEV UUID
==== ================
sda3 BC4208CF42089076
sda5 968E185A8E183569
Addendum
Is the expression .*sda[\"$@\"] going to produce *.sda[35] or *.sda[3 5]? If it is the latter, will it cause a problem? If so, how do I solve it?
seddoesn't needgrepand second, to get the device name and UUID you definitely don't needblkid(nor root access for that matter):man lsblkis your friend... and btw, OR-ing patterns is done via|/dev/sd[a-z][0-9]+and so on...) and then replace that entire nonsense withlsblk -o name,uuid "$@". A typical case of XY question...