簡體   English   中英

如何在文件夾中沒有特定擴展名的情況下搜索 bash 腳本文件?

[英]How do I search for bash script files without having a specific extension within a folder?

我想在 Array 的文件夾下找到 bash 腳本文件。 但是 bash 腳本文件沒有指定的擴展名。 我寫了這樣的東西:

for i in "${array[@]}"
do
    # Here I will write the condition that the file is found in the folder $k
done

如果您的腳本在其第一行中有#!/bin/bash#!/bin/sh (應該如此),那么您可以使用file命令來檢查文件是否為腳本。

例如,以這個腳本為例:

#!/bin/bash
echo "I am a script!"

file filename.sh的 Output 將是filename.sh: Bourne-Again shell script, ASCII text executable ,表示它是 Z2591C98B70119FE6248918B1E424 腳本。 請注意, file命令不使用文件的擴展名來指示其格式,而是使用文件的內容。

file 命令確定文件類型。 例如

#!/bin/bash
arr=(~/*)
for i in "${arr[@]}"
do
    type=`file -b $i | awk '{print $2}'`
    if  [[ $type = shell ]];then
        echo $i is a shell script
    fi
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM