How can I run a perl script from bash using command line arguments?

I am trying to create a script to batch mark a user group as privileged in RT. I found a script on the RT RT wiki to add users to a group and provide them with privileged status, and then delete the bits associated with adding to the group. Perl script I have the following left:

#!/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 users in the file, one username per line. I don't know perl yet, so I tried to create a small bash script to skip the file and run the perl script once for each name. bash script, what it looks like 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 tried several methods to run this command, but to no avail. It’s annoying that I can take any of the commands from the command.sh file and paste them directly into the terminal without problems, this works great. When they run through a bash script, although I just get a bunch of these messages:

 [Tue Sep 4 07:43:56 2012] [critical]: _AddMember called with a parameter that 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 (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 (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) 

assuming the command runs without any parameters. At this point, I could run the command once for each user during the time I was trying to solve it, can anyone help?

+4
source share
5 answers

Come, I'm sad that it took me a long time to understand such a simple problem. The problem was formatting the strings. The usernames.txt file was created by someone using windows and had dos formatting (CRLF). I assume this argument appeared as [username] [LF] and screwed up an instance of the $ User variable.

I am sure that I would never have received this without coming here to discuss it, I gathered in circles, just trying to do it myself.

The permission was only:

 sudo apt-get install tofrodos sudo fromdos usernames.txt 

And then the original script worked

Thanks so much for helping the guys.

Edit: I have enough time to transfer this from editing the original question to my own answer

+2
source

You have a bunch of programming errors in this Perl code - the error messages you receive are compiler messages. So the call is correct, but the Perl code is incorrect.

What you want to do is edit the Perl code until all error messages and warnings disappear.

Edit: Perl code is incorrect in the environment in which it was running at that time.

+2
source
 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 instance,

 $ 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 can use xargs instead of while read :

 $ xargs -n1 -d\\n script.pl <usernames.txt arg=<<ikegami>> arg=<<Chris O'Kelly>> 
+1
source

Another solution: do not use a bash script, but directly edit the perl script, open the usernames.txt file and read it one by one - this will also solve your problem.

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
source

UPDATE: Now the possibility of spaces in user names is taken into account, thanks to the valuable tip from the Penis 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 
0
source

Source: https://habr.com/ru/post/1432380/


All Articles