我有一项作业,要求我使用file命令检查文件是否为可执行文件类型。
作业中使用的措词如下:
应该使用file命令来一般性地确定参数是哪种文件,尤其是文件是否为可执行文件类型时。如果文件是可执行类型,则file命令的输出将包含特殊的“ executable”关键字。
在问题中进一步指出:
- 使用file并在输出中查找“ executable”关键字,以确定该文件是否为可执行文件类型。
到目前为止,我一直找不到任何说明该操作方法的文档。当我在CentOS8中的bash脚本上对此进行测试时,它将作为输出返回:
validate.sh: ASCII text
Edit1 / found_the_answer_edit:我尝试使用ti7的使用测试脚本的想法,它得到的输出与他们获得的输出相同。这让我开始思考。我的作业脚本需要对它们进行一些格式化。具体来说,为了获得我们脚本的信誉,我们必须具备:
#START SCRIPT
#username
``
at the beginning of the file and:
#END脚本
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"
您的脚本validate.sh
可能不是可执行文件file
决定了它!这可能是因为标题错误
% cat test.sh
#!/bin/bash
echo "foo"
% file test.sh
test.sh: Bourne-Again shell script text executable, ASCII text
解决后,您可以检查executable
响应中的关键字,也许grep
% file test.sh | grep -q executable ; echo $?
0
% touch test-empty.sh
% file test-empty.sh | grep -q executable ; echo $?
1
如果您不被迫使用file
,-x
按照这里的建议使用test的arg可能会更好,因为file
不检查权限,而是文件格式是可执行的
请注意,测试命令命令实际上是[
或[[
具有细微差别
[[ -x "validate.sh" ]] && echo "executable" || echo "not executable"
您可以使用来检查其权限,该权限ls
列出目录中的文件以及它们的属性(可选)
ls -lhaF
你可以用你的脚本执行chmod
(平凡,chmod +x
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句