Solved

pass shell variable to perl one liner

Posted on 2014-11-19
10
268 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange 2007 3 55
cron job says it ran, no results 25 114
Bash Script-Trigger email when server reboots 11 68
how to pick files up from ftp via script 8 49
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now