Solved

pass shell variable to perl one liner

Posted on 2014-11-19
10
291 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
Industry Leaders: 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Matching a random pattern with one common character 2 86
AWS- KeepAlived notify script not working 23 112
Perl script to process a .csv file 18 66
AWK: Pythagoras bp script 18 41
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

726 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