![](/img/trans.png)
[英]How do I search for a string before a specific element in a Bash array?
[英]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.