Reputation: 1893
I am trying to create a script to batch mark a group of users as privileged in RT. I found a script on the RT wiki for adding users to a group and giving them the privileged status, then removed the bits of it to do with adding to a group. The perl script I have remaining is:
#!/usr/bin/perl
# Usage: ./rt_set_privileged.pl <username>
use strict;
use lib "/var/www/ticket.ourcompany.com/lib";
use RT;
use RT::User;
use RT::Interface::CLI;
RT::LoadConfig();
RT::Init();
# Create RT User Object
my $user = new RT::User($RT::SystemUser);
# Instantiate the user object with the user passed as parameter
my $usertoadd = $ARGV[0];
$user->Load( $usertoadd );
# Set the privileged flag (1=privileged, 0=unprivileged)
$user->SetPrivileged(1);
exit 1
I have the users in a file, one username per line. I don't know perl as of yet, so I tried to create a little bash script to loop through the file and run the perl script once per name. The Bash script as it looks right now:
#!/bin/bash
touch commands.sh
cat usernames.txt | while read LINE ; do
N=$((N+1))
echo /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\" >> commands.sh
/home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
perl /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
perl -w /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
eval /home/chris/RT/bin/rt_set_privileged.pl \"$LINE\"
perl "/home/chris/RT/bin/rt_set_privileged.pl $LINE"
done
echo "Processed $N users"
As you can see I have tried quite a few methods to get the command to run, but to no avail. The annoying thing is, I can take any of the commands from the commands.sh file afterwards and paste them straight into the terminal without a problem, this works fine. When they are run through the bash script though, I just get a bunch of these messages:
[Tue Sep 4 07:43:56 2012] [critical]: _AddMember called with a parameter that's not an integer. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:912)
[Tue Sep 4 07:43:58 2012] [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 970. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:968)
[Tue Sep 4 07:43:58 2012] [error]: Group::HasMember was called with an argument that isn't an RT::Principal or id. It's (undefined) (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:973)
[Tue Sep 4 07:43:58 2012] [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 970. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:968)
[Tue Sep 4 07:43:58 2012] [error]: Group::HasMember was called with an argument that isn't an RT::Principal or id. It's (undefined) (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:973)
[Tue Sep 4 07:43:58 2012] [warning]: Use of uninitialized value in concatenation (.) or string at /var/www/ticket.ourcompany.com/lib/RT/User.pm line 341. (/var/www/ticket.ourcompany.com/lib/RT/User.pm:341)
[Tue Sep 4 07:43:58 2012] [critical]: User is neither privileged nor unprivileged. something is drastically wrong. (/var/www/ticket.ourcompany.com/lib/RT/User.pm:341)
[Tue Sep 4 07:43:58 2012] [warning]: Use of uninitialized value $new_member in pattern match (m//) at /var/www/ticket.ourcompany.com/lib/RT/Group.pm line 911. (/var/www/ticket.ourcompany.com/lib/RT/Group.pm:911)
suggesting that the command is being run without any parameters. At this point I could have actually run the command once for each user in the time I have been trying to solve it, can anyone help?
Upvotes: 5
Views: 15936
Reputation: 1797
You have a bunch of programming errors in that Perl code — the error messages you get are all compiler messages. So the call is correct, but the Perl code is wrong.
What you want to do is edit the Perl code until all error messages and warnings are gone.
Edit: The Perl code is wrong in the environment it runs in at that time.
Upvotes: 2
Reputation: 1893
Arrgh, I'm sad it took me this long to realize such a simple issue. The problem was one of string formatting - The file usernames.txt was created by someone using windows and had dos formatting (CRLF). I'm guessing the argument was arriving as [username][LF] and screwing up the instantiation of the $User variable.
I'm sure I never would have gotten this without coming here to discuss it, I was going round in circles just trying it on my own.
The resolution was just to:
sudo apt-get install tofrodos
sudo fromdos usernames.txt
And then the original script worked
Thanks very much for the help guys.
Edit: enough time has now passed for me to move this from an edit of the original question to it's own answer
Upvotes: 2
Reputation: 6571
UPDATE: Now accounts for possibility of spaces in usernames, thanks to valuable tip from Peniwize's Blog.
OLD_IFS=$IFS
IFS=$'\n'
priveleged=( $( cat usernames.txt) )
for i in "${priveleged[@]}"
do
perl /home/chris/RT/bin/rt_set_privileged.pl $i
done
IFS=$OLD_IFS
Upvotes: 0
Reputation: 1797
Another solution: Don't use a bash script, but directly edit the perl script, open your usernames.txt file and read it line by line - that'll solve your problem, too.
Like here http://www.perlfect.com/articles/perlfile.shtml:
my $file = "usernames.txt";
open USERS, "<$file" or die "Can't open $file ($!)";
while(<USERS>) {
my $usertoadd = $_;
chomp $usertoadd;
$user->Load( $usertoadd );
$user->SetPrivileged(1);
}
Upvotes: 1
Reputation: 385647
perl /home/chris/RT/bin/rt_set_privileged.pl "$LINE"
If the file was made executable, you can also do:
/home/chris/RT/bin/rt_set_privileged.pl "$LINE"
For example,
$ cat usernames.txt
ikegami
Chris O'Kelly
$ cat script.pl
#!/usr/bin/env perl
print "arg=<<$ARGV[0]>>\n";
$ while read LINE ; do script.pl "$LINE"; done <usernames.txt
arg=<<ikegami>>
arg=<<Chris O'Kelly>>
You could use xargs
instead of while read
:
$ xargs -n1 -d\\n script.pl <usernames.txt
arg=<<ikegami>>
arg=<<Chris O'Kelly>>
Upvotes: 1