[英]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.