scott@talarian.UUCP (Scott Weitzenkamp) (10/05/90)
I am trying to write a shell script (either in sh or csh on SunOS 4.0.3) that can clone the file protection from one file to another. I'd like to do something like this: chmod `get_protection old_file_name` new_file_name Is there a (easy) way from sh or csh to retrieve the file protection of a file in a format that chmod can understand? It doesn't look to me like ls(1) will do the trick. What I wound up doing was to write a C program to print a file's protection in octal: /* getmod.c -- print file protection of a file in octal */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> int main(argc, argv) int argc; char **argv; { struct stat stat_buf; if (argc != 2) { fprintf(stderr, "Usage: %s file\n", argv[0]); return 1; } /* if */ if (stat(argv[0], &stat_buf) != 0) { perror("stat"); return 1; } /* if */ printf("0%o\n", stat_buf.st_mode & 0777); return 0; } /* main */ Now I can do what I want: chmod `getmod old_foo.c` new_foo.c I have a feeling this is probably easy to do in Perl, but I not really interested in a Perl solution because I cannot guarantee that our customers will have Perl (I suppose I could put Perl on our product tape, though). Do anybody see an easier way to do this? -- Thanks in advance... Scott Weitzenkamp, Talarian Corporation, Mountain View, CA uunet!talarian!scott (415) 965-8050 "Welcome to the late show, starring NULL and void" -- Men At Work
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (10/06/90)
In article <277@talarian.UUCP> scott@talarian.UUCP (Scott Weitzenkamp) writes:
: Now I can do what I want:
:
: chmod `getmod old_foo.c` new_foo.c
:
: I have a feeling this is probably easy to do in Perl, but I not
: really interested in a Perl solution because I cannot guarantee that
: our customers will have Perl (I suppose I could put Perl on our
: product tape, though).
Yes, it's certainly easier in Perl:
chmod (stat("old_foo.c"))[2], "new_foo.c";
You have the added advantage of not spawning two processes per file.
I would recommend you put Perl on your product tape. Others have done this--
talk to Tom Christiansen of Convex in particular. You can even go as far
as to pre-compile it for them, or at least supply a ready-made config.sh.
They'll be really impressed!
I'm not at all biased, of course... :-)
Larry Wall
lwall@jpl-devvax.jpl.nasa.gov
tchrist@convex.COM (Tom Christiansen) (10/07/90)
In article <277@talarian.UUCP> scott@talarian.UUCP (Scott Weitzenkamp) writes: > I am trying to write a shell script (either in sh or csh on SunOS 4.0.3) >that can clone the file protection from one file to another. I'd like >to do something like this: > > chmod `get_protection old_file_name` new_file_name [Solution in C deleted.] > I have a feeling this is probably easy to do in Perl, but I not > really interested in a Perl solution because I cannot guarantee that > our customers will have Perl (I suppose I could put Perl on our > product tape, though). You're right -- it *is* easy in perl. Error checking aside, this should do the trick: perl -e 'chmod(((stat(shift))[2] & 0777), @ARGV)' f0 f1 f2 f3 ... This copies f0's perms to the rest of the list, although it doesn't propagate set[ug]id or sticky bits -- use 07777 for that behavior. How many times will you need to cook up another solution in C because your customers don't have perl? I know that this is just one little thing, but after running into dozens of such little things, I realized I was making my own life unnecessarily difficult because of that very line of thinking, so I got perl added to my company's standard utilities tape. I suggest you try to do the same. Just out of curiosity, what other companies supply perl? --tom -- "UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." [Doug Gwyn]