Okay, I think I understand what's happening, and it goes back to understanding the scope of an environment. When you run the command as a script, typically it will look something like:
Code:
#! /bin/bash
mac=`ifconfig -a eth0 | grep "HWaddr" | cut -d " " -f 11 | tr -s : .`
When executed, a child shell is launched, and the variable $mac is created correctly in that shell (only). When the shell exits, its environment vanishes with it, and all traces of $mac are lost. The variable is not magically transferred up to the parent shell. To get around this, bash and most other shells allow you to '
source' a file; that is it is executed in the context of the current shell process, just as if you were typing it at your keyboard. So, if you put the command string into a file 'getmac', and then source it:
you should see the $mac variable created in the current shell.
This same principle is at play when the script is executed in the
rc.local script. Once that script has run to completion, the $mac variable no longer exists. Moreover, it only ever did exist for the owner of the script, which is root. To cause the variable to be available to all users, you must have
rc.local create a second script which can be
sourced by either
/etc/bashrc or by user-specific
~/.bashrc scripts. This might be done something like this:
(in rc.local, Redhat style)
Code:
mac=`/sbin/ifconfig -a eth0 | grep "HWaddr" | cut -d " " -f 11 | tr -s : .`
echo "export MAC=$mac" > /etc/profile.d/eth0_mac.sh
Now, when
/etc/bashrc executes on each user login, it will source your small script, and create an environment variable '$MAC' containing the MAC address of eth0.
One last point: in bash, the '.' character is shorthand for the command word 'source'. THis notation is more commonly used than the longhand word 'source'.
Hope this is on track.
--- rod.