【问题标题】:Bash: grep result of command using pattern from a different command?Bash:使用来自不同命令的模式的命令的 grep 结果?
【发布时间】:2014-07-14 20:10:04
【问题描述】:

我正在尝试使用来自 mysql 命令的模式对命令 df -h 进行 grep。现在我有这样的东西:

df -hP | grep $(mysql -uroot -e "select statement")

现在这是尝试 grep mysql 查询的结果,而不是使用结果作为模式来 grep df 结果

mysql语句的结果是“raid_48”

然后我想将它通过管道传输到 mailx。也许我不应该尝试用一个班轮来做到这一点

【问题讨论】:

  • 那么,有什么问题呢?
  • 您能否根据返回的内容添加$(mysql -uroot -e "select statement") 的输出,这可能会破坏grep 语法。
  • 如果grep 试图将raid_48 处理为文件而不是表达式,请尝试按照我的回答中的建议添加-e

标签: linux bash shell grep


【解决方案1】:

如果您的命令可能有问题,您可以考虑以下几点:

# Add `-e` before the argument to explicitly tell grep that the argument that follows is an expression not an option.
# Quote your argument to prevent word splitting with spaces.
df -hP | grep -e "$(mysql -uroot -e "select statement")"

# Perhaps try using fgrep as well to prevent reading argument as regex.
df -hP | fgrep -e "$(mysql -uroot -e "select statement")"

【讨论】:

    猜你喜欢
    • 2012-05-04
    • 2021-12-29
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2021-10-30
    • 2019-03-23
    • 2014-06-11
    • 2013-01-28
    相关资源
    最近更新 更多