Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

deleting a user name from a file

0 views
Skip to first unread message

Lokeey

unread,
May 9, 2008, 8:35:03 AM5/9/08
to begi...@perl.org
I'm writing this script to remove users from a file, the sudoers file, to be
more specific.

Here is what I have so far, not sure where to go from here.

*#!/usr/bin/perl -w

# this script removes a user from sudoers file

my $sites = $ARGV[0];
my $user = $ARGV[1];

for my $site ($#ARGV[1]){
open FILE, "$_/local/etc/sudoers" or die "cannot open sudoers for $sites:
$!";

print "Enter username you wish to remove from $sites sudoers.\n";

while (<FILE>) {s/$_//}
close FILE;
}*


--
"It's not what people call you, it's what you answer to."

Rob Dixon

unread,
May 9, 2008, 1:56:35 PM5/9/08
to begi...@perl.org, Lokeey
Lokeey wrote:
>
> I'm writing this script to remove users from a file, the sudoers file, to be
> more specific.
>
> Here is what I have so far, not sure where to go from here.
>
> *#!/usr/bin/perl -w

There's a star before your #! Is it a typing mistake?

Also,

use strict;

or there is little point in declaring variables with 'my'. And

use warnings;

is better than the -w command-line option.

> # this script removes a user from sudoers file

I suggest you write a simple usage help in here, for instance

die <<USAGE unless @ARGV;
This program should be run with command-line parameters like this:
:
:
USAGE

> my $sites = $ARGV[0];
> my $user = $ARGV[1];

Fine. But you don't use $sites or $user again. What does your program do exactly?

> for my $site ($#ARGV[1]){
> open FILE, "$_/local/etc/sudoers" or die "cannot open sudoers for $sites: $!";

You have written a 'for' iterator, but its list is $#ARGV[1] which doesn't make
any sense. I doubt if it will compile. Was $ARGV[0] supposed to be a list of
sites somehow?

> print "Enter username you wish to remove from $sites sudoers.\n";
>
> while (<FILE>) {s/$_//}
> close FILE;
> }*

(Ah, and another star. Delimiters for pasted code somehow?)

You prompt for a username, even though you have already used $ARGV[1] for $user.
And you don't read from STDIN to see what the user typed in response to the prompt.

I suggest you forget about @ARGV and prompting for and retrieving input. Hard
code a case where there is only a single site and username to be removed and get
that working, with our help as necessary. Then you can expand your program to
take parameters and prompt for input if that is what you want.

HTH,

Rob

Rob Dixon

unread,
May 9, 2008, 3:40:00 PM5/9/08
to begi...@perl.org, Lokeey
Lokeey wrote:
>
> Rob thanks for your input. Yes the * is a typo or pasted somehow. Rushed
> and wasn't paying attention, and worked on it some more after I sent
> that out and thought about how the ARGV and my other syntax, it didn't
> make sense to me. But I saw your comment just now and had already taken
> out the ARGV. Here's what I have so far. I'm just not sure how to lay
> out my loop, (while or if)
>
> /#!/usr/bin/perl -w

Always

use strict;
use warnings;

and Perl will tell you about many simple mistakes that you may have made.

> # this script removes a user from sudoers file
>

> print "Enter Site-ID: "; $site = <STDIN>;
> chomp($site);
>
> print "\nEnter the username to remove from /$site/local/etc/sudoers: " ;
> $user = <STDIN>;
> chomp($user);
>
> # confirm user to be removed
>
> print "\n$user will be removed from /$site/local/etc/suders. ";
>
> open FILE, "@$site/local/etc/sudoers" or die
> "cannot open sudoers for $sites. ";/
>
> *### not exactly sure how to lay out my loop or if i should use while or
> if statements###*
> /
> print "Done! User $user has been removed from $site sudoers. \n";/

OK, but you've now removed everything to do with the real purpose of the
program. I know it's tempting to write at least /something/, but all you've done
is to pretend to gather parameters for a procedure that you haven't yet defined.
Throw all this away, which is nothing to do with what the program really does,
and write


use strict;
use warnings;

my $site = 'sss';
my $user = 'uuu';

and then start to fill the rest out. I gave you many hints in my previous post
so please look at that again. The first thing is to define what your program
does and what command-line parameters it expects, so I think you should start by
writing the usage text as I showed:

die <<USAGE unless @ARGV;
This program should be run with command-line parameters like this:
:

And does this:
:
USAGE

and change that text until you are happy to commit to it and make your program
do what it says it does.

Rob

Lokeey

unread,
May 9, 2008, 2:49:13 PM5/9/08
to Rob Dixon, begi...@perl.org

Rob thanks for your input. Yes the * is a typo or pasted somehow. Rushed and
wasn't paying attention, and worked on it some more after I sent that out
and thought about how the ARGV and my other syntax, it didn't make sense to
me. But I saw your comment just now and had already taken out the ARGV.
Here's what I have so far. I'm just not sure how to lay out my loop, (while
or if)

*#!/usr/bin/perl -w

# this script removes a user from sudoers file

print "Enter Site-ID: "; $site = <STDIN>;
chomp($site);

print "\nEnter the username to remove from /$site/local/etc/sudoers: " ;
$user = <STDIN>;
chomp($user);

# confirm user to be removed

print "\n$user will be removed from /$site/local/etc/suders. ";

open FILE, "@$site/local/etc/sudoers" or die
"cannot open sudoers for $sites. ";*

*### not exactly sure how to lay out my loop or if i should use while or if
statements###*

*
print "Done! User $user has been removed from $site sudoers. \n";*

0 new messages