如何使用 Bash 中的文件命令检查文件是否可执行?
How do I check if a file is executable using the file command in Bash?
我有一个任务,要求我使用文件命令来检查文件是否是可执行文件类型。
作业中使用的措辞如下:
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. The output of the file command will include the special "executable" keyword if the file is an executable type.
问题的进一步说明:
- Use file and look for the "executable" keyword in the output to determine if the file is an executable type or not.
到目前为止,我找不到任何说明如何执行此操作的文档。当我在 CentOS8 的 bash 脚本上测试它时,它 returns 输出为:
validate.sh: ASCII text
Edit1/found_the_answer_edit: 我尝试使用 ti7 的使用测试脚本的想法,它得到了与他们得到的相同的输出。这让我开始思考。我的作业脚本需要对它们进行一些格式化。具体来说,为了让我们的脚本获得荣誉,我们必须:
#START SCRIPT
#username
``
at the beginning of the file and:
#结束脚本
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
决定了它!这可能是因为 header 上写错了
% 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
参数可能会更好 here,因为 file
不检查 权限,而不是文件格式是可执行的
注意测试命令命令实际上是[
或[[
,有细微差别
[[ -x "validate.sh" ]] && echo "executable" || echo "not executable"
您可以使用 ls
检查它的权限,它列出目录中的文件和它们的可选属性
ls -lhaF
您可以使用 chmod
使您的脚本可执行(通常,chmod +x
)
不确定这是否真的是 bash 运行 在 Windows 上,所以也许是 WSL。如果是,文件实际上 return 给定的 Windows exe 文件是可执行的。
即
file write.exe
write.exe: PE32+ executable (GUI) x86-64, for MS Windows
考虑到这一点,您可以通过管道传递给 grep 并测试输出,即
file write.exe | grep -q executable && echo "Executable" || echo "Not executable"
Executable
我有一个任务,要求我使用文件命令来检查文件是否是可执行文件类型。
作业中使用的措辞如下:
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. The output of the file command will include the special "executable" keyword if the file is an executable type.
问题的进一步说明:
- Use file and look for the "executable" keyword in the output to determine if the file is an executable type or not.
到目前为止,我找不到任何说明如何执行此操作的文档。当我在 CentOS8 的 bash 脚本上测试它时,它 returns 输出为:
validate.sh: ASCII text
Edit1/found_the_answer_edit: 我尝试使用 ti7 的使用测试脚本的想法,它得到了与他们得到的相同的输出。这让我开始思考。我的作业脚本需要对它们进行一些格式化。具体来说,为了让我们的脚本获得荣誉,我们必须:
#START SCRIPT
#username
``
at the beginning of the file and:
#结束脚本
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
决定了它!这可能是因为 header 上写错了
% 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
参数可能会更好 here,因为 file
不检查 权限,而不是文件格式是可执行的
注意测试命令命令实际上是[
或[[
,有细微差别
[[ -x "validate.sh" ]] && echo "executable" || echo "not executable"
您可以使用 ls
检查它的权限,它列出目录中的文件和它们的可选属性
ls -lhaF
您可以使用 chmod
使您的脚本可执行(通常,chmod +x
)
不确定这是否真的是 bash 运行 在 Windows 上,所以也许是 WSL。如果是,文件实际上 return 给定的 Windows exe 文件是可执行的。
即
file write.exe
write.exe: PE32+ executable (GUI) x86-64, for MS Windows
考虑到这一点,您可以通过管道传递给 grep 并测试输出,即
file write.exe | grep -q executable && echo "Executable" || echo "Not executable"
Executable