简体   繁体   English

如何使用命令行参数从bash运行perl脚本?

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

I am trying to create a script to batch mark a group of users as privileged in RT. 我正在尝试创建一个脚本,以批处理将一组用户标记为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. 我在RT Wiki上找到了一个脚本,用于将用户添加到组中并赋予他们特权状态,然后删除其中的部分以添加到组中。 The perl script I have remaining is: 我剩下的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

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. 我到目前为止还不了解perl,所以我尝试创建一个小的bash脚本来循环遍历文件,并为每个名称运行一次perl脚本。 The Bash script as it looks right now: 现在看起来的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"

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. 烦人的事情是,我以后可以从commands.sh文件中提取任何命令,然后将它们直接粘贴到终端中而不会出现问题,这很好。 When they are run through the bash script though, I just get a bunch of these messages: 但是,当它们通过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)

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? 此时,在尝试解决该问题时,我实际上可以为每个用户运行一次命令,有人可以帮忙吗?

You have a bunch of programming errors in that Perl code — the error messages you get are all compiler messages. 该Perl代码中有很多编程错误-您得到的错误消息都是编译器消息。 So the call is correct, but the Perl code is wrong. 因此,该调用是正确的,但是Perl代码是错误的。

What you want to do is edit the Perl code until all error messages and warnings are gone. 您要做的是编辑Perl代码,直到所有错误消息和警告都消失了。

Edit: The Perl code is wrong in the environment it runs in at that time. 编辑:在当时运行的环境中,Perl代码是错误的。

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). 问题是字符串格式之一-文件名usernames.txt是由使用Windows的人创建的,并且具有dos格式(CRLF)。 I'm guessing the argument was arriving as [username][LF] and screwing up the instantiation of the $User variable. 我猜测参数以[username] [LF]的形式到达,并且搞砸了$ User变量的实例化。

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 编辑:现在已经有足够的时间让我将其从原始问题的编辑移到自己的答案

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代替while read

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

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. 另一个解决方案:不要使用bash脚本,而是直接编辑perl脚本,打开您的usernames.txt文件并逐行读取它-也可以解决您的问题。

Like here http://www.perlfect.com/articles/perlfile.shtml : 像这里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);
}

UPDATE: Now accounts for possibility of spaces in usernames, thanks to valuable tip from Peniwize's Blog . 更新:现在感谢Peniwize的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用命令行的情况下运行 Perl 脚本? - How can I run a Perl script without using the command line? 使用bash脚本中的空格处理Perl命令行参数? - Handling Perl command line arguments with spaces from a bash script? 如何让Perl脚本接受来自STDIN和命令行参数的参数? - How can I get a Perl script to accept parameters from both STDIN and command line arguments? 如何将包含特殊字符的命令行参数传递给通过Perl或Bash启动的进程? - How can I pass command-line arguments containing special characters to a process launched via Perl or Bash? 如何从Perl运行和结束script()linux命令? - How can I run and end the script() linux command from Perl? 如何创建Perl脚本以获取一些“命名”命令行参数? - How can I create a Perl script to get some “named” command line arguments? 如何管理用 Perl 编写的脚本的命令行参数/变量? - How can I manage command line arguments/variables for a script written in Perl? 如何将命令行参数传递给 Perl 程序? - How can I pass command-line arguments to a Perl program? 如何在Perl中混合&lt;&gt;的命令行参数和文件名? - How can I mix command line arguments and filenames for <> in Perl? 如何使用perl从命令行传递目录路径作为参数? - How to pass directory path as arguments from command line using perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM