首页 > 解决方案 > 如何推迟打印错误,直到每次执行

问题描述

目前我遍历目录.py中的每个文件/。对于每次迭代,我pycodestyle一看到错误就调用并退出。

  1. 但是我想查看每个文件的所有错误消息,即使任何文件在 << 之前有错误,这有助于开发人员查看他/她应该更改哪些行以通过测试(linting)。

  2. 如果没有文件打印错误,则不要打印错误。<< 这对我的 Jenkins 管道很有用。

    for file in $(find /-type d -name test -prune -o -type f -name '*.py' -print); do
        filename=$(basename $file)
        if [[ $filename != "__init__.py" ]] ; then
            echo "$file"
            pycodestyle "${file}" || exit 1 <<< This causes an error.
                       << If it passes the linting, it doesn't exit. 
        fi
    done
    

我的解决方案:

不知何故,我需要一个布尔局部变量来显示它是否打印错误。最后我可以检查变量并返回退出与否。但我不知道如何实现这个......谢谢!

标签: bash

解决方案


而不是退出,设置一个变量,您在最后进行测试,以便您以正确的状态退出。

status=0
for file in $(find /-type d -name test -prune -o -type f -name '*.py' ! -name '__init__.py' -print); do
    echo "$file"
    pycodestyle "${file}" || status=1
done
exit "$status"

__init__.py此外,您可以在命令中过滤掉find,因此您不需要if(我在Exclude a certain directory while (find command) - BASH中向您展示了相同的内容)。

另外,请参阅为什么循环查找的输出是不好的做法?


推荐阅读