Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help returning active interface? 2

Status
Not open for further replies.

rigstars1

Instructor
Oct 2, 2010
19
US
The script searches for active interfaces and returns the correct value except I want to also add a statement in there that returns "No active interfaces found" if there are truly no active interfaces.. having trouble adding an if/else statement in here? Any suggestions?

#!/bin/sh

for iface in `ifconfig -lu` ; do
case $iface in
lo*) continue ;;
esac
ifconfig $iface | grep -q 'inet ' && echo $iface
done
 
I am not familiar with the ifconfig command, but I am going to assume that if nothing prints by the time the loop ends, there was nothing to print.

So try this:

#!/bin/sh

found="N"

for iface in `ifconfig -lu` ; do
case $iface in
lo*) continue ;;
esac
ifconfig $iface | grep -q 'inet ' && echo $iface
FOUND="Y"
done

if [ "$found" = "N" ]; then
echo "No active interfaces found"
fi


Using the Korn shell I would change the script to this:

#!/bin/ksh

found="N"

for iface in `ifconfig -lu` ; do
if [[ "$iface" != lo* ]]; then
ifconfig $iface | grep -q 'inet ' && echo $iface
found="Y"
fi
done

[ "$found" = "N" ] && echo "No active interfaces found"


I am not sure, but the Bourne shell may not support the double square brackets in the "if" condition. As well, the "&&" operator may not be supported either. You can try to confirm.
 
dkyrtata,

Thanks for the quick reply. The bash one almost works as expected ..when it finds the active interface ..it prints both en1 and No active interfaces found ..like this

en1
No active interfaces found

Can you please correct that?
 
I am not familiar with the Bash shell. However a Google search shows that Bash uses the "==" operator. Try it like this:

[ "$found" == "N" ] && echo "No active interfaces found"

or use a conventional "if" block like this:

if [ "$found" == "N" ] ; then
echo "No active interfaces found"
fi

Don't forget that the square brackets and the "==" operator must be surrounded by at least one space (as shown)
 
I already tried that. still produces the same result. I tried putting an 'exit' block after it finds the interface so that it doesn't go to the next line of code "No active interfaces found" but it if I do that, this never gets checked

if [ "$found" = "N" ]; then
echo "No active interfaces found"

Is there any other way to escape the program once it finds the interface? Doesn't look like the 'goto' doesn't exist for bash scripts. The problem is this doesn't exit the program after finding the active interface

ifconfig $iface | grep -q 'inet ' && echo $iface
FOUND="Y"
done
 
Make sure the "en1" line of output is coming from the ifconfig line and not another part of your script. Try commenting out that line.

echo the value of $found at the end of your script. It appears that it is still set to "N". If so, make sure the assignment of "Y" is being done correctly. There must be no spaces to the left and right side of the "=" sign of found="Y"

If the value of $found is still "Y", examine the syntax of the if condition.

No there are no GOTO statements in shell scripting. It would not help you anyway. You need to pinpoint which statement is not executing as expected.
 
FYI, found and FOUND are different variables ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
whether 'found' is upper or lower case ..the script executes the same ..i made lower case and it still produces the same result ..both statements are written when an active interface is found .. like so ..which is not the desired results

en1
No active interfaces found
 
Why not posting your actual code exhibiting this ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
# here is the script that dkyrtata provided to me

found="N"

for iface in `ifconfig -lu` ; do
if [[ "$iface" != lo* ]]; then
ifconfig $iface | grep -q 'inet ' && echo $iface
found="Y"
done
fi

if [ "$found"=="N" ] ; then
echo "???"
fi

# below is my changes to try to fix it .. no avail

found="N"

for iface in `ifconfig -lu` ; do
if [[ "$iface" != lo* ]]; then
ifconfig $iface | grep -q 'inet ' && echo $iface
found="Y"
else
found="N"
echo "No active interface found"
fi
done
 
The script that I provided has the "fi" statement before the "done". Yours would generate a syntax error and proceed to the next line where $found would still be "N"

Your else block that you added to your version of the script could change $found back to "N" after it has been changed to "Y".

You must let the while-loop complete and then check if anything had printed inside it (i.e. only when $found is set to "Y" in the loop)

If nothing had printed (i.e when $found is still "N") then you can print your "not found" message.

 
sorry dkyrtata ..here's your original script .. I think there is confusion ..
the output of the script is right if there are no active interfaces found. The issue here is when it does find an active interfaces, it executes both statements ..printing
like so .. no matter what I do to modify it ..it won't just print the active interface only, it prints out both statements ..which is not what its suppose to do.

//wrong output
en1
No active interfaces found

----------------------------------------------------

found="N"

for iface in `ifconfig -lu` ; do
if [[ "$iface" != lo* ]]; then
ifconfig $iface | grep -q 'inet ' && echo $iface
found="Y"
fi
done

[ "$found" = "N" ] && echo "No active interfaces found
 
If you are using the Bash shell as you said you were, you need to use "==" in the condition:

[ "$found" == "N" ] && echo "No active interfaces found"

What is the value of $found? Add this after the last line:

echo "found=$found
 
when I ran this script with the following changes .. I get this output

en1
found=Y

I can't get it to NOT print the "found=Y" statement. I tried to put a 'break' after the found="Y" but it still echos the found="N"
----------------------------------------
found="N"

for iface in `ifconfig -lu` ; do
if [[ "$iface" != lo* ]]; then
ifconfig $iface | grep -q 'inet ' && echo $iface
found="Y"
fi
done

found="N"
[ "$found" == "N" ] && echo "No active interfaces found"
echo "found=$found"

------------------------------------
but if I purposely disable the interface ..the output of the script is correct.

found=N
 
CORRECTION:

I can't get it to NOT print the "found=N" statement.
 
What about this ?
Code:
found='N'
for iface in `ifconfig -lu` ; do
  if [[ "$iface" != lo* ]]; then
    ifconfig $iface | grep -q 'inet ' && echo $iface && found='Y'
  fi
done
case $found in N) echo 'No active interfaces found';; esac

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If $found is still "N" there is something else wrong. The "not found" message would be correct. The problem is somewhere in the loop.

What did you get with PHV's changes?

Can you include the output of the "ifconfig -lu" command in your next reply?
 
PVH,

The add-in of the case statement made it work. Thanks for that!!!

Dkyrtata,

Appreciate your help and for not giving up on the assistance!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top