Link to home
Start Free TrialLog in
Avatar of Mazdajai
MazdajaiFlag for United States of America

asked on

pass shell variable to perl one liner

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

Avatar of ozo
ozo
Flag of United States of America image

In what way did it not work?
What was the value of $mac1?
Which shell are you using?
Avatar of Mazdajai

ASKER

Hi,
Mac1 has the variable but the replacement didn't happen.
I am trying to replace mac address basically.
SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$Mac1 is different from $mac1, but it looks like you are setting $mac1
Are there lines in eth0test that match /^HWADDR=.+?$/
export should not be necessary in the above shell code
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!
Avatar of Tintin
Tintin

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial