I have a project with some lua and some bash files. I want to loop over all files and depending on the shebang I want to execute a validity check.


You can use find and awk to check the first line (your awk has to support FNR, like POSIX awks do) and add some search patterns to find (like excluding hidden folders):

echo "check bash files ..."
find . -not -path '*/\.*' \
  -type f -exec awk 'FNR == 1 && /^#!.*sh/{print FILENAME}' {} + | \
  while IFS= read -r f; do 
    echo "checking $f ..."
    bash -n "$f"

Note: use IFS= to avoid splitting filenames containing one of the $IFS, for example: 'a b'

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.