Im trying to write a script:
env PYTHONPATH=$PYTHONPATH: $Dir/scripts find * -name ‘*.py' -exec pylint (} \\; | grep . && exit 1
The code is finding all scripts in the root directory instead of using the environmental variables I set. Any help on writing this code to only look for files in the directory I set as a value in PYTHONPATH.
CodePudding user response:
env PYTHONPATH=$PYTHONPATH: $Dir/scripts
isn't doing what you think it's doing. Including $PYTHONPATH
includes the former value of PYTHONPATH
, meaning whatever you have it already set to or a blank default. The space in your variable also makes it invalid, and instead interprets the $Dir/scripts
as a new command. It looks like what you want would be env PYTHONPATH=$Dir/scripts
— but there's actually an easier way.
If you have __init__.py
files in your directory, you can just do pylint ./some-directory
. If you don't, you can use xargs
: find . -type f -name "*.py" | xargs pylint
. If you wanted to pass the directory instead of have it coded to .
(your current calling directory) you could do that too:
# set directory to first argument
dir="$1"
# check if "dir" was actually provided, if not, set to `.`
if [ -z "$dir" ]; then dir=.; fi
find "$dir" -type f -name "*.py" | xargs pylint
You could save that in a script or function and then call it either with a directory (like run-pylint-on-everything.sh ~/foo/bar
, or not, in which case it would run starting from your current shell location.
CodePudding user response:
There’s no space between the PYTHONPATH value, it was a typo mistake, I want to run the command on a CLI instead of a script.