简体   繁体   English

如何使用 Bash 中的 file 命令检查文件是否可执行?

[英]How do I check if a file is executable using the file command in Bash?

I have an assignment wherein I am required to use the file command to check if a file is an executable type or not.我有一项作业,其中要求我使用 file 命令来检查文件是否为可执行类型。

The wording used in the assignment is as follows:作业中使用的措辞如下:

The file command should be used to determine generically what kind of file the argument is and in particular if the file is an executable type or not. file 命令应该用于一般地确定参数是什么类型的文件,特别是文件是否是可执行类型。 The output of the file command will include the special "executable" keyword if the file is an executable type.如果文件是可执行类型,文件命令的输出将包含特殊的“可执行”关键字。

Further on in the question, it states:进一步在问题中,它指出:

  1. Use file and look for the "executable" keyword in the output to determine if the file is an executable type or not.使用 file 并在输出中查找“executable”关键字以确定文件是否为可执行类型。

I have so far been unable to find any documentation that explains how to do this.到目前为止,我一直无法找到任何解释如何执行此操作的文档。 When I test this on a bash script in CentOS8, it returns this as output:当我在 CentOS8 中的 bash 脚本上对此进行测试时,它会将其作为输出返回:

validate.sh: ASCII text

Edit1/found_the_answer_edit: I tried using ti7's idea of using a test script and it got the same output as what they had gotten. Edit1/found_the_answer_edit:我尝试使用 ti7 的使用测试脚本的想法,它得到的输出与他们得到的输出相同。 Which got me thinking.这让我思考。 The scripts for my assignments are required to have some formatting done to them.我的作业脚本需要对它们进行一些格式化。 Specifically, in order to get credit for our scripts, we must have:具体来说,为了获得我们脚本的荣誉,我们必须:

#START SCRIPT
#username
``
at the beginning of the file and:

#END SCRIPT #结束脚本

at the end of it. I deleted these bits in my validate.sh file and when I tested it again with the file command I got the same output that I got from the test script. So the answer is that there isn't anything special. File just can't deal with a file that doesn't start with "#!/bin/bash"

Your script validate.sh probably isn't executable as file determines it!您的脚本validate.sh可能无法执行,因为file决定了它! This is probably because the header is wrong on it这可能是因为标题错误就可以了

% cat test.sh
#!/bin/bash
echo "foo"
% file test.sh
test.sh: Bourne-Again shell script text executable, ASCII text

Once that's settled, you can check for the executable keyword in the response, perhaps with grep一旦确定,您可以检查响应中的executable关键字,也许使用grep

% file test.sh | grep -q executable ; echo $?
0
% touch test-empty.sh
% file test-empty.sh | grep -q executable ; echo $?
1

If you're not forced to use file , using test's -x arg may be much better as suggested here , as file doesn't check permissions , rather that the file format is executable如果您不是被迫使用file ,则按照此处的建议使用 test 的-x arg 可能会好得多,因为file不检查权限,而是文件格式是可执行的

Note the test command command is actually [ or [[ with subtle differences注意 test 命令命令实际上是[[[有细微的差别

[[ -x "validate.sh" ]] && echo "executable" || echo "not executable"

You can check the permissions on it with ls , which lists files in a directory and optionally properties of them您可以使用ls来检查它的权限,它会列出目录中的文件以及它们的可选属性

ls -lhaF

You can make your script executable with chmod (trivially, chmod +x )您可以使用chmod使您的脚本可执行(简单地说, chmod +x

Not sure if this is actually bash running on Windows and so maybe WSL.不确定这是否真的是在 Windows 上运行的 bash,所以可能是 WSL。 If it is, file will actually return that a given Windows exe file is executable.如果是,文件实际上将返回给定的 Windows exe 文件是可执行的。

ie IE

file write.exe

write.exe: PE32+ executable (GUI) x86-64, for MS Windows

With this in mind, you can pipe through to grep and test the output ie考虑到这一点,您可以通过管道传递到 grep 并测试输出,即

file write.exe | grep -q executable && echo "Executable" || echo "Not executable"

Executable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM