0

There is a command 'netcfg' which is equivalent to 'ifconfig' which list out the interfaces. The output of the 'netcfg' command is something like below

lo       up     127.0.0.1/8 0x0000049 23:45:12:11:43:54
dummy0   DOWN   0.0.0.0     0x0000078 11:22:44:55:21:98
p2p0     UP     0.0.0.0     0x0000078 11:22:44:55:21:98
ppp0     UP     192.168.3.4 0x0000054 34:54:88:94:22:FF

I need to run the command 'netcfg' in the shell script and need to check if ppp0 interface is up. Can anybody please give some idea how to do it in shell script in linux?enter image description here

2 Answers 2

2

netcfg | grep -c -e '^ppp0\s*UP' will print 1 and will set $? to 0 if the ppp0 is UP. will print 0 and and will set $? to 1 if ppp0 is DOWN or not present in the netcfg output.

update, working on android:

$ adb shell
shell@hammerhead:/ $ netcfg | grep -c -e '^lo[[:space:]]*UP'
1
shell@hammerhead:/ $
Sign up to request clarification or add additional context in comments.

8 Comments

I like your answer better. :)
For scripts, grep -q is probably more useful than grep -c.
Please find the attached screeshot
Looks like you are running on android. I bet your grep is not as functional as expected -- perhaps not recognizing \s, maybe [[:space:]] would work instead?
@SurjyaNarayanaPadhi we are all new to something at some point, doesn't really mean we all skip doing our homework. your problem of "not working for me" is just a matter of syntax. you could google it out in no time just by "grep" "expression" "spaces". I wonder if you have been waiting 7 hours to get the answer.
|
1
X=`netcfg | grep ppp0 | awk '{print $2}'`
if [ "$X" == "UP" ]; then
   echo "ppp0 is UP!"
fi

2 Comments

You have a useless use of grep there. netcfg | awk '/ppp0/ { print $2 }'
Or maybe say netcfg | awk '/ppp0/ { if ($2 == "UP") print "y"; else print "n"; }'.

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.