Solved

pass shell variable to perl one liner

Posted on 2014-11-19
10
304 Views
Last Modified: 2014-11-26
I am trying to pass shell variable to a perl one liner but it didn't work.

Am I missing something here?

export mac1
mac1=`ifconfig|grep HWaddr|gawk '{print $5}'`
eth0_input=/etc/sysconfig/network-scripts/ifcfg-eth0
eth0_input=eth0test
perl -s -p -i -e "s/^HWADDR=.+?$/HWADDR=$mac1/g" $eth0_input

Open in new window

0
Comment
Question by:Mazdajai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40453907
In what way did it not work?
What was the value of $mac1?
Which shell are you using?
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 40453918
Hi,
Mac1 has the variable but the replacement didn't happen.
I am trying to replace mac address basically.
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 100 total points
ID: 40453928
You should first fill the variable, then export it.

Or use "export mac1=`ifconfig ... ... `"

EDIT: Please take the above just as a general hint! I just saw that in your case it should work as is.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 40453930
$Mac1 is different from $mac1, but it looks like you are setting $mac1
Are there lines in eth0test that match /^HWADDR=.+?$/
0
 
LVL 84

Expert Comment

by:ozo
ID: 40453937
export should not be necessary in the above shell code
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 40453951
Was typing on my cell phone - it is mac1.
There is a line begins with the match:
HWADDR=
Will try again when I have a chance!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40453959
No real need for perl.

mac=$(ifconfig eth0|awk '/HWaddr/ {print $NF}')
sed -i "s/^HWADDR=.*/HWADDR=$mac/" /etc/sysconfig/ifcfg-eth0

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 40453970
Note that /^HWADDR=.*/ can match where /^HWADDR=.+?$/ may not, and /etc/sysconfig/ifcfg-eth0 may not be the same as eth0test
Otherwise, there should be no difference between the sed method and the perl method
0
 
LVL 21

Author Comment

by:Mazdajai
ID: 40455594
Looks like it was the regex, I thought .+? (non-greedy) is the same as .* (greedy) -  Is there a tool to troubleshoot regex / matching in perl?
NO
HWADDR= 

Open in new window

YES
HWADDR=aa:bb:cc:dd::ee 

Open in new window


Tintin,
Yes sed works but I am more familiar with the regex engine in Perl. I have seen issue with the regex support in GNU tools.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 40456353
perl -mYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new($_)->explain for qw/^HWADDR=.*/,qw/^HWADDR=.+?$/'
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question