有一个命令 'netcfg' 相当于列出接口(interface)的 'ifconfig'。 “netcfg”命令的输出如下所示
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
我需要在 shell 脚本中运行命令 'netcfg' 并需要检查 ppp0 接口(interface)是否启动。谁能告诉我如何在 linux 的 shell 脚本中做到这一点?
最佳答案
netcfg | grep -c -e '^ppp0\s*UP'
将打印 1 并设置 $?如果 ppp0 为 UP,则为 0。将打印 0 并将设置 $?如果 ppp0 关闭或 netcfg 输出中不存在,则为 1。
更新,在 android 上工作:
$ adb shell
shell@hammerhead:/ $ netcfg | grep -c -e '^lo[[:space:]]*UP'
1
shell@hammerhead:/ $
关于linux - 如何在 shell 脚本中编辑 linux 命令输出字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237985/