-1

Currently I iterate through every .py file in / directory. For each iteration, I call pycodestyle and exit as soon as I see the error.

  1. But I want to see all the error messages for every file even if any of file had an error before << This helps developer to see which lines he/she should change in order to pass the test (linting).

  2. If no file prints an error, don't print error. << This will be useful for my Jenkins pipeline.

    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
    

My solution:

Somehow I need a boolean local variable to show whether it prints error. At the end I can check the variable and returns exit or not. But I don't know how to implement this... Thanks!

merry-go-round
  • 4,533
  • 10
  • 54
  • 102

1 Answers1

2

Instead of exiting, set a variable, which you test at the end so you exit with the proper status.

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"

Also, you can filter out __init__.py in the find command, so you don't need the if (I showed you the same thing in Exclude a certain directory while (find command) - BASH).

Also, see Why is looping over find's output bad practice?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • but doesn't `exit 0` fails on Jenkins? @Barmer – merry-go-round May 31 '19 at 00:52
  • 1
    0 is success, non-zero is failure. – Barmar May 31 '19 at 00:52
  • @EdMorton Do you have a link to a question that shows the right way to restructure it? – Barmar May 31 '19 at 01:00
  • damnn bash script is super difficult. probably harder than C. – merry-go-round May 31 '19 at 01:20
  • a) don't think of it as a language like C because then you'll read up on how to do X instead of assuming you know how to do X, and b) only use a shell script (bash or otherwise) to create/destroy files and processes and sequence calls to tools (i.e. don't try to use it for things it wasn't designed to do) and you'll find it relatively easy to learn since it has a fraction of the constructs of a general purpose language like C. It's syntax and semantics are just "different" and so need to be learned, free of assumptions of how you might do something similar in some other language. – Ed Morton May 31 '19 at 01:27
  • 1
    @hellofanengineer Bash scripting is normally considered easier than other languages, because you're mostly using command that you're already familiar with when working interactively. – Barmar May 31 '19 at 01:28
  • @Barmar Could I redirect to this issue: https://stackoverflow.com/questions/56466433/error-while-running-a-command-with-ssh-command-line-line-0-bad-configuration – merry-go-round Jun 05 '19 at 18:55