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
In reply to Re: Convert ls listing of -rwxr-xr-x into mode for stat
by ferrency
in thread Convert ls listing of -rwxr-xr-x into mode for stat
by hackdaddy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |