我正在尝试创建一个脚本来批量将一组用户标记为RT中的特权用户。我在RT wiki上找到了一个脚本,用于将用户添加到一个组并赋予他们特权状态,然后删除了与添加到组有关的部分。我剩下的perl脚本是:
#!/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
我有一个文件,里面每行都是一个用户名。 因为我还不会perl,所以我尝试编写了一个简单的bash脚本,通过循环遍历文件,并针对每个用户名运行一次perl脚本。目前这个Bash脚本的代码如下:
#!/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"
您可以看到,我尝试了很多方法来运行命令,但都没有成功。令人烦恼的是,我可以在之后从commands.sh文件中取出任何命令并直接粘贴到终端中,这样可以正常工作。但是当它们通过bash脚本运行时,我只会收到一堆这样的消息:
[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)
提示该命令在没有任何参数的情况下被运行。在这一点上,我实际上已经花费了解决它的时间来为每个用户运行一次命令,有人可以帮忙吗?
print "arg=<<$ARGV[0]>>\n";
来查看传递了什么。我一直以为参数没有到达 perl 脚本中,但似乎实际上是到达了。在开头加上这个之后,我的输出就是:>>g=<<3Cubed
不,那不是拼写错误,打印结果只是看起来很奇怪。所以,参数已经传递给脚本了……我不明白。如果我将/home/chris/RT/bin/rt_set_privileged.pl ChrisO
粘贴到终端中,则正常工作。但如果我将它粘贴到 bash 脚本中并运行它,则出现此问题。 - Chris O'Kellyxargs -n1 rt_set_privileged.pl < usernames.txt
。 - reinierpost