Technical Discussion
  >> Linux Issues


Register (or login) on our website and you will not see this ad.


  Print Thread
Standard User metalhead41
(knowledge is power) Sat 16-Mar-13 23:51:50
Print Post

Script help


[link to this post]
 
I've been trying to learn some bash scripting and am having a little trouble trying to pass an awk into a variable...
This what works if I run the command locally:
Text
1
HW=$(ifconfig -a | grep eth1 | awk '{ print $5 }')

That will create the variable HW and stores the MAC address in there.


Now, if I put this into a script to run on a remote machine via ssh, it doesn't work, I think it is an escape character issue, however, if I don't pass it to the variable, it displays the MAC address as expected.
This is what I have in my script at the moment for it to work (without passing to the variable):
Text
1
23
4
read -p "Enter Host Name: " HOST
echo $HOSTssh $HOST "ifconfig -a | grep eth1 | awk '{ print \$5 }'"
exit


And this is what it looks like when trying to pass to the variable
Text
1
23
45
read -p "Enter Host Name: " HOST
echo $HOSTssh $HOST "HW=\$(ifconfig -a | grep eth1 | awk '{ print \$5 }')"
echo $HWexit


Any ideas on where I've gone wrong and what I need to do to get it to work? I'm sure it's something simple...

Cheers

----------------------------------------------------
We may be human But we're still animals

My Blog | follow me @metalhead41 | Surfin' Bird Dance
Standard User mixt
(experienced) Sun 17-Mar-13 00:48:36
Print Post

Re: Script help


[re: metalhead41] [link to this post]
 
Your escaping of \$ is all good, but you're tripping up on the fact that variables set on a remote ssh bash fork, on an external server, are not transfered back over to your local shell.

So the following is never going to work, as the value of HW is on the remote computer, and is lost once the SSH session has finished:

Text
1
2
ssh $HOST "HW=\$(ifconfig -a | grep eth1 | awk '{ print \$5 }')"
echo $HW


However, this will work:

Text
1
2
HW=$(ssh $HOST "ifconfig -a | grep eth1 | awk '{ print \$5 }'")
echo $HW


I trust you appreciate the difference.

Now on <aaisp.net> (21CN+IPv6)
Previous ISPs: Virgin Media (50Mb/Cable), Be* Un Limited, ZeN
Is Linux routing your internet connection?
Need to make BIND geo-aware?
Standard User metalhead41
(knowledge is power) Mon 18-Mar-13 10:00:21
Print Post

Re: Script help


[re: metalhead41] [link to this post]
 
Thanks, that explains why it wasn't displaying smile
I've gone back to my original code and expanded it a bit more so it now replaces the MAC address in a file with the correct one, then sets up SSH on the remote machine and copies certain files over from a deployment server smile

Just to point out, I could have done this all manually and saved myself time, but this was more of an exercise to learn some bash scripting, so I'll be expanding it further as and when I can think of things to do just to make it better and more efficient (I've already managed to setup a PXE server that can build VM's without interaction from me).

----------------------------------------------------
We may be human But we're still animals

My Blog | follow me @metalhead41 | Surfin' Bird Dance


Register (or login) on our website and you will not see this ad.

  Print Thread

Jump to