Is it possible to read a variable from a file and use in a Sh script

In a file called ifcfg-eth0 there is a line which states HWADDR=XX:XX:XX:XX:XX

I have a script called "gatherdata" and have set a variable called "HWADDR" and I want the variable to be set to XX:XX:XX:XX:XX

Is there an easy way of doing this? ive had a look on google and have seen the source command but couldn't find a good example to fit my scenario.

Im on CentOS 6.8

Thanks
PJ0302917Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nociSoftware EngineerCommented:
You could use below command sequence:

HWADDR=$( . /etc/sysconfig/network-scripts/ifcfg-eth0 ; echo $HWADDR)
tel2Commented:
Hi PJ,

I think noci's quite elegant solution will work if ifcfg-eth0 contains nothing which causes ifcfg-eth0 not to execute as a script (since he runs it as one).  His solution will also set any other environment variables assigned in the ifcfg-eth0 file.  If that's not good for you, here's an alternative way to do it:

==> ifcfg-eth0 <==
HWADDR=01:23:45:67:89
anything else

==> gatherdata <==
#!/bin/bash
HWADDR=$(perl -ne 'print $1 if /^HWADDR=(..:..:..:..:..)$/' ifcfg-eth0)
echo "HWADDR = '$HWADDR'"

Here's how it runs:
$ ./gatherdata
HWADDR = '01:23:45:67:89'
nociSoftware EngineerCommented:
@tel2,
These network config scripts  used in CentOS/RHEL  etc. only contain variable assignments.
The variables are otherwise used in the ifup/ifdown script in a similar fashion....
Sourced in & used to activate/deactivate interface in a subshell.
So no side effects ;-)

otherwise:
HWADDR=$(  grep HWADDR=  /etc/sysconfig/network-scripts/ifcfg-eth0 |  sed 's/^HWADDR=//' )

HWADDR may not (probably is not) the first line,
Configuration script mostly use  DEVICE=eth0 on the first line.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

tel2Commented:
Thanks noci,
I guess that will be fine then, and I think I was mistaken where I said your "solution will also set any other environment variables assigned in the ifcfg-eth0 file", because the assignments will be done in a $(sub-shell), so will be forgotten immediately.
Nice work!

Question for noci: Is it possible for there to be any spaces (or other strange characters) in the assignments in the ifcfg-eth0 file, which might make it crash?  For example, this would fail:
VARIABLE=123 456
nociSoftware EngineerCommented:
There might be spaces  before variable names, or after the value, as these files are mostly genereated during system setup there mostly are no spaces...
Here is an example:

DEVICE=eth0
TYPE="Ethernet"
ONBOOT="yes"
USERCTL="no"
BOOTPROTO="static"
IPADDR="192.168.1.235"
NETMASK="255.255.255.0"
HWADDR="00:15:5d:01:16:35"

Open in new window


In this one no trailing spaces, usage in ifup:
ifup call ifup-eth which does:
....
CONFIG=${1}

need_config "${CONFIG}"

source_config
....

Open in new window



Source config is somewhat more complex:
source_config ()
{
    CONFIG=${CONFIG##*/}
    DEVNAME=${CONFIG##ifcfg-}
    . /etc/sysconfig/network-scripts/$CONFIG
    [ -r "keys-$DEVNAME" ] && . /etc/sysconfig/network-scripts/keys-$DEVNAME
    case "$TYPE" in
    Ethernet)
        DEVICETYPE="eth"
        ;;
    CIPE)
        DEVICETYPE="cipcb"
        ;;
    IPSEC)
        DEVICETYPE="ipsec"
        ;;
    Modem)
        DEVICETYPE="ppp"
        ;;
    xDSL)
        DEVICETYPE="ppp"
        ;;
    ISDN)
        DEVICETYPE="ippp"
        ;;
    Wireless)
        DEVICETYPE="eth"
        ;;
    "Token Ring")
        DEVICETYPE="eth"
        ;;
    CTC)
        DEVICETYPE="ctc"
        ;;
    GRE | IPIP | IPIP6)
        DEVICETYPE="tunnel"
        ;;
    SIT | sit)
        DEVICETYPE="sit"
        ;;
    InfiniBand | infiniband)
        DEVICETYPE="ib"
        ;;
    OVS*)
        DEVICETYPE="ovs"
        ;;
    esac
    if [ -n "$HWADDR" ]; then
        HWADDR=$(echo $HWADDR | awk '{ print toupper($0) }')
    fi
    if [ -n "$MACADDR" ]; then
        MACADDR=$(echo $MACADDR | awk '{ print toupper($0) }')
    fi
    [ -z "$DEVICE" -a -n "$HWADDR" ] && DEVICE=$(get_device_by_hwaddr $HWADDR)
    [ -z "$DEVICETYPE" ] && DEVICETYPE=$(echo ${DEVICE} | sed "s/[0-9]*$//")
    [ -z "$REALDEVICE" -a -n "$PARENTDEVICE" ] && REALDEVICE=$PARENTDEVICE
    [ -z "$REALDEVICE" ] && REALDEVICE=${DEVICE%%:*}
    [ -z "$SYSCTLDEVICE" ] && SYSCTLDEVICE=${REALDEVICE/.//}
    if [ "${DEVICE}" != "${REALDEVICE}" ]; then
        ISALIAS=yes
    else
        ISALIAS=no
    fi
    if is_nm_running && [ "$REALDEVICE" != "lo" ] ; then
        nm_con_load "$CONFIG"
        if ! is_false $NM_CONTROLLED; then
            UUID=$(get_uuid_by_config $CONFIG)
            [ -n "$UUID" ] && _use_nm=true
        fi
    fi
}

Open in new window

tel2Commented:
Thanks noci.

PJ, is noci's solution working with your ifcfg-eth0 file?
PJ0302917Author Commented:
Hi noci & tel2

Thank you for taking the time to help, really appreciate it

@noci, I've just tried your suggestion. First I had to make ifcfg-eth0 executable.
Then I tried the following

HWADDR=""
HWADDR=$(  /etc/sysconfig/network-scripts/ifcfg-eth0 ; echo $HWADDR)
echo "$HWADDR"

I had to remove the dot in-front of the path name to find the file
I seem to be getting an empty variable as I'm seeing a blank line when I echo "$HWADDR"

Any ideas?
PJ0302917Author Commented:
@ noci

just tried HWADDR=$(  grep HWADDR=  /etc/sysconfig/network-scripts/ifcfg-eth0 |  sed 's/^HWADDR=//' )

and that worked a treat, thank you very much
nociSoftware EngineerCommented:
Eh. you DONT need to set the script executable. is it SHOULD not be executed but read.


you missed the '.'  space   isn't './etc...' but '.' SPACE '/etc/....'
before the script name wich means 'source' .
 (read the file, not execute the file). The FIRST DOES work is might be slightly more effective.

It could be rewritten as:

HWADDR=$( source /etc/sysconfig/network-scripts/ifcfg-eth0 ; echo $HWADDR)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.