Ask Your Question
0

How to grep command line argument on which RegEx has been applied?

asked 2017-09-29 07:42:25 -0500

SomuSysAdmin gravatar image

updated 2018-11-04 04:03:35 -0500

hhlp gravatar image

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-09-29 14:32:41 -0500

sixpack13 gravatar image

updated 2017-09-29 14:35:07 -0500

it's not what you're asking for, but maybe the following generate your wanted output:

lsblk -o NAME,PARTUUID

man lsblk
edit flag offensive delete link more

Question Tools

Stats

Asked: 2017-09-29 07:42:25 -0500

Seen: 142 times

Last updated: Sep 29 '17