如何解析 Perl 中的符号链接?

发布于 2024-08-03 07:06:23 字数 660 浏览 7 评论 0原文

我有一个符号名称 javajrejre1.5jre1.6java1。 5java1.6 将指向相应的目录。考虑到第一个实例,它看起来像:

   java -> /usr/java/jdk1.5.x.x

我的目标如下:

  1. 检查符号是否存在
  2. 获取它指向的目录的值
  3. 使用正则表达式而不是在代码中使用 if-else 语句。

该链接将位于 /usr/tmp 中。

我在 Perl 中编写了一些代码,如下所示:

 $filename = "/usr/local/java";
 if (-l $filename) {
        print "File exists \n";
 } else {
        print "Not \n";
 }

但如果 java 不存在,而是 java1.4java1.5 存在我想搜索哪个应该以 java 开头并且是符号链接。

我还需要获取它指向的目录。请告知如何进行。

I have a symbolic name java or jre or jre1.5 or jre1.6 or java1.5 or java1.6 that will point to the respective directory. Taking into account the first instance, it will look like:

   java -> /usr/java/jdk1.5.x.x

My aim is as follows:

  1. To check whether the symbolic exists or not
  2. To get the value of the directory it is pointing
  3. To use regular expression instead of using if-else statement in the code.

The link will be in /usr/tmp.

I wrote some code in Perl which is as follows:

 $filename = "/usr/local/java";
 if (-l $filename) {
        print "File exists \n";
 } else {
        print "Not \n";
 }

But in case java is not present and instead java1.4 or java1.5 is present I want to search which should start with java and is a symbolic link.

Also I need to get the directory that it is pointing. Please advice how to proceed.

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

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

发布评论

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

评论(3

白云不回头 2024-08-10 07:06:23
use Cwd 'abs_path';

$absFilePath = abs_path("$linkPath") ;
use Cwd 'abs_path';

$absFilePath = abs_path("$linkPath") ;
不必你懂 2024-08-10 07:06:23

首先,您确定不想使用 ${JAVA_HOME} 环境变量吗?

您可以使用内置的 readlink() 函数读取符号链接目标。

要检查其他可能性,您可以将它们全部硬编码并一一检查是否存在,使用 opendir() / readdir() 内置函数或目标 IO:: Dir 查找目录中的所有符号链接并检查它们是否匹配或模式,甚至是 glob() 函数,这在您的情况下可能更简单。

First of all, are you sure that you don't want to use ${JAVA_HOME} environment variable?

You can read the symlink target using builtin readlink() function.

To check other possibilities, you may either hardcode them all and check one by one for existence, use opendir() / readdir() builtins or objective IO::Dir to lookup all the symlinks in the directory and check whether they match or pattern, or even glob() function which may be simpler in your case.

掐死时间 2024-08-10 07:06:23

这是链接文件或软链接文件的解决方案。以下代码片段将通过递归解析所有链接来给出链接的原始文件

#!/usr/bin/perl

use warnings;
use strict;

use File::Basename;

my $file='/dev/mapper/vg_ms1-lv_root'; #this should be your input
my $myCwd =`pwd`;
chomp($myCwd);

while ( -l $file ) {
    my $readLnk=readlink( $file );
    #print "$readLnk\n";
    my $directory = dirname( $file );
    my $fileName = basename( $file ); #not doing anything with this name
    #print "$directory\n";
    chdir($directory);
    my $linkPDir = dirname( $readLnk );
    chdir($linkPDir);
    my $next_file = basename( $readLnk );
    my $curDir=`pwd`;
    chomp($curDir);
    $file="$curDir"."/"."$next_file";
}

print "$file\n";
chdir($myCwd);

Here is the solution for the linked files or soft link files. the following snippet will give the original file of the link, by resolving all links recursively

#!/usr/bin/perl

use warnings;
use strict;

use File::Basename;

my $file='/dev/mapper/vg_ms1-lv_root'; #this should be your input
my $myCwd =`pwd`;
chomp($myCwd);

while ( -l $file ) {
    my $readLnk=readlink( $file );
    #print "$readLnk\n";
    my $directory = dirname( $file );
    my $fileName = basename( $file ); #not doing anything with this name
    #print "$directory\n";
    chdir($directory);
    my $linkPDir = dirname( $readLnk );
    chdir($linkPDir);
    my $next_file = basename( $readLnk );
    my $curDir=`pwd`;
    chomp($curDir);
    $file="$curDir"."/"."$next_file";
}

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