简体   繁体   English

如何在文件夹中没有特定扩展名的情况下搜索 bash 脚本文件?

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

I want to find bash script files under folders in Array.我想在 Array 的文件夹下找到 bash 脚本文件。 But bash script files do not have a specified extension.但是 bash 脚本文件没有指定的扩展名。 I wrote something like this:我写了这样的东西:

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

If your scripts have #!/bin/bash or #!/bin/sh in their first line (as they should), then you can use the file command to check if a file is a script or not.如果您的脚本在其第一行中有#!/bin/bash#!/bin/sh (应该如此),那么您可以使用file命令来检查文件是否为脚本。

For example, take this script:例如,以这个脚本为例:

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

Output of file filename.sh will be filename.sh: Bourne-Again shell script, ASCII text executable , which is indicating it is a shell script. file filename.sh的 Output 将是filename.sh: Bourne-Again shell script, ASCII text executable ,表示它是 Z2591C98B70119FE6248918B1E424 脚本。 Note that the file command does not use the extension of the file to indicate its format, but uses the content of it.请注意, file命令不使用文件的扩展名来指示其格式,而是使用文件的内容。

The file command determines a file type. file 命令确定文件类型。 eg例如

#!/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.

相关问题 如何在 Bash 数组中的特定元素之前搜索字符串? - How do I search for a string before a specific element in a Bash array? php如何获取文件夹中没有扩展名的文件数组 - php how to get an array of files without extension in the folder 如何编写bash脚本,将目录中所有子级别的随机文件一个接一个地传递到可执行文件中? - How do I write a bash script to pass random files from all sublevels of a directory into an executable one by one? 如何使用php查找数组中两个特定键的平均值,而不求整个数组的平均值 - How do I use php to find an average of two specific keys within an array without averaging the entire array 如何在Shell脚本中搜索元素 - How do I search an element in shell script 在 bash 中的多个目录中使用空格的列出文件中搜索 substring - Search for a substring within listed files with spaces from multiple directories in bash 如何通过 ruby​​ 中的哈希值在哈希数组中搜索? - How do I search within an array of hashes by hash values in ruby? 如何在数组中的列表中搜索字符串? - How do I search for a string in a list within an array? 如何根据文件名和/或扩展名检索文件夹的所有文件大小? - How can I retrieve all files sizes of a folder based on file name and/or extension? 如何在 python 的数组中获取特定索引? - how do I get a specific index within an array in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM