This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/")
.. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise.
On 19 Jun 10:27, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/")
.. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise.
How about:
mac=$(ip link show dev eth0 | sed -e '/link/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;') echo "This is my MAC: ${mac//:}"
Done.
(works in bash, won't work in dash)
On 19/06/12 11:42, Brett Parker wrote:
How about: mac=$(ip link show dev eth0 | sed -e '/link/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;')
Thanks, nice to have an alternative that makes better use of sed and loses grep.
What is the meaning/reasoning behind "[ ][ ]*"? My interpretation of it is just "one or more spaces", is that correct? Is "[ ]" just a style preference to make it easier to read than " *", or am I missing something?
Other than that I have managed to parse it in my head after a couple of attempts! At the moment I've adapted it to: mac=$(ip link show dev eth0 | sed -e '/link/ether/ { s# *link/ether *##; s# .*$##; p }; d;')
.. as I don't see "zero-or-more spaces" being too general, but that makes assumptions about my understanding of your version....
echo "This is my MAC: ${mac//:}"
The problem with this is that it puts the "template" at the point in the code where it's used, where I want to be able to define the template in the user-config part of the script at the top, and
# Config section of script template="This is my MAC: ${mac//:}"
# Main script, do not edit # Lots of stuff in here..... mac=$(ip link show dev eth0 | sed -e '/link/ether/ { s#[ ][ ]*link/ether[ ][ ]*##; s# .*$##; p }; d;') echo $template
doesn't work as the variable expansion happens before it is set.
Mark
On Tuesday 19 June 2012 10:27:59 Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
Also, if I have something like: echotemplate="This is my MAC: (MAC)" mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g') echo $(echo $echotemplate | sed -e "s/(MAC)/$mac/")
.. is there a better way to do the substitution in the last line? I can't swap the top two lines around (one is a user-defined setting at the top of the script, the value of $mac is calculated in the body of the script), otherwise I would just use echo "This is my MAC: $mac" .. which would be nice and easy! But I'm using this as a learning exercise.
How about:
/sbin/ifconfig eth0 | grep HWaddr | cut -d\ -f11
On 19/06/12 11:58, Stuart Bailey wrote:
How about: /sbin/ifconfig eth0 | grep HWaddr | cut -d\ -f11
Another neat approach, thanks.
Mark
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address
Adam
On Tue, 19 Jun 2012 22:16:55 +0100 Adam Bower adam@thebowery.co.uk allegedly wrote:
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address
Adam
Cool.
I looked around /proc thinking I might find something like that. And of course got nowhere.
That is useful.
Mick
--------------------------------------------------------------------- blog: baldric.net fingerprint: E8D2 8882 F7AE DEB7 B2AA 9407 B9EA 82CC 1092 7423 ---------------------------------------------------------------------
On 19 Jun 22:16, Adam Bower wrote:
On Tue, Jun 19, 2012 at 10:27:59AM +0100, Mark Rogers wrote:
This works: mac=$(ifconfig eth0 | grep -Eo '..:..:..:..:..:..' | sed -e 's/://g')
.. but I'm sure there's a better way. Suggestions?
cat /sys/class/net/eth0/address
Surely, you mean...
mac=$(</sys/class/net/eth0/address)
That does rely on the sys filesystem though...
On 20/06/12 09:49, Brett Parker wrote:
Surely, you mean... mac=$(</sys/class/net/eth0/address)
Thanks Brett and Adam, that's much nicer.
Can I combine that with the removal of ":" characters in a single command?
That does rely on the sys filesystem though...
Is that ever likely to be an issue on a Debian-based install? The boxes I'm playing with are mostly ARM processors (my current tests are on a Pi) running Debian 6, although I'll need things to also work on Ubuntu Server installs too.
Mark
On 20 Jun 11:29, Mark Rogers wrote:
On 20/06/12 09:49, Brett Parker wrote:
Surely, you mean... mac=$(</sys/class/net/eth0/address)
Thanks Brett and Adam, that's much nicer.
Can I combine that with the removal of ":" characters in a single command?
That does rely on the sys filesystem though...
Is that ever likely to be an issue on a Debian-based install? The boxes I'm playing with are mostly ARM processors (my current tests are on a Pi) running Debian 6, although I'll need things to also work on Ubuntu Server installs too.
Unlikely on anything even remotely modern to be a problem.
Thought you needed it both with and without the colons?
mac=$(OLDIFS="$IFS";IFS=":"; for thing in $(<sys/class/net/eth0/address); do echo -n $thing; done)
Not, erm, pretty - but functional.