1

I have written script on bash. It got all list of interfaces. And in loop has compared with variable. But it doesn't work good. My script:

#!/bin/bash

search=`ls -1 /sys/class/net | grep -v lo`
echo $search

eth=eno1
echo $eth

for i in $search; do
if [ $i == $eth ]; then
echo "You choose is: $i"
else
echo "not found"
fi
done

In result i have had:

dummy0 eno1 enp4s0 virbr0 virbr0-nic
eno1
not found
You choose is: eno1
not found
not found
not found

It is ok. I want, to get one message "You choose is: eno1". If $eth does not contains in "/sys/class/net", i want to get message "not found". Where i have mistake ? Please help. Thank you.

1

replace the whole loop with one if statement like this:

if [[ "$eth" =~ "$search" ]];then
    echo "You choose is: $eth"
else
    echo "not found"
fi

And the whole script in a much compact way:

#!/bin/bash
eth=eno1
if [ -d /sys/class/net/$eth ];then
    echo "You choose is: $eth"
else
    echo "not found"
fi
  • i am sorry, but it doesn't work. i have in result: dummy0 eno1 enp4s0 virbr0 virbr0-nic eno1 You choose is: dummy0 You choose is: eno1 You choose is: enp4s0 You choose is: virbr0 You choose is: virbr0-nic I need to get one result – Piduna Valeriu Nov 15 '16 at 11:44
  • sorry, i've swapped your variables. corrected. – Ipor Sircer Nov 15 '16 at 11:46
  • i have in all interation this output: + [[ eno1 =~ dummy0 eno1 enp4s0 virbr0 virbr0-nic ]] + echo 'not found' not found – Piduna Valeriu Nov 15 '16 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.