【问题标题】:using perl script to invoke cleartool commands using fmt使用 perl 脚本使用 fmt 调用 cleartool 命令
【发布时间】:2013-04-09 18:46:07
【问题描述】:

我使用 Perl 脚本在 ClearCase 上的多个 VOB 中为多个用户运行命令。我有一个从文本文件中读取的 VOB 列表。然后我在该列表上循环并执行我想要执行的任何 ClearCase 命令。但是,这一次脚本似乎不起作用。如果我将命令打印到屏幕上,然后在提示符处复制并粘贴它,它工作正常。它只是不会从 Perl 脚本中执行。我看到的唯一区别是 fmt 字符,但即使我删除它也不会执行。我尝试首先将 fmt 直接放在线上,然后尝试将它们设置为变量。你会看到第一个注释行是失败的,但我把它留在那里作为我尝试过的一个例子。最后两个 cmets 来自我这样运行的另一个脚本,它确实有效。

代码:

#! /usr/local/bin/perl -w
use strict;

open(VOBS,"vobs.txt") || die "Can't open: !$\n";

my $u = '%u';
my $a ='%Ad';
my $n ='%N/n';
my $user='john';
my $ct = '/usr/atria/bin/cleartool';

while(my $newvobs=<VOBS>){

  chomp($newvobs);
  my $tag = $newvobs;
  print "\n $tag \n"; 
  print " $ct lstype -kind brtype -invob $tag | grep $user ";
  `$ct lstype -kind brtype -invob $tag | grep $user`;
  # `/usr/atria/bin/cleartool lstype -kind brtype -invob $tag -fmt '%u %Ad %N/\n' `;
  # print "\n cleartool rmtag -view  $tag \n";
  #`/usr/atria/bin/cleartool rmtag -view  $tag `;
}

close(VOBS);

【问题讨论】:

  • cleartool 命令需要用户交互?还是平稳运行?
  • 它会顺利运行 我会在运行时实际生成一个 txt 文件的输出,我发送给用户让他们决定要删除哪些分支。
  • 在这种情况下,“|grep $user”部分应该被删除,它对命令结果没有贡献
  • @Carole 您可以添加任何错误消息吗?
  • 是的,我有打印,所以我可以看到我所在的vob,然后我可以看到它应该执行的命令,然后它应该执行。但是,从屏幕上我只得到两个这样的打印语句:

标签: perl clearcase


【解决方案1】:

实际上你的程序运行了,但没有打印任何东西。

例子:

#!/usr/bin/perl
use strict;
use warnings;
my $cmd = "cat";
`$cmd $0 | grep warning`;

输出:(无)



最容易修复。最后一行

print `$cmd $0 | grep warning`

输出:


use warnings;
print `$cmd $0 | grep warning`;

如果您需要退出代码,请将最后一行替换为

my $exit = system("$cmd $0 | grep warning");
print $exit;

输出:


use warnings;
my $exit = system("$cmd $0 | grep warning");
0

或者使用open处理输出:

open my $fh, "$cmd $0 | grep warning|" or die;
while (<$fh>) { print $_; }
close $fh;

输出:


use warnings;
open my $fh, "$cmd $0 | grep warning|" or die;

但我可以建议如下。使用 AUTOLOAD,clearcase 命令可以用作内部 perl 命令。

#!/usr/bin/perl
use strict;
use warnings;

sub AUTOLOAD {
    (my $sub = $::AUTOLOAD) =~ s/.*:://;
    print "---\n";
    system("time $sub @_");
    print "---\n";
}

my $cmd = "cat";
eval "$cmd($0)";

输出:


---
#!/usr/bin/perl

use strict;
use warnings;

sub AUTOLOAD {
    (my $sub = $::AUTOLOAD) =~ s/.*:://;
    print "---\n";
    system("time $sub @_");
    print "---\n";
}

cat($0);
0.00user 0.00system 0:00.00elapsed 400%CPU (0avgtext+0avgdata 2112maxresident)k
0inputs+0outputs (0major+174minor)pagefaults 0swaps
---

【讨论】:

  • 感谢您在 my exit 中使用的示例帮助它运行。我仍然对使用 fmt 的 grep 有一些问题,因为我认为它将它视为一个长字符串。但是,我能够让它以我可以使用的格式运行。谢谢
【解决方案2】:

作为参考,这里有一个使用-fmt、“Finding the latest baseline for a component”的perl脚本:

示例:

ccperl StreamComp.pl mystream@\pvobtag | findstr {component}

脚本“streamcomp.pl”:

#!/usr/bin/perl -w
my $cmdout = `cleartool desc -fmt '%[latest_bls]CXp' stream:$ARGV[0]`;
my @baselines = split(/,/,$cmdout);
foreach $baseline (@baselines)
{
  $compname=`cleartool desc -fmt '%[component]p' $baseline`;
  printf("%-30s \t %s\n", $compname, $baseline);
}

这是一个适用于 ClearCase UCM 环境的程序,但它可以让您了解可以尝试在您自己的基本 ClearCase 程序中重现的工作语句类型(先尝试不使用 grep)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-20
    • 2017-01-13
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多