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
grep -E, you have the pattern surrounded by quotes, whereas when using$patternit does not have the quotes. Edit: I just tried it and I don't get a syntax error either way, but trygrep -E "$pattern" $mainfn^[in a file named\t]*.... Keep the quotes.