linux - 如何在 shell 脚本中编辑 linux 命令输出字符串?

标签 linux sh

有一个命令 '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 脚本中做到这一点? enter image description here

最佳答案

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/

相关文章:

linux - 执行远程登录并执行另一个本地脚本的脚本

从 cronjob 运行时,PHP 没有读取或写入文件的权限

linux - 如何使用 bash 脚本计算一个单词中出现次数最多的 3 个字母序列

bash/sh 嵌套转义序列

bash - 在 bash heredoc 中使用变量

python - 将 py 和 sh 代码合并到一个文件中

python - Ckan 安装失败,出现 : Unknown distribution option: 'message_extractors'

linux - 运行带有 --quiet 选项标志的 rpmbuild 命令会产生大量调试信息

shell - 将 shell 转义的参数字符串传递给 Bourne shell 中的子命令

linux - 使用 nginx 提供 1GB 文件的最佳文件系统,在适度的写入、读取性能方面?