title: I'm trying to get the MAC address as a variable in Linux, but it rarely works
body: <p>I'm using the following code to get the MAC address of eth0 into a variable for use in a filename, but it rarely every works.  It isn't that it NEVER works, it is just unpredictable.</p>
  
  <pre><code>ntpdate -b 0.centos.pool.ntp.org
  DATE=$(date +%s)
  MAC=$(ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | sed 's/://g')
  cp logfile logfilecp-$MAC-$DATE
  </code></pre>
  
  <p>Now, it seems to work less-frequently if I use the ntpdate line, but regardless, it is wholly unpredictable.  Anyone have any idea what I could do to make this work better?  I end up with a filename like</p>
  
  <pre><code>logfile--1375195808.bz2
  </code></pre>
  
  <p><em><strong>New info</em></strong></p>
  
  <p>I've got the script setup to run as a cronjob (crontab -e).  I notice that when it runs as a cronjob, it doesn't get the MAC, but when I run it manually ./runscript.bash it does get the MAC.  Hopefully someone knows why this might be causing it.</p>
  
  <p>Thanks.</p>