检查文件是否是可执行的 [英] check if a file is executable

查看:123
本文介绍了检查文件是否是可执行的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是来检查程序的可执行文件使用bash最简单的方法,不执行呢?它至少应该检查该文件是否具有执行权,并且是相同的体系结构(例如,而不是一个窗口的可执行或其它不支持的结构中,如果不是64比特的系统是32位,...)作为当前的系统。

I am wondering what's the easiest way to check if a program is executable with bash, without executing it ? It should at least check whether the file has execute rights, and is of the same architecture (for example, not a windows executable or another unsupported architecture, not 64 bits if the system is 32 bits, ...) as the current system.

推荐答案

看看各种测试运营商(这是test命令本身,而是内置的BASH和TCSH测试或多或少相同)。

Take a look at the various test operators (this is for the test command itself, but the built-in BASH and TCSH tests are more or less the same).

您会注意到 -x文件说的 FILE存在和执行(或搜索)权限授予

BASH,伯恩,KSH,岩组脚本

BASH, Bourne, Ksh, Zsh Script

if [[ -x "$file" ]]
then
    echo "File '$file' is executable"
else
    echo "File '$file' is not executable or found"
fi

TCSH或CSH脚本:

TCSH or CSH Script:

if ( -x "$file" ) then
    echo "File '$file' is executable"
else
    echo "File '$file' is not executable or found"
endif

要确定的键入的文件是,请尝试使用文件命令。您可以解析输出,看看它到底是什么类型的文件。 词'O警告:有时候文件将返回比一行多。这里是我的Mac上会发生什么:

To determine the type of file it is, try the file command. You can parse the output to see exactly what type of file it is. Word 'o Warning: Sometimes file will return more than one line. Here's what happens on my Mac:

$ file /bin/ls    
/bin/ls: Mach-O universal binary with 2 architectures
/bin/ls (for architecture x86_64):  Mach-O 64-bit executable x86_64
/bin/ls (for architecture i386):    Mach-O executable i386

文件命令将返回不同的输出取决于操作系统。然而,这个词执行将在可执行程序,通常建筑会出现了。

The file command returns different output depending upon the OS. However, the word executable will be in executable programs, and usually the architecture will appear too.

以上比较什么,我得到我的Linux机器:

Compare the above to what I get on my Linux box:

$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, AMD x86-64, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), stripped

和一个Solaris系统:

And a Solaris box:

$ file /bin/ls
/bin/ls:        ELF 32-bit MSB executable SPARC Version 1, dynamically linked, stripped

在所有三个,你会看到这个词执行和架构( X86-64 386 SPARC 32位)。

In all three, you'll see the word executable and the architecture (x86-64, i386, or SPARC with 32-bit).

非常感谢你,这似乎要走的路。我标记这是我的答案之前,可以请你指导我为我会有什么样的剧本外壳检查来执行(即,什么样的解析),以检查是否能执行一个程序上的'文件'?如果这样的测试是太难做一般性的基础上,我至少要检查它是否是一个Linux的可执行文件或OSX(Mach-O的)

Thank you very much, that seems the way to go. Before I mark this as my answer, can you please guide me as to what kind of script shell check I would have to perform (ie, what kind of parsing) on 'file' in order to check whether I can execute a program ? If such a test is too difficult to make on a general basis, I would at least like to check whether it's a linux executable or osX (Mach-O)

关闭我的头顶,你可以做在bash是这样的:

Off the top of my head, you could do something like this in BASH:

if [ -x "$file" ] && file "$file" | grep -q "Mach-O"
then
    echo "This is an executable Mac file"
elif [ -x "$file" ] && file "$file" | grep -q "GNU/Linux"
then
    echo "This is an executable Linux File"
elif [ -x "$file" ] && file "$file" | grep q "shell script"
then
    echo "This is an executable Shell Script"
elif [ -x "$file" ]
then
    echo "This file is merely marked executable, but what type is a mystery"
else
    echo "This file isn't even marked as being executable"
fi

基本上,我运行测试,那么如果说是成功的,我做的grep在文件命令的输出。在的grep -q 办法不打印任何输出,但使用grep的退出code,看看我找到的字符串。如果你的系统不采取的grep -q ,你可以尝试 grep的正则表达式>的/ dev / null的2 - ;&放大器; 1

Basically, I'm running the test, then if that is successful, I do a grep on the output of the file command. The grep -q means don't print any output, but use the exit code of grep to see if I found the string. If your system doesn't take grep -q, you can try grep "regex" > /dev/null 2>&1.

同样,文件的输出命令可能会有所不同,从系统到系统,所以你必须确认这些将在您的系统上工作。另外,我检查可执行位。如果文件是一个二进制可执行文件,但可执行位不上,我会说这不是可执行文件。这可能不是你想要的。

Again, the output of the file command may vary from system to system, so you'll have to verify that these will work on your system. Also, I'm checking the executable bit. If a file is a binary executable, but the executable bit isn't on, I'll say it's not executable. This may not be what you want.

这篇关于检查文件是否是可执行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆