【问题标题】:How can you execute a command outside of your current working directory in WinCvs?如何在 WinCvs 中执行当前工作目录之外的命令?
【发布时间】:2015-06-11 15:52:43
【问题描述】:

我正在开发一个 Perl 包装器以在 WinCvs 中执行命令。如果不对您希望在其上执行命令的目录执行 chdir,我一直无法找到执行 Cvs 命令的方法。

这很烦人,因为这意味着每次我想在 perl 脚本中对 Cvs 做任何事情时,我都需要获取当前工作目录,将目录更改为 Cvs 路径,执行我的命令,然后将目录更改回原来的工作目录。

有没有办法将路径传递给 Cvs 命令,以便您可以对当前不在的目录发出命令?

例如,如果我的 Perl 脚本中的当前工作目录是 C:\test\,并且我想在 C:\some_other_directory 上执行 Cvs update 命令,如何在不执行 chdirC:\some_other_directory 的情况下执行该命令第一个?

我当前如何执行命令的示例:

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

my $cwd = cwd();
chdir "C:\some_other_directory" or die $!;
system 'cvs update';
chdir $cwd or die $!;

我想要的是找到一种能够将“C:\some_other_directory”直接传递给Cvs命令并摆脱所有这些chdir废话的方法......

【问题讨论】:

  • 你可以试试 system 'cvs C:\some_other_directory\update';
  • 我试过了,还是不行。它会给你一个“未知命令”错误。
  • 不知道我是怎么得到-1的,这个问题和我的目标很明确......但是哦,好吧。

标签: perl command cvs chdir wincvs


【解决方案1】:

Flavio 的回答有效,但我也找到了这个替代解决方案。它允许您更改目录以发出命令,但比使用 chdir 危险性小,并且不太可能让您对脚本在任何给定时间可能位于哪个目录感到困惑。

模块File::Chdir可以很轻松的解决这个问题:

use File::Chdir;

# Set the current working directory
$CWD = '/foo/bar/baz';

# Locally scope this section
{
    local $CWD = '/some/other/directory';

    # Updates /some/other/directory
    system 'cvs update';
}

# current working directory is still /foo/bar/baz

【讨论】:

    【解决方案2】:

    另一种方法是在单个系统调用中调用多个命令:

    system("cd C:\some_other_directory && cvs update");
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2021-07-23
      • 1970-01-01
      • 2014-01-07
      • 2014-04-16
      • 1970-01-01
      相关资源
      最近更新 更多