?
Solved

pass shell variable to perl one liner

Posted on 2014-11-19
10
Medium Priority
?
314 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1600 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 1600 total points
ID: 40456353
perl -mYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new($_)->explain for qw/^HWADDR=.*/,qw/^HWADDR=.+?$/'
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

764 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