Reputation:
How do i validate every .sh file in a specified directory which ends .sh to make sure the first line is #!/bin/bash?
I want the output for each file to be either filename.sh is valid
or filename.sh is missing the header
.
Upvotes: 0
Views: 420
Reputation: 14491
Consider also pure-bash solution. It does not call any other process.
Note that a valid file may contain spaces between the #!
and the /bin/bash
.
#! /bin/bash
PAT="^#! */bin/bash"
for file in *.sh ; do
line=
read line < $file
if [[ "$line" =~ $PAT ]] ; then
echo "$file is valid"
else
echo "$file is missing the header"
fi
done
If you need to support other interpreters (e.g., bin/sh), you can expand the pattern to include other shells with
PAT="#! *(/bin/bash|/bin/sh)"
Upvotes: 1
Reputation: 52589
Maybe something like
awk 'FNR == 1 { if ($0 == "#!/bin/bash") {
print FILENAME, "is valid"
} else {
print FILENAME, "is missing the header"
}
nextfile
}' *.sh
Upvotes: 0