Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用shell命令的Perl脚本不';行不通_Perl_Shell - Fatal编程技术网

调用shell命令的Perl脚本不';行不通

调用shell命令的Perl脚本不';行不通,perl,shell,Perl,Shell,我正在编写一个简单的Perl程序来测试用于更改目录的shell脚本。但它不起作用 这是我的代码: $result = `cd/`; print $result; 当我使用时,它工作得很好 $result =`dir`; cd(默认情况下)不输出任何内容,因此您将为$result变量分配一个空字符串 如果要输出更改到的目录的(完整)路径,只需将&&pwd附加到backticks中: $result = `cd / && pwd`; 请注意,创建了一个子进程,用于使用指定

我正在编写一个简单的Perl程序来测试用于更改目录的shell脚本。但它不起作用

这是我的代码:

$result = `cd/`;
print $result;  
当我使用时,它工作得很好

$result =`dir`;
cd
(默认情况下)不输出任何内容,因此您将为
$result
变量分配一个空字符串

如果要输出更改到的目录的(完整)路径,只需将
&&pwd
附加到backticks中:

 $result = `cd / && pwd`;
请注意,创建了一个子进程,用于使用指定的命令运行shell,因此您在那里执行的任何环境更改(包括更改目录)都不会影响Perl脚本本身

换句话说:您不能用shell命令更改Perl脚本的当前目录


如果您的意图是:

  • 只需测试您在
    `…`
    中包含的shell命令是否成功
    ,请使用;e、 g:

        system('cd /') == 0 || die "Command failed";
    
  • 捕获shell命令的输出,请假定它是一个目录路径并

        $result = `cd / && pwd` || die "Command failed.";
        chomp $result; # trim trailing newline
    
        # Change Perl script's working dir.
        chdir $result || die "Could not change to $result.";
    

如果需要更改脚本中的cwd目录,那么应该使用Perl内置的
chdir
函数。

要影响perl进程的当前工作目录,请使用函数。

您需要比“不工作”更具体。你有错误吗?您能告诉我们预期的和实际的输出吗?`cd/`not`cd/` Perl的问题是,有时如果出现错误,它不会打印出来。而是等待下一个命令。所以我不确定。但我原以为我的程序会更改目录,但事实并非如此。即使是'cd/`也无济于事。