從裏到外構建它。
use String::ShellQuote qw(shell_quote);
my $host = 'HOST';
my $path_to_xml = '/home/harrikr/Scripts/TcServerScripts/data.xml';
my $virtualhost1 = 'VIRTUALHOST1';
my $perl_prog = <<'__EOI__';
BEGIN { my $vh = shift(@ARGV); }
s/xmlNamespaceAware="false">\K/<Alias>\Q$vh\E</Alias>/g;
__EOI__
my $perl_cmd = sprintf q{perl -i -pe%s %s %s},
shell_quote($perl_prog),
shell_quote($virtualhost1),
shell_quote($path_to_xml);
my $su_cmd = sprintf q{sudo su - -c%s},
shell_quote($perl_cmd);
my $ssh_cmd = sprintf q{ssh -t %s %s},
shell_quote($host),
shell_quote($su_cmd);
system($ssh_cmd);
您可能注意到我通過使用\ K regex原子縮短了您的Perl程序。這需要5.10,所以隨時恢復這一變化。
成爲root似乎是不必要的。由於正在編輯的文件似乎屬於harrikr
,爲什麼不簡單地ssh作爲那個人?我認爲這也意味着-t
變得不必要。
use String::ShellQuote qw(shell_quote);
my $host = '[email protected]';
my $path_to_xml = '/home/harrikr/Scripts/TcServerScripts/data.xml';
my $virtualhost1 = 'VIRTUALHOST1';
my $perl_prog = <<'__EOI__';
BEGIN { my $vh = shift(@ARGV); }
s/xmlNamespaceAware="false">\K/<Alias>\Q$vh\E</Alias>/g;
__EOI__
my $perl_cmd = sprintf q{perl -i -pe%s %s %s},
shell_quote($perl_prog),
shell_quote($virtualhost1),
shell_quote($path_to_xml);
my $ssh_cmd = sprintf q{ssh %s %s},
shell_quote($host),
shell_quote($perl_cmd);
system($ssh_cmd);
「_I我試圖用perl -pi -e來編輯一個XML文件。」好的,但是編輯該文件的目標是什麼?你有什麼錯誤嗎? – sidyll 2012-03-28 18:10:09
定義「不起作用」。並且請告訴我,問題在於你試圖訪問的服務器有一些明智的安全性,而且你不能sudo root。 – 2012-03-28 18:10:41
我收到抱怨操作員的錯誤消息。 運算符在Untitled.pl第7行附近找到的字符,在「」ssh -t harrikr \ @ \「sudo su - root -c perl -pi -e's {xmlNamespaceAware =」false「 在Untitled.pl第7行,在「false」附近>} {xmlNamespaceAware =「」 (在運行之前丟失了運算符?)在運算符期望的Untitled.pl第7行附近,「>> {xmlNamespaceAware =」false「 –
Badboy
2012-03-28 18:31:54