LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2007, 07:22 AM   #1
schris403
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Rep: Reputation: 0
env variable mac address script problem


I have been trying for days to try and get the following script to load the mac variable upon boot in the rc.local file and just can't seem to get it to work. It does work when I manually run the line but when I run it as a normal sh script or in rc.local, it doesn't work.

mac=`ifconfig -a eth0 | grep "HWaddr" | cut -d " " -f 11 | tr -s : .`


Maybe there is a different way of accomplishing this, what I want is a variable with my eth0 mac address with . instead of : seperating them. I will be using this on OSX mostly. Any help would be greatly appreciated.

Thanks so much!!

Chris
 
Old 08-01-2007, 08:00 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
I'm assuming it doesn't work because the scope of the variable is limited to the script you are trying to run. Effectively once the script ends any variables that are created in it are destroyed.

I'm not familiar with OS X but is there anyway you can just put that assignment in your .bash_profile?

This tutorial essentially outlines what I have said above. Configuring your OS X Unix Environment

HTH,

Centinul
 
Old 08-01-2007, 08:11 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
some guesses,
maybe the PATH ain't set up right yet?

try using the full path name for ifconfig etc
(all though it sounds unlikely now I write it)

is it DHCP? maybe eth0 isn't up and running yet when it's called?
isn't rc.local where you set up interfaces with wep keys etc.

I would suggest making a script, set -x and call that in rc.local with all output logged to a file (exec > logfile 2>&1)

split it up see what you are getting

Last edited by bigearsbilly; 08-01-2007 at 08:14 AM.
 
Old 08-01-2007, 08:12 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You haven't shown what actually does result, but I'll offer that the script is not finding ifconfig. Try using an absolute path to it, probably '/sbin/ifconfig'. The rest of the logic looks fine to me, although it is very sensitive to the formatting of ifconfig's output.
--- rod.
 
Old 08-01-2007, 08:55 AM   #5
schris403
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Original Poster
Rep: Reputation: 0
It does all indeed work when run manually from a command line, what it outputs is the following example on one line:
00.00.00.00.00.00

It also works with most Linux flavors I have tried it on but the problem goes back to getting it to run as a script or as a startup script to set the variable.

Thanks!
Chris
 
Old 08-01-2007, 09:55 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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:
Code:
source getmac
you should see the $mac variable created in the current shell.
Code:
echo $mac
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.
 
Old 08-01-2007, 02:05 PM   #7
schris403
LQ Newbie
 
Registered: Aug 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks! This does help temendously and everything seems to work except for when I want to install it as a system wide variable using export. I've tried using bashrc to run:
source /etc/eth0_mac.sh

it just doesn't seem to run when it is in bashrc. I also tried running it as a cronjob with no luck. It does work though when run manually at a command line. Any thoughts?

Chris
 
Old 08-01-2007, 03:04 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Check the permissions on the script. Users would need read-execute access. Also, if you used my example, the script would be in /etc/profile.d/, and my /etc/bashrc runs every readable Bourne shell script that it finds there:
Code:
if ! shopt -q login_shell ; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
            if [ -r "$i" ]; then
                . $i
            fi
        done
        unset i
fi
In the installation I'm presently using, all scripts in /etc/profile.d are permission level 755; all owner & group-onwer root.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
driconf on debian Etch fails...env variable problem deepclutch Debian 1 07-22-2006 06:20 PM
script that can pull ip and mac address off the network thefedexguy Linux - Networking 1 11-29-2005 03:10 PM
env variable allelopath Linux - Software 3 04-14-2005 12:07 PM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 02:16 PM
RedHat 7.3 PATH env variable problem NewRedHatter Linux - Newbie 4 09-01-2002 05:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration