我需要检查配置文件是否存在,所以我为测试目的写了这个
#!/usr/bin/perl
use strict;
use warnings;
my $prfle=`~/sqllib/db2profile`;
print $prfle;
但它没有打印......
脚本检查配置文件,如果没有找到,它将询问用户,直到提供了有效路径并执行该配置文件,我在shell脚本中成功实现了这一点,但在perl中遇到了麻烦
答案 0 :(得分:4)
在Perl中,反引号执行shell命令。例如,这将打印hi:
`echo hi`;
要检查文件是否存在,请使用-e
:
$prfle= '~/sqllib/db2profile';
if (-e $prfle) {
print "File Exists!\n";
}
请注意单引号'
,而不是在字符串文字周围反引号`
。
答案 1 :(得分:2)
根据您的评论,我怀疑您想要这样的内容:
my $profile = ''; # default profile
while (not -e $profile) { # until we find an existing file
print "Enter a valid profile: ";
chomp($profile = <>); # read a new profile
}
qx($profile); # execute this file
执行该文件有多个选项。 qx()
与反引号相同,将返回标准输出。 system()
将返回系统为执行的命令提供的返回值。 exec()
将执行该命令并退出perl脚本,有效地忽略exec
之后的任何代码。根据您的需求,选择最适合您的选项。