![](/img/trans.png)
[英]How do I add a string to the beginning of the name of a collection of files in another directory using shell
[英]how do I select only shell scripts from a collection of files
我想找到我所有的bash腳本(我現在已經積累了很多)並自動通過bash -n
運行它們。
什么是快速的方法呢? 我想讓grep
只匹配第一個非空白行以#!/bin/sh
或#!/usr/bin/env sh
或#!/usr/bin/env bash
或#!/bin/bash
開頭的文件#!/bin/bash
......
一個可用的答案當然是有用的
for file in *; do
if head -n 5 $file | grep "#!.*sh$" > /dev/null; then
bash -n $file
fi
done
但是為了“正確”,我怎么能合理地只在第一個非空白(或非空白)線上做我的grep?
使用find:
find . -type f -exec grep -e "^#\!\/bin\/.*sh$" {} +
用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
並將它減少到#!
。 FNR==1
以及regex
將確保檢查第一行的she-bang
線。 nextfile
將確保沒有文件超出第一行。 print
如果只是為了記錄。 FILENAME
將打印正在檢查的文件的名稱。 *
將glob到工作目錄下的所有文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.