检查 PATH 中是否存在文件

发布于 2024-12-17 15:46:53 字数 142 浏览 2 评论 0原文

我有这样的东西:

if(! -e $filename) {
    # do something
}

但我需要更改它,以便它甚至在我的路径上查找文件。有没有办法在不分析 PATH 的情况下实现这一目标?

I have something like this:

if(! -e $filename) {
    # do something
}

but I need to alter it so that it looks for file even on my PATH. Is there any way to achieve this without analysing PATH?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扮仙女 2024-12-24 15:46:53

如何在不查看 $ENV{PATH} 的情况下查看文件是否位于 $ENV{PATH} 指定的目录之一中? ……这是一个反问句。

这是我不久前编写的简短脚本。您应该能够根据您的需求进行调整:

#!/usr/bin/perl

use strict; use warnings;

use File::Basename;
use File::Spec::Functions qw( catfile path );

my $myname = fileparse $0;
die "Usage: $myname program_name\n" unless @ARGV;

my @path = path;
my @pathext = ( q{} );

if ( $^O eq 'MSWin32' ) {
    push @pathext, map { lc } split /;/, $ENV{PATHEXT};
}

PROGRAM: for my $progname ( @ARGV ) {
    unless ( $progname eq fileparse $progname ) {
        warn "Not processed: $progname\n\tArgument is not a plain file name\n";
        next PROGRAM;
    }

    my @results;

    for my $dir ( @path ) {
        for my $ext ( @pathext ) {
            my $f = catfile $dir, "$progname$ext";
            push @results, $f if -x $f;
        }
    }

    print "$progname:\n";
    print "\t$_\n" for @results;
}

How can you see if a file is in one of the directories specified in $ENV{PATH} without looking at $ENV{PATH}? … That's a rhetorical question.

Here is a short script I wrote some time ago. You should be able to adapt it for your needs:

#!/usr/bin/perl

use strict; use warnings;

use File::Basename;
use File::Spec::Functions qw( catfile path );

my $myname = fileparse $0;
die "Usage: $myname program_name\n" unless @ARGV;

my @path = path;
my @pathext = ( q{} );

if ( $^O eq 'MSWin32' ) {
    push @pathext, map { lc } split /;/, $ENV{PATHEXT};
}

PROGRAM: for my $progname ( @ARGV ) {
    unless ( $progname eq fileparse $progname ) {
        warn "Not processed: $progname\n\tArgument is not a plain file name\n";
        next PROGRAM;
    }

    my @results;

    for my $dir ( @path ) {
        for my $ext ( @pathext ) {
            my $f = catfile $dir, "$progname$ext";
            push @results, $f if -x $f;
        }
    }

    print "$progname:\n";
    print "\t$_\n" for @results;
}
夏至、离别 2024-12-24 15:46:53

系统在加载可执行文件时使用 PATH 变量。因此,为了让底层系统为您完成工作,我相信您需要尝试加载可执行文件。听起来这不是您想要做的。

很可能有一些库可以提供此类功能,但编写自己的库非常简单。您只需要使用 split 然后迭代即可。

The PATH variable is used by the system when loading executables. So to get the underlying system to do the work for you I believe you would need to attempt to load an executable. It doesn't sound like this is what you are looking to do.

There may well be some library that will offer such functionality but it is very simple to write your own. You just need to use split and then iterate.

三生池水覆流年 2024-12-24 15:46:53

补充 daximSinan Ünür 的 有用的答案:

有限情况下,如果您只关心是否给定的程序在系统路径中,您可以采取捷径

如果目标程序是一个具有无副作用选项(除了生成标准输出)的 CLI,该选项会导致程序成功退出非常快 - 例如,--version - 您可以执行以下操作:

my $exe = 'bash';   # example
`$exe --version` || die "'$exe' not found.";

To complement daxim's and Sinan Ünür's helpful answers:

In limited circumstances, if you only care whether or not a given program is in the system's path, you can take a shortcut:

If the target program is a CLI that has a side-effect-free option (other than producing stdout output) that causes the program to exit successfully very quickly - e.g., --version - you can do the following:

my $exe = 'bash';   # example
`$exe --version` || die "'$exe' not found.";
倥絔 2024-12-24 15:46:53

如果你想使用核心模块,有 can_run() 来自 IPC::Cmd

use IPC::Cmd qw(can_run);
if (can_run($filename)) {
    # do something
}

File::Which 的 perldoc 指出了一个警告,即这也将搜索当前目录,即使那不在你的路径中:

IPC::Cmd

带有一个“can_run”函数,其语义略有不同
传统的UNIX在哪里。它将在当前目录中查找可执行文件
目录,即使当前目录未被搜索
Unix 上的默认值。

If you want to use a core module, there's can_run() from IPC::Cmd.

use IPC::Cmd qw(can_run);
if (can_run($filename)) {
    # do something
}

The perldoc for File::Which points out the caveat that this will also search the current directory, even if that's not in your path:

IPC::Cmd

Comes with a "can_run" function with slightly different semantics that
the traditional UNIX where. It will find executables in the current
directory, even though the current directory is not searched for by
default on Unix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文