perlsymlinkaix

Resolving a symlink without readlink utility


I am trying to use a perl one liner to print a string using regex match in unix command line pipeline.

ls -l /APPL

gives output

lrwxrwxrwx 1 root system 4 Nov 27 12:07 /APPL -\> /PRD

My goal is to print 'PRD'

the command I am using

ls -l /APPL | perl -ne 'print "$1\\n" if / \/APPL\s\S+\s\s\/(\S+)\s/'

I was tracking it with regex debugger https://regex101.com/ and it looks like it looses it at \/(\S+)\s

I am using AIX on a system without the readlink command-line utility. I know I could use awk, but this is not the case here.


Solution

  • Prints the value of a symlink:

    readlink /APPL
    
    perl -le'print readlink( $ARGV[0] )' /APPL
    

    Converts to an absolute path with all symlinks (if any) resolved:

    readlink -e /APPL
    
    perl -MCwd=abs_path -le'print abs_path( $ARGV[0] )' /APPL
    
    perl -le'use Cwd qw( abs_path ); print abs_path( $ARGV[0] )' /APPL