[SOLVED] Is there a command that can match a string with regular expression in linux ?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a command that can match a string with regular expression in linux ?
I want to find a command that can judge a string match the specified regular expression or not .
e.g. command "abc486de" "ab*\d*e\b"
Match, return success code
not match , return failed code, and promote why not match.
What is the command? Thanks!
I just want to judge if a string match a regular expression or not. I do not want to find or cut some line by using awk or grep. So "if" command is enough for me. But what's difference between "if [ ] " and "if [[ ]] "
Thanks.
[ is a symlink to the /bin/test command whereas [[ is built in to bash syntax. Using [[ bash will understand the command as a logic comparison but using [ to bash it's just a bunch of characters it passes on like any other command line, expanding variables, causing whtespace issues, null value problems etc. use [[.
but using [ to bash it's just a bunch of characters it passes on like any other command line, expanding variables, causing whtespace issues, null value problems etc. use [[
Sorry, I have never used [[ ]] , and I don't understand
Quote:
Using [[ bash will understand the command as a logic comparison but using [ to bash it's just a bunch of characters
then that would be expanded by bash before evaluation as by the [ / test command as
if [ -z hello I am a string ]
which is nonesense, as each word is taken to be a different parameter passed to [. SO you have to do ugly things like wrap the variable name in quote marks etc.
using [[, the test work is done by bash itself. it doesn't replace $BOB, it *understands* that $BOB is a variable being used as part of a logic test so evaluates it correctly within its own code, behaving much more like a normal programming language.
"[" is a command, a synonym for "test". That means that everything following it is just an argument to the command, like any other.
"[[" is a bash keyword, an internal function that follows special parsing rules. This means that it can do things and handle certain kinds of syntax that would choke the older command, such as regular expressions.
If you really want to learn things like this, start by reading this guide. It will give you all the basic concepts you need to know. http://mywiki.wooledge.org/BashGuide
Sorry , I had studied http://mywiki.wooledge.org/BashFAQ/031, I understood now.
But "&&" after ]] seems act for "if then", "||" act for "else then", not the meaning of "and" "or" .
If x evaluates as true, then also evaluate y. If y is also true, then the whole expression is true. If either one evaluates as false, then the whole expression is false. So both xandy must be true for the test to succeed.
Code:
[[ x || y ]]
If x evaluates as false, then also evaluate y. If y is also false, then the whole expression is false. If either one evaluates as true, then the whole expression is true. So either xory must be true for the test to succeed.
The behavior of the operators outside of the test construct is similar. It's not entirely equal to if-then-else, but it can usually be used as a shorthand for it.
Code:
x && y
If the exit code of command x (any command, not just test) is 0 (success), then also run command y. Otherwise skip it.
Code:
x || y
If the exit code of command x is >0 (failure) then also run command y. Otherwise skip it.
Code:
x && y || z
If x exits successfully, then run command y. Then if y exits successfully, skip z.
But if x fails, skip y. Then since the exit code of x is still >0, run z.
However, and this is the big difference, if x succeeds, and then yfails, then z will also run.
The thing to remember is that these two operators always base their actions on the status of the exit code of the command that preceded it, without consideration of the expression as a whole. && continues if the previous command succeeded, and || continues if the previous command failed.
As the link above points out, if you want to be completely safe, use a full if-then-else instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.