Category: System Utilities
Author/Contact Info Chris Monahan aka 'Maze' ForeverWatcher@googlemail.com
Description:

this little script assumes that you have a /home/guest directory and a /home/default directory

it cleans out the /home/guest directory and saves the content of various directories under a time-stamped folder, it then mirrors the /home/default directory in the /home/guest and sets the appropriate modes,uid's and gid's.

#!/usr/bin/perl
#reguest.pl
#by Chris Monahan
#this is free software, you can distribute it under the same terms as 
+perl itself
use warnings;
use strict;

my @dirs = ("Music", "Pictures", "Documents", "Desktop");

print "preserving guest content under a timestamped folder \n \n";

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdist) =
     gmtime(time);
     
     $min++;
     $hour++;
     $mday++;
     $sec++;
     $mon++;
     $year++;
     
my $count = 0;
     
my @mkdir = ( "$year:$mon:$mday", "$hour:$min:$sec");
chdir ("/home/guest-content");
while ($count ne @mkdir){
    system "mkdir $mkdir[$count]";
    chdir "$mkdir[$count]";
    $count ++;
}
chdir ("/home");

$count = 0;

while ($count ne @dirs){
    my $dest = "guest-content/$year:$mon:$mday/$hour:$min:$sec/";
    #unless (
    system ("mv guest/$dirs[$count]/ $dest"
    )
    ;
    #){
    #}
    $count++;
}

print "purging configuration settings \n ";
system ("rm -R  guest");

print "restoring guest home \n ";
system ("mkdir guest");
system ("cp -R default/* guest");


$count = 0;
while ($count ne @dirs){
    system ("mkdir guest/$dirs[$count]");
    $count++;
}

print "restoring guest account password and ownership details \n";

#system("usermod -p password guest");
system ("chown -R guest:guest guest");

print "done \n"
Replies are listed 'Best First'.
Re: Guest Cleanup
by jdporter (Paladin) on Oct 01, 2006 at 14:09 UTC
    my $count = 0; my @mkdir = ( "$year:$mon:$mday", "$hour:$min:$sec"); chdir ("/home/guest-content"); while ($count ne @mkdir){ system "mkdir $mkdir[$count]"; chdir "$mkdir[$count]"; $count ++; }

    O.k., you've got some things to learn, so let's start with the above.

    First and foremost: don't use ne for comparing numbers. It and its kin — eq, lt, gt, etc. — are for comparing strings only. For numbers, use !=, ==, <, >, etc. (See Equality Operators.)

    Second, while the use of $[ is officially discouraged, it's still much safer to relate indices of an array to $#a rather than scalar(@a).

    Third, stylistically, you might want to watch out for unnecessary quoting of scalars.

    Fourth, don't shell out to execute a command that Perl has built in.

    Fifth, you can use the C-style for loop as a syntactic condensation of your $count testing and incrementing.

    Putting the above together:

    my @mkdir = ( "$year:$mon:$mday", "$hour:$min:$sec"); chdir ("/home/guest-content"); for ( my $count = 0; $count <= $#mkdir; $count++ ) { mkdir $mkdir[$count]; chdir $mkdir[$count]; }

    Sixth, unless the array is very long, it's more idiomatic Perl to use iterate the index over a list of the indices using the range operator:

    for my $count ( 0 .. $#mkdir ) { mkdir $mkdir[$count]; chdir $mkdir[$count]; }

    Seventh, it's bad style to iterate through arrays by index, unless for some reason you need the index for something else (such as iterating through multiple arrays in parallel). You can iterate on the elements of the array directly:

    for my $elem ( @mkdir ) { mkdir $elem; chdir $elem; }

    Eighth, consider eliminating variables which are only used once. You can almost always operate on a list as well as on an array.

    for my $elem ( "$year:$mon:$mday", "$hour:$min:$sec" ) { mkdir $elem; chdir $elem; }

    Lastly, check to see if there's a module that already does what you're trying to do.

    use File::Path; mkpath( "$year:$mon:$mday/$hour:$min:$sec" );

    One more thing: Consider that using ":" (colon) in pathnames is non-portable. It's illegal on Windows, and is the path separator on Mac.

    Update: One more thing... You should always check the result of system calls, including the built-in ones.

    mkdir "guest" or die "$! - mkdir guest"; system "cp -R default/* guest" and die "cp -R failed";

    We're building the house of the future together.
      $elem is a lousy variable name, just as bad as $count. Also note that mkpath throws exceptions on some errors; when switching to it instead of mkdir, consider whether or not you want to catch them with eval { }.
        $elem is a lousy variable name, just as bad as $count.

        I respectfully disagree. It's bad, but not as bad. $elem carries very little information; $count is actually misleading.

        In such situations, I tend to use $_, which has the advantage of being known, a priori, as a list iterator, by virtue of its special relation to foreach and while(<>).

        for ( "$year:$mon:$mday", "$hour:$min:$sec" ) { mkdir $_ or die "mkdir $_ - $!"; chdir $_ or die "chdir $_ - $!"; }
        We're building the house of the future together.
Re: Guest Cleanup
by jwkrahn (Abbot) on Oct 02, 2006 at 00:23 UTC
    it cleans out the /home/guest directory and saves the content of various directories under a time-stamped folder, it then mirrors the /home/default directory in the /home/guest and sets the appropriate modes,uid's and gid's.
    Doesn't your system have an /etc/skel directory for default files like other Unix/Linux systems?
    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdist) = gmtime(time); $min++; $hour++; $mday++; $sec++; $mon++; $year++; my $count = 0; my @mkdir = ( "$year:$mon:$mday", "$hour:$min:$sec");
    Say that you run this code at one minute before midnight on the 28th of February 2007 your two dir names will be "108:2:29" and "24:60:60". Are you sure that you want to do that? And why declare $wday, $yday and $isdist if you are not going to use them?
Re: Guest Cleanup
by Maze (Sexton) on Oct 04, 2006 at 22:50 UTC

    wow, i'm pleasantly surprised at the response to my poky little script, i suppose I should probably write something up then

    as you will have spotted this is far from a piece of academic code, i put this together quickly when I wanted to demo linux to people at school.

    Didn't actually get round to demo-ing, but needless to say it is worth considering creating a -proper- guest management program, sounds like fun

    I do actually know about some of the things mentioned, eg: the for loop, and $count being the lesser way of iterating, but I learnt it that way and when I wrote this I hadn't got into the habit of the better way - the module file::path looks interesting, I think the file::* manuals should be required reading for those looking to make utilities in perl

    I didn't know about /etc/skel... one of those other things that i've now learnt via obscura...

    I still haven't got my head round Objects and Classes in perl, perl doesn't seem like a really OO language to me, correct me at your leisure

    oh yes, I used system since I was being very economical with the amount of learning needed, it was quite late when I wrote this, and I was thinking shell script but with more power