Converts UNIX file permissions to absolute mode. ( ie. rwsrwxrwx -> 4777 ). This is useful when you don't have the file locally .. just the output of the listing of the file. (Ex: Using Expect to access a remote system and obtaining data via shell commands )
#!/usr/local/bin/perl $a = <>; chomp( $a ); @p0{"-","r"} = ( 0, 400 ); @p1{"-","w"} = ( 0, 200 ); @p2{"-","x","s","S"} = ( 0, 100, 4100, 4000 ); @p3{"-","r"} = ( 0, 40 ); @p4{"-","w"} = ( 0, 20 ); @p5{"-","x","s","l"} = ( 0, 10, 2010, 2000 ); @p6{"-","r"} = ( 0, 4 ); @p7{"-","w"} = ( 0, 2 ); @p8{"-","x","t","T"} = ( 0, 1, 1001, 1000 ); @a = (\%p0,\%p1,\%p2,\%p3,\%p4,\%p5,\%p6,\%p7,\%p8); $perms = 0; $result = `ls -lLd $a | awk '{print \$1}' | cut -b2-10`; chomp( $result ); print "$result\n"; for ( $x = 0; $x <= 8; $x++ ) { $b = substr( $result, $x, 1 ); print "char -> $b position -> $x "; print "value -> $a[$x]{substr( $result, $x, 1 )}\n"; $perms = $perms + $a[$x]{substr( $result, $x, 1 )}; } print "perms -> $perms\n";

Replies are listed 'Best First'.
Re: File permission converter
by chipmunk (Parson) on Feb 15, 2001 at 03:02 UTC
    You may also be interested in this example from the documentation for stat:
    $mode = (stat($filename))[2]; printf "Permissions are %04o\n", $mode & 07777;
    This gets the mode directly, and then uses printf to print it as an octal number.

    Update: This example is from perl5.005_03's documentation. The documentation on PerlMonks is from 5.005_02.

      Thanks chipmunk! Did look at the documentation for stat.. but not see this sample. Appeared to me that you could only test the effective and real UID and GID permissions.
Re: File permission converter
by AgentM (Curate) on Feb 15, 2001 at 02:46 UTC
    Nice. ++ I've been looking for some reliable method for this other than ripping the ls code. I wondering, though, if you could nuke the shell call....
    AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.
      Man that post was totally sarcastic I cannot believe people have been voting negative on it - lighten up Monks!! -JSchlitz
Re: File permission converter
by merlyn (Sage) on Feb 15, 2001 at 04:52 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.