【问题标题】:"Command not found" in ifconfig | grep IF statementifconfig 中的“找不到命令”| grep IF 语句
【发布时间】:2020-12-07 05:10:24
【问题描述】:

我正在尝试检查我的以太网适配器是否是硬连线的。为此,我的代码中有以下部分:

if 'ifconfig | grep "status: active" | head -2 | tail -1 | awk {'print$2'} | cut -f1 -d:'; then
  wired_status=True
  echo $wired_status

对于这部分代码,它给出了以下错误:

./tst_wired.sh:第 27 行:ifconfig | grep "状态:活动" |头-2 |尾-1 | awk {打印} | cut -f1 -d:: 找不到命令

在我的终端中键入“ifconfig”会被识别并有效。

【问题讨论】:

  • 谢谢。我也试过了,认为这就是问题所在。但是,将其更改为反引号会产生相同的错误。

标签: grep ifconfig


【解决方案1】:

看起来您使用撇号而不是反引号字符:

` and ' are different

【讨论】:

  • 我发现了问题。 IF 语句正在生成输出,操作系统解释为命令。使用 if [[ ifconfig | grep "status: active" | head -2 | tail -1 | awk {'print$2'} | cut -f1 -d: -eq "active" ]]。解决了这个问题。
  • 这仍然非常冗长。 useless grep 等的曲折迷宫可以重构为 ifconfig | awk '/status: active/ { split(/:/, n, $2) } END { print n[1] }',如果您只关心它是否“活跃”,您可以进一步简化为 ifconfig | awk ¨/status: active/ && $2 ~ /^active:/ { rc=1 } END { if(rc) print "True" }' 并完全摆脱 shell if 语句。跨度>
猜你喜欢
  • 2021-06-21
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 2016-05-27
相关资源
最近更新 更多