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*')
2
  • BTW, for anything in $(find ...) is bad practice in general. See DontReadLinesWithFor for an explanation of why (short form: it'll mangle perfectly legal filenames), and UsingFind for alternatives (see in particular those practices using either -print0 or -exec, both of which can pass all possible names correctly). Commented May 21, 2019 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 describing the syntax used. Commented May 21, 2019 at 21:22

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

9 Comments

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.
It is super alien code to me. But this is really working great
@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. :(
@JohnBaek, the BashGuide is a great resource, as is the bash-hackers' wiki. See the links in the StackOverflow bash tag wiki 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 and BashPitfalls pages.
@JohnBaek I'm sure there are tutorials, but I don't have any recommendations.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.