Link to home
Start Free TrialLog in
Avatar of PJ0302917
PJ0302917

asked on

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
Avatar of noci
noci

You could use below command sequence:

HWADDR=$( . /etc/sysconfig/network-scripts/ifcfg-eth0 ; echo $HWADDR)
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'
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PJ0302917

ASKER

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?
@ noci

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

and that worked a treat, thank you very much
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)