我有一些简单的事情(通常)不起作用,这是Perl中变量的连接。我必须用Perl为Redhat的服务器创建一个脚本。
我有两份文件:
#get the variables
if ( $type =~ /remotePath/ ) {
$remotePath = $val;
}
if ( $type =~ /logType/ ) {
$logType = $val;
}
$logName = "tcf_.$logType";
#we would like to add the date at the logType and a prefix..
for ( my $i = 0; $i <= $#ARGV; $i++ ) {
if ( $ARGV[$i] =~ /(\d{4})(\d{2})(\d{2})/ ) {
$dateFormatLog = sprintf( "%s-%s-%s", $1, $2, $3 );
}
}
#I tried to print each variable separately it works well, but not the concatenation..
my $finalPath = "$remotePath/$logName.$dateFormatLog";
#or
my $finalPath = $remotePath . '/' . $logName . $dateFormatLog;
print $finalPath;
预期的/opt/applicaton/logs/backup/tcf_statistics.log.2014-10-13.zip结果是
但我有一件事
.2014-10-13 tics.logwas/logs/backup
或
2014-10-13istics.logwas/logs/backup
我不知道为什么连在一起的结果是这样的事情,如果有人有一个想法,这将是非常有用的!
提前谢了,
吉米
发布于 2014-10-13 15:43:52
首先,您的数据似乎来自一个源自Windows (或可能是Mac)系统的文件。这样的系统在每一行的末尾都有一个CR,或者是一个LF,它不会被Linux平台上的chomp
删除。您没有显示如何读取数据,但修复此类文件的最佳方法是在读取每一行时使用s/\s+\z//
而不是chomp
。
其次,您希望文件名看起来像tcf_statistics.log.2014-10-13.zip
,但是$logName
的值将类似于tcf_.statistics.log
,所以在tcf_
之后还有一个额外的点.
。您应该设置如下的值
my $logName = "tcf_$logType";
您还需要以某种方式合并文件扩展名.zip
的值;可能是这样的。
my $finalPath = "$remotePath/$logName.$dateFormatLog.zip"
这应该能让你的代码正常工作。
https://stackoverflow.com/questions/26343561
复制相似问题