Shell script in the case of the if conditional regular expression =~ quotation mark problem

Source: Internet
Author: User

Today, when you run the If judgment in the script, you always get into the corresponding branch and check the regular expression without errors. In the shell alone to execute or show no match. Rather strange, it was searched, it was found in the =~ after the regular expression can not be added to the quotation marks, and the point to represent any character, the last is not add (.) * To match the end of the answer.

Here are some of the information that you have searched for:

A test that matches the IP address, and the result does not match:

Newip=' 192.168.1.1 '"$newip]];' Found the IP address 'fi       

I have searched the Chinese data in Google and found no problem. Finally, in English, I searched for an article in the "Advanced Bash-scripting Guide" that introduced the Bash version, which wrote:

The =~ Regular Expression match operator no longer requires quoting
of the pattern within [[...]].

Replace the test conditions with the following so that it succeeds.

"$newip=~ ^([0-9]{1,3}.) {3}[0-9]{1,3}];  Then .... fi            

When using variables, you can use quotation marks:

Newip=' 192.168.1.100 'reg=' ^ ([0-9]{1,3}.) {3} [0-9] {1,3}$ '$newip]];' Found the IP address 'fi          

However, for $reg this variable, in the [[]] operator, you cannot add double quotes, otherwise it will not match.

Shell script in the case of the if conditional regular expression =~ quotation mark problem

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.