gpt4 book ai didi

bash - 如何从文件集合中只选择 shell 脚本

转载 作者:行者123 更新时间:2023-11-29 09:12:10 25 4
gpt4 key购买 nike

我想找到我所有的 bash 脚本(我现在已经积累了很多)并通过 bash -n 自动运行它们。

有什么快速的方法可以做到这一点?我想让 grep 只匹配第一个非空行以 #!/bin/sh#!/usr/bin/env 开头的文件sh#!/usr/bin/env bash#!/bin/bash...

一个有用的答案当然是

for file in *; do 
if head -n 5 $file | grep "#!.*sh$" > /dev/null; then
bash -n $file
fi
done

但是为了“正确性”,我如何合理地在 仅第一个非空白(或者非空白) 行上执行 grep?

最佳答案

使用查找:

     find . -type f -exec grep -e "^#\!\/bin\/.*sh$" {} +

关于bash - 如何从文件集合中只选择 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com