5

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?

8
  • Heres something interesting, I tried adding print "arg=<<$ARGV[0]>>\n"; to the perl script to see what was getting passed. I had been assuming the arg doesn't make it to the perl script but it seems it actually is. With that at the beginning, my output is: >>g=<<3Cubed no, thats not a typo, the print just seems to turn out really weird. So the argument is getting to the script... i don't understand. If I paste /home/chris/RT/bin/rt_set_privileged.pl ChrisO into the terminal it works fine. If I paste it into a bash script and run it, I get this problem. Commented Sep 4, 2012 at 23:09
  • Can you try and print ARGV[0] without the << >> in the print statement? In fact, I would make a program that does two things, prints the number of arguments passed ( scalar(@ARGV) ) and then prints $ARGV[0]. Call that program from a bash script like the one you have for this and see what you get. Commented Sep 4, 2012 at 23:19
  • I get arg=3Cubed, which is perfect. That means it should then do $user->Load( 3Cubed ); which should work fine. If I type in sudo ./rt_set_privileged.pl 3Cubed I get no error messages, the same arg=3Cubed output, and the user is added to the privileged group. Could this be to do with string encoding maybe? Commented Sep 4, 2012 at 23:25
  • 1
    it WAS string encoding! I'll post this in it's own answer Commented Sep 4, 2012 at 23:37
  • 1
    @Chris O'Kelly: An easier way to call the Perl script for each user is xargs -n1 rt_set_privileged.pl < usernames.txt Commented Oct 10, 2012 at 11:19

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

8 Comments

Hi,but if I run the script straight from terminal, ie, if I type in /home/chris/RT/bin/rt_set_privileged.pl ChrisO and run it, it works fine, so the perl script must be working. The error messages seemed to suggest (to me at least) that an expected parameter was undefined, which supported my original theory that the command line argument wasn't making it all the way, but now I am not sure.
Ok - let's take f.ex. this warning: [warning]: Use of uninitialized value $principal in pattern match (m//) at /var/www/ticket.minecorp.com/lib/RT/Group.pm line 970 -> it says that in the module Group.pm the variable $principal is empty, but shouldn't be. So there probably is an error in the API use of Group.pm - go to Group.pm and see what it expects!
The RT-library probably expects some more arguments/options/function calls or whatever in order to do what you expect - in that your relation to a missing argument might be right.
Well - one more possibility: If two different calls behave differently, it might be caused by the environment (which is a common trap in scripting) - you say a direct call works, but a call from a bash script won't work? So what's the difference in the two environments?
I don't understand what you mean... as far as I can see that error message suggests that the value $principle is set from the output of another function and that function failed. It would seem to me that the instantiation of the $User variable failed and the error messages here cascaded from there. I fail to see how this can be to do with the use of the API when I can run this script from the same directory, with the same command line argument (as far as I can tell) and have it run successfully.
|
1
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>>

6 Comments

Hi, both scripts are executable already, and I have tried both those lines in my bash script. I get the same error messages.
@Chris O'Kelly, Then you have another error somewhere else. You asked how to pass the arguments, and that's what I answers.
You're right, your answer does properly address the title of my question, and for that reason you had my upvote :)
Not just the title. The whole question was you showing you didn't know how to do it. In fact, it's the only question for which you provide any useful amount of information. If you have further problems with your script, please start a new question, providing a minimal, runnable demonstration of the problem.
I'm not sure where the hostility is coming from, I asked a question, I was thankful for your answer, but you appear to be more interested in having an argument over what my question was about. If you read the rest of the thread you would see that I have solved the issue I was having and posted my own solution, so I am not sure what "further problems" you are referring to, but in terms of a runnable demonstration of the problem, did you not notice the full script included in my question?
|
1

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);
}

1 Comment

yeah this would probably have been a better solution. But learning Perl is next weeks' task.
0

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

4 Comments

I just tried this, but I get the same error messages. The weird thing is - there's no spaces in the usernames. A few have $'s in them, which I can see giving some trouble, but this issue persists even when I remove those from the file and just have alpha usernames.
@ChrisO'Kelly Strange. What if you replace the line with the call to perl with echo $i? It should show you a list of all the names in your file.
I tested it with a perl script that uses the command line arg and it seems to work.
@ikegami Updated solution to account for spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.