[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: 'Inverse' chmod?



This also works, and might be a little more readable(?)

eval "perl -e 'printf \"%#o\", ((stat(\"$1\"))[2] & 0x1ff)'"

eval takes a pass at the string that follows and performs regular
shell variable substition before executing the command, so this is
the command that is executed:

perl -e 'printf "%#o", ((stat("<arg1>"))[2] & 0x1ff)'

where <arg1> is the actual value of the argument you passed.
The output looks a little better if you terminate it with a newline:

eval "perl -e 'printf \"%#o\\n\", ((stat(\"$1\"))[2] & 0x1ff)'"

In general, just escape anything (with \) that the shell might try to
interpret, like quotation marks, backslashes, dollar signs, semi-colons,
etc. (except those that you really *do* want the shell to interpret).

Marc

----------
Marc Mongeon <mongeon@bankoe.com>
Unix Specialist
Ban-Koe Systems
9100 W Bloomington Fwy
Bloomington, MN 55431-2200
(612)888-0123, x417 | FAX: (612)888-3344
----------
"It's such a fine line between clever and stupid."
   -- David St. Hubbins and Nigel Tufnel of "Spinal Tap"


>>> Ben Cranston <zben@ni.umd.edu> 07/15 4:27 PM >>>
> It works fine from the command line,
> but I tried it in a shell script with no luck.

> #! /bin/bash
> perl -e 'printf "%#o", ((stat($1))[2] & 0x1ff)'

> Needless to add I know as much about shell scripts
> as Hillary does about New York.

Yeah, the problem is that the $ is inside a ' (perl script).
Try something like:

perl -e 'printf "%#o", ((stat("'"$1"'"))[2] & 0x1ff)'


How it works:

Unix shells silently concatenate strings when they are adjacent.
So these are equivalent:

    foo "abc"
    foo "a"'b'"c"

The argument to perl -e above is three strings:

   'aaa'"bbb"'ccc'

where aaa  ==   printf "%#o", ((stat("      note trailing doublequote
      bbb  ==   $1                          argument gets substituted here
      ccc  ==   "))[2] & 0x1ff)             note leading doublequote

So, if you give "dd" as a filename the perl call gets an argument:

     printf "%#o", ((stat("dd"))[2] & 0x1ff)

which is kind of what you want???

-- 
Charles B. (Ben) Cranston
mailto:zben@ni.umd.edu 
http://www.wam.umd.edu/~zben 


-- 
Unsubscribe?  mail -s unsubscribe debian-user-request@lists.debian.org < /dev/null



Reply to: