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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12272084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:11:53  来源:igfitidea点击:

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

perlbashrt

提问by Chris O'Kelly

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:

我正在尝试创建一个脚本来批量标记一组用户在 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

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:

我有一个文件中的用户,每行一个用户名。我目前还不了解 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"

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:

如您所见,我尝试了很多方法来让命令运行,但都无济于事。令人讨厌的是,之后我可以从 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)

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?

提示该命令在没有任何参数的情况下运行。在这一点上,我实际上可以在我试图解决它的时候为每个用户运行一次命令,有人可以帮忙吗?

采纳答案by Chris O'Kelly

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.

啊,我很难过我花了这么长时间才意识到这么简单的问题。问题是字符串格式之一 - 文件 usernames.txt 是由使用 Windows 的人创建的,并且具有 dos 格式(CRLF)。我猜这个论点是作为 [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

编辑:现在已经过去了足够的时间,我可以将其从原始问题的编辑转移到自己的答案

回答by cslotty

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.

你在那个 Perl 代码中有一堆编程错误——你得到的错误消息都是编译器消息。所以调用是正确的,但是 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 代码在当时运行的环境中是错误的。

回答by cslotty

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

回答by ikegami

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 xargsinstead of while read:

您可以使用xargs代替while read

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

回答by David

UPDATE: Now accounts for possibility of spaces in usernames, thanks to valuable tip from Peniwize's Blog.

更新:现在考虑了用户名中空格的可能性,这要归功于Peniwize 博客的宝贵提示。

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