Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a perl script to set permissions on a couple of directories.

I received the details of what needs to be done from someone I'm trying to help out. I'll be the first to admit I'm not a Unix guy, so please excuse my ignorance. I have figured out how to do everything below EXCEPT set the file permissions. I have found documentation on chown, but am not sure how to retrieve the uid and gid at runtime. My Linux book says there's a whoami command which is the same as id -un however, I cannot find an equivalant perl method for either of these.

Any assistance in figuring this out is appreciated. Below are the commands I am attempting to script. Specifically I do not know how to do the Chown stuff in Perl.

Cd /home/myname Mkdir www Cd www Mkdir htdocs Mkdir logs Cd .. Chown myname:nobody www –R Cd .. Chown myname:nobody {USER}

Thanks.

Replies are listed 'Best First'.
Re: whoami?
by AgentM (Curate) on Nov 20, 2000 at 22:15 UTC
    If you're looking for permission sets, then you're interested in chmod instead. chown changes the owner of a file. you can also look into the perl standard variables $>, $<, and $(, $), which return uids and gids, respectively.
    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.
(jeffa) Re: whoami?
by jeffa (Bishop) on Nov 20, 2000 at 22:06 UTC
    sorry - bad information

    please refer below for correct and useful answers

      The UID and GID are kept in the standard Perl variables $</$> and $(/$). You can then discover the username via getpwuid:
      print "Your username is: ", getpwuid($<) || "unknown???", "\n";
      This allows you to get this information without ever needing to spawn an external process.
Re: whoami?
by Kurious (Novice) on Nov 21, 2000 at 01:52 UTC
    I'm the one the posted this. I forgot to log in first. Sorry.

    Anyway, thanks A LOT for the help. The part I am still stumped on is, the :nobody stuff.

    I understand that the -R indicates that the Chown command should be repeated for all sub-directories. Is there an equivalant switch in Perl or will I need to write the recursion myself?

    Also, again, what is the :nobody and how do I account for it in my script?

    Thanks in advance for your help. Hopefully one day I'll actually know enough to be able to help someone else here.

    Just, Kurious

      Both chmod and chown implement a recursive engine already, using, as you said, -R. I'm still curious as why you are changing the owner, though. Which user are you changing this from?
      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.
        It looks to me more like he's simply trying to change the group. Giving the nobody group permission to view the folder, so that the Apache web server can see the stuff in there. Of course, that's assumption, not necessarily close to being right. <br
        If that is the case, I would suggest you use chgrp instead of chown, easier and gives a better error message (such as "You are not a part of the group nobody"). At least try this from the command line first, and see if that's what's causing the problem :-)
        OK, here's the scoop. The truth is am a Windows Java / C++ programmer, and I don't know very much about Perl and less about Unix.

        (Pause here for snicker and sneer break.)

        Now that you've gotten that out of your system. I've been trying to learn Perl and I'm in the process of putting together a Linux box, so maybe I'll actually be able to answer some questions here some day.

        However, in the meantime, I've got a friend that asked if I could help him automate some commands that he is having to type manually everytime he sets up a user on his web server. This is a script for priviliged users alone and not for the public at large.

        I believe I'm down to the last issue, thanks to all the help (and fast too!) I have gotten here. The last thing I have to do is implement the recursion by hand because it does not appear that the chown command actually supports the -R switch. If it does, I have not found documentation on it yet.

        So, I hope that kind of puts everything in perspective.

        BTW, why does my posting say I have a Reputation of 0? Is that because I'm all questions and no answers?

        Thanks again for all the help.

        Just,
        Kurious

      Check the man pages for the Unix 'chown' command. If you specify a user:group, the GID of the file is set to 'group' (the part that follows the colon).

      If you want to do recursion, you should probably look into File::Find, which does all of that for you.