So I am trying to find empty files that may exist in my project, as well as prevent their existence in future. I am doing this in a bash script like so.
#!/bin/bash -eu
if [[ "$(find . -type f -empty -not -path "./node_modules/*" | wc -l)" -gt 0 ]]; then
echo "[check-for-empty-files] Empty files found. $(find . -type f -empty -not -path "./node_modules/*" -ls)"
exit 1
else
echo "[check-for-empty-files] No empties."
fi
... and it works as expected. However, the issue here is I'd like to store the find cmd in a variable like so:
empty_file_cmd=$(find . -type f -empty -not -path "./node_modules/*")
... so that I could use it in other places in the script. However this returns "Permission denied" and I cannot seem to figure out why that happens, given that the prior implementation does not return the "Permission denied" error.
CodePudding user response:
Don't store command lines in variables. For one thing, you'll screw up field separation and substitution. Instead use a function:
myFindCmd() {
find . -type f -empty -not -path "./node_modules/*"
}
myFindCmd # execute it whereever you like