+ -
当前位置:首页 → 问答吧 → Perl切换工作路径的问题

Perl切换工作路径的问题

时间:2010-11-04

来源:互联网

大家好,

我使用perl执行一些shell命令,
比如
system("cmd1 ");
这个命令会创建一个文件夹,比如temp,
接下来,我要进入这个文件夹执行一些命令,我尝试
system("cd temp");
但是好像不成功,系统提示:
Can't exec "cd": No such file or directory at git.pl line 15

我想问问,使用perl如何切换当前的工作目录?
另外执行命令会有一些输出
如何把这些输出收集到我的日志文件里呢。

print LOG_HANDLE "log start \n";
谢谢!

作者: hepower   发布时间: 2010-11-04

chdir

LOG输出也有相应模块,你也可以自己 print

作者: zhlong8   发布时间: 2010-11-04

hi,

chdir已经搞定。

我的意思是
我执行一个cmd比如:
system("cmd"),会有一些输出信息在屏幕上打印出来,我怎么把这些信息打印到log文件里,我尝试:
print DB_LOG_FILE system("git log | grep Date");

但是失败了。。
谢谢!

作者: hepower   发布时间: 2010-11-04

本帖最后由 zhlong8 于 2010-11-04 11:58 编辑

$str = `dir`

$str 里面即为 dir 的内容,注意是反引号


QUOTE:
qx/STRING/
`STRING`
A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.


见 perlop

作者: zhlong8   发布时间: 2010-11-04