Perl 检查远程服务器中是否存在目录 [英] Check Whether Directory Exists In Remote Server For Perl
问题描述
我想检查给定的路径是否存在,或者是否是 Perl 中另一个站点服务器中的目录.我的代码如下.
I wish to check whether a path given is exists or is a directory in another site server in Perl. My code is as below.
my $destination_path = "<path>";
my $ssh = "usr/bin/ssh";
my $user_id = getpwuid( $< );
my $site = "<site_name>";
my $host = "rsync.$site.com";
if ($ssh $user_id\@$host [-d $destination_path]){
print "Is a directory.\n";
}
else{
print "Is not a directory.\n";
}
我确定我的代码是错误的,因为我根据从另一个问题中看到的 bash 示例修改了代码,但我不知道如何修复它.感谢所有在这里提供帮助的人.
I am sure my code is wrong as I modify the code according to bash example I see from another question but I have no clue how to fix it. Thanks for everyone that helps here.
推荐答案
[
是命令的名称,在命令行中必须与其他单词分开.所以只需使用更多空格:
[
is the name of a command, and it must be separated from other words on the command line. So just use more spaces:
$ssh $user\@$host [ -d $destination_path ]
要实际执行此命令,您需要使用内置的system
功能.system
当它执行的命令成功时返回 0(参见链接中的文档)
To actually execute this command, you'll want to use the builtin system
function. system
returns 0 when the command it executes is successful (see the docs at the link)
if (0 == system("$ssh $user\@$host [ -d $destination_path ]")) {
print "Is a directory.\n";
} else {
print "Is not a directory.\n";
}
这篇关于Perl 检查远程服务器中是否存在目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!