【问题标题】:Using test inside xargs在 xargs 中使用测试
【发布时间】:2012-02-17 13:55:48
【问题描述】:

我的任务是针对某些条件解析子目录中的文件,然后执行某些操作,因此我有一个可以正常工作的代码,如下所示:

cat FILENAME | grep -qe 'condition'; \
  test $? -ne 0 && echo -ne '\n add condition to file \n'>>FILENAME

但是,当我尝试在 xargs 语句中使用它时,我遇到了一些问题:

git grep -e "condition" | awk -F : 'BEGIN {} { print $1 } END {}'| sort| uniq |\
  xargs -I {} -i bash -c "cp {} {}.updated;  echo -ne 'Processing: {}\n'; \
    cat {}.updated | grep -qe 'condition'; \
    test $? -ne 0 && echo -ne '\n add condition\n' >>{}.updated;  \
    mv {} {}.backup; mv {}.updated {}; echo -ne ' End\n';"

尽管实际的 grep 结果,xargs 中的test $? -ne 0 始终评估为 true 的问题。

【问题讨论】:

  • cat file | grep -qe condition ; test $? -ne 0 && 疯了。请改用grep -qe condition file &&
  • 谢谢,我正在使用它,但是当我的命令不起作用时,我开始尝试它。

标签: bash grep xargs


【解决方案1】:

尝试更改 $?到 \$?

Bash 可能正在评估 $?过早的

【讨论】:

  • 或者只是单引号传递给-c的脚本字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
相关资源
最近更新 更多