Power

 View Only
Expand all | Collapse all

A test to see if something is a regular file incorrectly reports that it is when actually it's a link; why?

  • 1.  A test to see if something is a regular file incorrectly reports that it is when actually it's a link; why?

    Posted Thu June 13, 2024 06:14 PM
    On a VIOS:
    # ls -al /home/padmin/.profile lrwxrwxrwx 1 root system 21 Mar 10 2023 /home/padmin/.profile -> /usr/ios/cli/.profile # ls -al /usr/ios/cli/.profile -r--r--r-- 1 root system 6799 Aug 09 2022 /usr/ios/cli/.profile
    So far, so good.  But, why does a test to see if /home/padmin/.profile is a regular file, report that it is (when it's actually a link)?
    # if [[ -f /home/padmin/.profile ]];then^Jecho File^Jelse^Jecho Not a file^Jfi
    File
    The man page for test  says that the -h and -L flags are supposed to return a True exit value if the specified Filename exists and is a symbolic link.  As expected:
    # if [[ -h /home/padmin/.profile ]];then^Jecho Link^Jelse^Jecho Not a link^Jfi Link # if [[ -L /home/padmin/.profile ]];then^Jecho Link^Jelse^Jecho Not a link^Jfi Link
    I get that the target of /home/padmin/.profile is a regular file... but /home/padmin/.profile itself is not!  Is the above behavior intentional, or a bug?


    ------------------------------
    Erich Wolz
    ------------------------------


  • 2.  RE: A test to see if something is a regular file incorrectly reports that it is when actually it's a link; why?

    Posted Fri June 14, 2024 08:06 AM

     

    It's not a bug – it's a feature, as they say.

     

    A link to a file  is intended to behave as a file does in almost all cases, so test -f agrees that it is a file.

    If it did not, then code checking for files would always have to check for links, and then check the thing that the links point to, needlessly complicating everything.

     

    In the relatively rare case in which one actually needs to know whether or not it is a link, test -h/test -L provides that capability.

     

     

     






  • 3.  RE: A test to see if something is a regular file incorrectly reports that it is when actually it's a link; why?

    Posted Fri June 14, 2024 04:17 PM

    As Michael said, this seems to be a "feature".
    Please know that bash in AIX as well as bash in Linux behave in the same way, so this seems to be a standard behavior.

    # ls -l dir*/file*
    -rw-r--r-- 1 root root  6 Jun 14 15:06 dir1/file1
    lrwxrwxrwx 1 root root 19 Jun 14 15:07 dir2/file1 -> /tmp/abe/dir1/file1
    
    # if [ -f dir1/file1  ] ; then  echo TRUE ; else echo FALSE; fi
    TRUE
    
    # if [ -f dir2/file1  ] ; then  echo TRUE ; else echo FALSE; fi
    TRUE
    
    # if [ -h dir2/file1  ] ; then  echo TRUE ; else echo FALSE; fi
    TRUE
    
    # if [ -h dir1/file1  ] ; then  echo TRUE ; else echo FALSE; fi
    FALSE
    
    # cat /etc/redhat-release 
    Red Hat Enterprise Linux release 9.3 (Plow)
    


    ------------------------------
    Abraham Alvarez
    ------------------------------