[英]how do I select only shell scripts from a collection of files
I want to find all my bash scripts (i have accumulated many of them now) and run them all through bash -n
automatically. 我想找到我所有的bash脚本(我现在已经积累了很多)并自动通过bash -n
运行它们。
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
... 我想让grep
只匹配第一个非空白行以#!/bin/sh
或#!/usr/bin/env sh
或#!/usr/bin/env bash
或#!/bin/bash
开头的文件#!/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? 但是为了“正确”,我怎么能合理地只在第一个非空白(或非空白)线上做我的grep?
使用find:
find . -type f -exec grep -e "^#\!\/bin\/.*sh$" {} +
With GNU awk
用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 #!
你可以使用上面的regex
并将它减少到#!
. 。 FNR==1
along with the regex
would ensure that the first line is checked for she-bang
line. FNR==1
以及regex
将确保检查第一行的she-bang
线。 nextfile
would ensure that no file is looked beyond first line. nextfile
将确保没有文件超出第一行。 print
if just for logging. print
如果只是为了记录。 FILENAME
will print the name of the file under inspection. FILENAME
将打印正在检查的文件的名称。 *
will glob to all files under working directory. *
将glob到工作目录下的所有文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.