2

I am trying to detect a NIC is configured or not. (On Ubuntu 1604, so the main configuration file will be /etc/network/interfaces).

I prepared a regex to search the configure from the interfaces file like below:

^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+eth0 

This regex works when I put it in grep command directly; but if I put this regex into a variable then used it in grep, then grep will throw error:

grep: Invalid regular expression 

Can you please help to figure out why put that it does not work that put regex into a variable?

Thanks!

root@ci-1-0:/home/lisa# mainfn=/etc/network/interfaces root@ci-1-0:/home/lisa# nic_name=eth0 root@ci-1-0:/home/lisa# pattern="^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+$nic_name" root@ci-1-0:/home/lisa# echo $pattern ^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+eth0 root@ci-1-0:/home/lisa# grep -E "^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+eth0" $mainfn auto eth0 iface eth0 inet dhcp root@ci-1-0:/home/lisa# grep -E $pattern $mainfn grep: Invalid regular expression 
4
  • 2
    Why did you remove the quotes in the second version? Commented Jul 5, 2017 at 22:16
  • 1
    I'm not sure if this would be the cause, but when you are putting it directly into grep -E, you have the pattern surrounded by quotes, whereas when using $pattern it does not have the quotes. Edit: I just tried it and I don't get a syntax error either way, but try grep -E "$pattern" $mainfn Commented Jul 5, 2017 at 22:17
  • When the quotes are missing, you are looking for the (invalid) regex ^[ in a file named \t]*.... Keep the quotes. Commented Jul 5, 2017 at 22:34
  • The default rule in bash ... until you know better use double quotes around ALL variables Commented Jul 6, 2017 at 3:41

1 Answer 1

1

On my OS Sierra terminal, your example gives a slightly different error message:

> grep -E $pattern filename grep: brackets ([ ]) not balanced 

But if I wrap the pattern in quotes, it works fine:

> grep -E "$pattern" filename auto eth0 iface eth0 inet dhcp 

The issue is that without quotes, the space in your pattern is being interpreted as a separator between arguments:

grep -E ^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+eth0 filename ^ HERE ^ (and also here) 

In other words, it is trying to search for an invalid regex ^[ (with unbalanced brackets, hence my error message), in three files: "\t]*(auto|iface|mapping|allow-.*)[", "\t]+eth0" (which obviously do not exist), and "filename".


As a general rule in bash, it's a good idea to wrap any use of variables in quotes like this, to avoid such whitespace issues.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.