Reputation: 1094
I'm trying to create a script that automatically looks for plugged in devices and makes a compressed backup of it. However, I'm having trouble finding the correct way on how to use expr
:
#!/bin/bash
MountText=`mount`
# Show result of regex search
expr "$MountText" : '\/dev\/(sd[^a])\d on [\S]+\/[\s\S]+? type'
The expression by itself is \/dev\/(sd[^a])\d on [\S]+\/[\s\S]+? type
, and captures the device name (sd*), while excluding mounts relating to sda.
I drafted the regex on Regexr (regex shared in link), and used what mount
dumped (gist).
For some reason, it only gives this odd error:
0
I looked around, and I found this SO question. It didn't help me too much, because now it's implying that expr
didn't recognize the parentheses I used to capture the device, and it also believed that the expression didn't capture anything!
I'm really confused. What am I doing wrong?
Upvotes: 0
Views: 1218
Reputation: 532053
A few things to note with expr
:
^
).\(...\)
.\s
, \S
, and +?
are not supported.The following will match the one device.
expr "$MountText" : '.*/dev/\(sd[^a]\)[[:digit:]] on '
Note that you don't need to use expr
with bash
, which has regular-expression matching built in.
regex='/dev/(sd[^a])[[:digit:]] on '
mount | while IFS= read -r line; do
[[ $line =~ $regex ]] && echo ${BASH_REMATCH[1]}
done
Upvotes: 1