심볼릭 링크 깨짐을 방지하는 방법과 sed 정규식 대신 Perl을 언제 사용해야 합니까?

심볼릭 링크 깨짐을 방지하는 방법과 sed 정규식 대신 Perl을 언제 사용해야 합니까?

기반으로sed -i가 심볼릭 링크를 깨는 것을 방지하는 방법은 무엇입니까?, 하지만 저는 Perl을 사용하고 있습니다.

나는 다음과 같은 질문을 모두 시도해 보았습니다.

그러나 성공하지 못했습니다. 다음은 작고 간단한 예입니다.

#!/bin/perl
use strict; use warnings;

while (<>) 
{

  ## For all lines, replace all occurrences of #5c616c with another colour
  s/#5c616c/#8bbac9/g $(readlink -e -- "<>")

  ## $(readlink -e -- "<>") is similar to --in-place --follow-symlinks

  ## Print the line
  print;
}

답변1

readlink -e명령은 이식 가능하지 않으므로 의존해서는 안 됩니다.

$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]

Perl 코드에서 링크를 가리키는 파일 이름으로 바꿉니다.readlink함수그런 다음 평소대로 입력을 반복합니다.

$ perl -i -ple 'BEGIN{for(@ARGV){ $_=readlink if -l }} tr/A-Z/a-z/' alink

alink여전히 심볼릭 링크이며 내용이 input수정되었습니다.

$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.

Perl 스크립트에서는 다음과 같이 보일 수 있습니다.

#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV) {
    $arg = readlink $arg if -l $arg;
}

# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline) {
    s/#5c616c/#8bbac9/g;
    print;
}

입력에 중복된 파일 이름이 포함된 경우 List::Util::uniq동일한 파일 이름을 두 번 이상 수정하지 않으려면 유사한 방법이 필요할 수 있습니다.

관련 정보