[[ $(/bin/mount) =~ /dev ]] && echo YES
That works, but this doesn't.
[[ $(/bin/mount) =~ ^/dev ]] && echo YES
Nor this:
[[ $(/bin/mount) =~ ^/dev ]] && echo YES
But this does:
[[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES
--
Alan Gutierrez - http://twitter.com/bigeasy - http://github.com/bigeasy
Dies ist freie Software. Sie darf verᅵndert und verteilt werden.
Fᅵr dieses Programm besteht keinerlei Garantie.
bash-4.1$ [[ $(/bin/mount) =~ ^/dev ]] && echo YES
YES
bash-4.1$
>
> Nor this:
>
> [[ $(/bin/mount) =~ ^/dev ]] && echo YES
Where's the difference to the one before?
>
> But this does:
>
> [[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES
>
works as well for me (but this matches by the way another string)
bash-4.1$ [[ $(/bin/mount) =~ [[:space:]]/dev ]] && echo YES
YES
bash-4.1$
> --
> Alan Gutierrez - http://twitter.com/bigeasy - http://github.com/bigeasy
Alternatively you could use (yes, one more process...):
/bin/mount | egrep -q '^/dev' && echo Yes
--
Michael
What version of Bash?
Does your version of mount output a whitespace character before "/
dev"?
Does this work for you?:
[[ $(/bin/mount) =~ ^[[:space:]]*/dev ]] && echo YES
This, using globs instead of regexes, should also work:
[[ $(/bin/mount) = /dev* ]] && echo YES
or some variation.
I was cutting and pasting too fast. I meant to say:
Nor this:
[[ $(/bin/mount) =~ \n/dev ]]&& echo YES
>> But this does:
>>
>> [[ $(/bin/mount) =~ [[:space:]]/dev ]]&& echo YES
>>
> works as well for me (but this matches by the way another string)
>
> bash-4.1$ [[ $(/bin/mount) =~ [[:space:]]/dev ]]&& echo YES
> YES
> bash-4.1$
> Alternatively you could use (yes, one more process...):
> /bin/mount | egrep -q '^/dev'&& echo Yes
Yes. That's what I did for now. Trying to learn the ins and outs of the
built-in regular expressions.
--
Alan Gutierrez
You probably have a /dev at the very start of your mount output. I
don't. I'm trying to have ^ match the start of line, or else explicitly
match a newline or start of line. This would be fine.
[[ $(/bin/mount) =~ ^/dev|\n/dev ]] && echo YES
[[ $(echo -e "foo\nbar") =~ "^bar|
bar" ]] && echo Yes
i found out that it does not work (I'm not sure myself, why this does
not work.) What however works is the following idea:
[[ $(echo -e "foo\nbar") =~ $(echo -e "^bar|\nbar") ]] && echo Yes
And echo -e "foo\nfoobar" correctly does not match. So this is one
solution for you using only shell builtins.
At the moment i can't think of another way to get this newline into your
pattern.
Have a nice day,
Michael