1
for interfacefile in `ls /etc/sysconfig/network-scripts/ifcfg-eth*`

I have following for loop in my shell script. now

ls /etc/sysconfig/network-scripts/ifcfg-eth*
/etc/sysconfig/network-scripts/ifcfg-eth0  /etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/ifcfg-eth0.0  /etc/sysconfig/network-scripts/ifcfg-eth1:1

the problem is I want to grep only normal Ethernets... not like ifcfg-eth0.0 and ifcfg-eth1:1 this type of comma as well as DOT separated strings.

1

3 Answers 3

1

Wouldn't something like ls /etc/sysconfig/network-scripts/ifcfg-eth[0-9] suffice?

Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest ifcfg-eth[0-9] instead. Otherwise it loops through all numbers. With [ ] it just looks for files within this pattern.
0

You can do it like this:

# enable extender pattern matching
shopt -s extglob

for interfaceFile in /etc/sysconfig/network-scripts/ifcfg-eth+([0-9])

Note that you don't need ls here. The shell will expand the filenames.

Comments

0

A portable way for POSIX shells:

for if in /etc/sysconfig/network-scripts/ifcfg-eth*; do
    [ -e "${if}" ] || continue
    if=${if##*/ifcfg-}
    case ${if#eth} in
        *[!0-9]*) continue ;;
    esac
    echo "${if}"
done

This will give you just the interface names (e.g. eth0, eth1, ...), if you are really after the full paths to the config files adapt the variable names accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.