簡體   English   中英

如何使用命令行參數從bash運行perl腳本?

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

我正在嘗試創建一個腳本,以批處理將一組用戶標記為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)

建議該命令正在運行而沒有任何參數。 此時,在嘗試解決該問題時,我實際上可以為每個用戶運行一次命令,有人可以幫忙嗎?

該Perl代碼中有很多編程錯誤-您得到的錯誤消息都是編譯器消息。 因此,該調用是正確的,但是Perl代碼是錯誤的。

您要做的是編輯Perl代碼,直到所有錯誤消息和警告都消失了。

編輯:在當時運行的環境中,Perl代碼是錯誤的。

抱歉,我花了這么長時間才意識到如此簡單的問題。 問題是字符串格式之一-文件名usernames.txt是由使用Windows的人創建的,並且具有dos格式(CRLF)。 我猜測參數以[username] [LF]的形式到達,並且搞砸了$ User變量的實例化。

我敢肯定,如果不來這里討論它,我永遠不會得到這個,我只是自己嘗試一下而已。

該決議只是為了:

sudo apt-get install tofrodos
sudo fromdos usernames.txt

然后原始腳本起作用了

非常感謝您的幫助。

編輯:現在已經有足夠的時間讓我將其從原始問題的編輯移到自己的答案

perl /home/chris/RT/bin/rt_set_privileged.pl "$LINE"

如果將文件設置為可執行文件,則還可以執行以下操作:

/home/chris/RT/bin/rt_set_privileged.pl "$LINE"

例如,

$ 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>>

您可以使用xargs代替while read

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

另一個解決方案:不要使用bash腳本,而是直接編輯perl腳本,打開您的usernames.txt文件並逐行讀取它-也可以解決您的問題。

像這里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);
}

更新:現在感謝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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM