0

This Bash script goes through every file in /app directory.

Here is the tree hirarchy

/app
_/a
-/b
-/test/unittest/python/test.py

Problem is,,, I don't want to execute lint step. How do I exclude test directory?

...
for file in $(find /app -name '*.py'); do
        filename=$(basename $file)
        if [[ $filename != "__init__.py" ]] ; then
                echo "$file"
                pylint "${file}" --rcfile="${lint_path}" || exit 1
        fi
done

This is a way to solve the problem not sure if it's right. not working!

$(find /app -name '*.py' -not -path '*test*')
merry-go-round
  • 4,533
  • 10
  • 54
  • 102
  • BTW, `for anything in $(find ...)` is bad practice *in general*. See [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor) for an explanation of why (short form: it'll mangle perfectly legal filenames), and [UsingFind](http://mywiki.wooledge.org/UsingFind) for alternatives (see in particular those practices using either `-print0` or `-exec`, both of which can pass all possible names correctly). – Charles Duffy May 21 '19 at 21:19
  • Similarly, `filename=$(basename $file)` is itself going to mangle names with spaces or expandable wildcards due to the unquoted expansion. `filename=$(basename "$file")` is safer, though removing `basename` entirely and making it `filename=${file##*/}` is equally correct and a lot faster to execute; see [BashFAQ #100](http://mywiki.wooledge.org/BashFAQ/100) describing the syntax used. – Charles Duffy May 21 '19 at 21:22

1 Answers1

1

Use the -prune option to prevent descending into the test directory.

You can also exclude __init__.py in find so you don't need to test that with if.

$(find /app -type d -name test -prune -o -type f -name '*.py' ! -name '__init__.py' -print)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Would you consider removing the command substitution syntax (or accepting an edit that does so)? That way the answer stays equally on-point but is no longer showcasing bad practices. – Charles Duffy May 21 '19 at 21:25
  • It is super alien code to me. But this is really working great – merry-go-round May 21 '19 at 21:25
  • @Barmar Sir. I changed my job from full-stack to devops engineer. I'm struggling to learn bash script. Is there any resource or the best practice to learn bash script??? It's not simple as python scripting to me. :( – merry-go-round May 21 '19 at 21:26
  • @JohnBaek, the [BashGuide](https://mywiki.wooledge.org/BashGuide) is a great resource, as is the [bash-hackers' wiki](http://wiki.bash-hackers.org/). See the links in the [StackOverflow bash tag wiki](https://stackoverflow.com/tags/bash/info) in general; the only thing there I explicitly advise *against* using (actually, maybe not there anymore) are the resources from TLDP. See also the [BashFAQ](http://mywiki.wooledge.org/BashFAQ) and [BashPitfalls](http://mywiki.wooledge.org/BashPitfalls) pages. – Charles Duffy May 21 '19 at 21:27
  • @JohnBaek I'm sure there are tutorials, but I don't have any recommendations. – Barmar May 21 '19 at 21:27
  • @JohnBaek But you shouldn't expect to find things like this in any tutorials. The details of every command are too specific for general tutorials. – Barmar May 21 '19 at 21:28
  • @CharlesDuffy I think the links in your comment are enough. Rewriting the answer to show the correct way to loop would obscure the important points. – Barmar May 21 '19 at 21:29
  • @Barmar, yes, I'm not suggesting showing a `while read` rewrite, just taking out the `$(` at the front and `)` at the end so it's no longer endorsing/showing practices that are only meaningful in combination with other practices best avoided. – Charles Duffy May 21 '19 at 21:29
  • Thanks for advice everyone... – merry-go-round May 21 '19 at 21:59