I want to find all my bash scripts (i have accumulated many of them now) and run them all through bash -n
automatically.
What's a quick way to do this? I'd like to have grep
match only the files whose first non blank lines start with #!/bin/sh
or #!/usr/bin/env sh
or #!/usr/bin/env bash
or #!/bin/bash
...
A serviceable answer is of course something along the lines of
for file in *; do
if head -n 5 $file | grep "#!.*sh$" > /dev/null; then
bash -n $file
fi
done
But for the sake of "correctness" how might I reasonably do my grep on only the first non-whitespace (or alternatively non-blank) line?
使用find:
find . -type f -exec grep -e "^#\!\/bin\/.*sh$" {} +
With GNU awk
awk '
FNR==1 && $0~/#!\/bin\/sh|#!\/usr\/bin\/env sh|#!\/usr\/bin\/env bash|#!\/bin\/bash/ {
print FILENAME " is shell script";
}
FNR>1 {
nextfile
}' *
regex
and reduce it to just #!
. FNR==1
along with the regex
would ensure that the first line is checked for she-bang
line. nextfile
would ensure that no file is looked beyond first line. print
if just for logging. FILENAME
will print the name of the file under inspection. *
will glob to all files under working directory.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.