in reply to Convert ls listing of -rwxr-xr-x into mode for stat
Assuming you have a valid 9 digit mode string with -'s where the bits are off, here's a small snippet that'll usually do what you want:
Not completely size optimized, but perhaps a bit easier to understand this way.$_ = "-r-x-w-rwx"; # Mode string in $_ my $bits = 0; for (split //) { $bits *= 2; $_ ne "-" and $bits++; } printf "My bits are: %o\n", $bits;
The basic technique is to loop through the string, checking each character to see whether it's a "-" (off bit) or not (on bit). Each time through the loop, multiply $bits by 2 to shift the previous result out of the way, and then add 1 if the lowest bit should be turned on.
This does not detect the setuid or setgid bits, or anything other than standard drwxrwxrwx bits correctly.
Alan
|
|---|