【问题标题】:unable to pass a parameter to dir command from perl script无法从 perl 脚本将参数传递给 dir 命令
【发布时间】:2017-06-18 07:56:56
【问题描述】:

我无法执行dir 命令,在指定要搜索的文件名all.xml 时出现错误,我无法使用/s 命令进行递归搜索,我收到以下错误@987654324 @ 并且不理解/s 参数是用于递归搜索,程序将其解释为文件路径

use strict;
use warnings;  
print "i";
my $vall = ` dir  //server1/dxx/mxx/rxx/ "ui.xml"  /s`; 

print $vall;

【问题讨论】:

  • 这是在 Windows 上吗?您可能需要使用反斜杠。
  • @simbabque 是的 windows,我不能写反斜杠,cmd 中的 cygwin 工具在我执行命令时要求我写 posix 等效项,我可以列出 @987654327 中的所有文件@ 使用正斜杠的文件夹,但我无法递归查找文件,因为 /s 参数被解释为文件路径
  • 把开关放在路径前面。
  • @simbabque 也试过了,如果这样做 dir //server1/dxx/mxx/rss/ 也不起作用,它会显示“rss”中存在的所有文件,但是当我写 file name /s 时,我得到错误no such file or directory 它还包括路径中的文件名ui.xml
  • 改用-s。听起来很奇怪,但我记得,类 Unix 路径也意味着类 Unix 选项。

标签: perl cmd directory


【解决方案1】:

这与 Perl 无关。您传递给 dir 的参数不正确。

  • dir/ 解释为选项的开头,除非它被引用,
  • 路径中间有一个不应该存在的空格,并且
  • /s 不适用于同时包含 / 和文件组件的路径。

此外,除了/s 之外,您可能还需要/b

固定:

dir "\\server1\dxx\mxx\rxx\ui.xml" /s/b

所以

my $dir_output = `dir "\\\\server1\\dxx\\mxx\\rxx\\ui.xml" /s/b`;

例子,

>dir "//localhost/C$/Users/ikegami/Desktop/" /s/b
\\localhost\C$\Users\ikegami\Desktop\a.jpg
\\localhost\C$\Users\ikegami\Desktop\cabinet.txt
...

>dir "//localhost/C$/Users/ikegami/Desktop/a.jpg" /s/b
File Not Found                                               <-- WTF?

>dir "\\localhost\C$\Users\ikegami\Desktop\a.jpg" /s/b
\\localhost\C$\Users\ikegami\Desktop\a.jpg
\\localhost\C$\Users\ikegami\Desktop\x\a.jpg

>dir \\localhost\C$\Users\ikegami\Desktop\a.jpg /s/b
\\localhost\C$\Users\ikegami\Desktop\a.jpg
\\localhost\C$\Users\ikegami\Desktop\x\a.jpg

>type a.pl
print `dir \\\\localhost\\C\$\\Users\\ikegami\\Desktop\\a.jpg /s/b`

>perl a.pl
\\localhost\C$\Users\ikegami\Desktop\a.jpg
\\localhost\C$\Users\ikegami\Desktop\x\a.jpg

也就是说,我个人会使用File::Find::Rule

use File::Find::Rule qw( );

my $qfns =
   File::Find::Rule
   ->name('ui.xml')
   ->file
   ->in('//server1/dxx/mxx/rxx');

【讨论】:

  • no /s 仍被解释为路径 /s:no such file or directory ,而 ui.xml 是我尝试在 rss 及其子目录中搜索的文件
  • 我试过这个命令,这是我得到的错误cygwin warning: MS-DOS style path detected: \server1\dxx\mxx\rxx\ui.xml l Preferred POSIX equivalent is: /cygdrive/d/server1/dxx/mxx/rxx/ui.xml CYGWIN environment variable option "nodosfilewarning" turns off this warning. Consult the user's guide for more details about POSIX paths: http://cygwin.com/cygwin-ug-net/using.html#using-pathnames dir: cannot access \\\\server1\\dxx\\mxx\\rxx\\ui.xml: N o such file or directory dir: cannot access /s: No such file or directory dir: cannot access /b: No such file or directory
  • 但是当我尝试这个`dir "//server1/dxx/mxx/rxx/" /s 我得到rxxcannot access /s :no such directory下的所有目录,`/s'开关仍然被解释为路径
  • 您在 Unix 仿真环境中,将cmd 命令传递给sh(其中dir 似乎是ls 的别名(这意味着您的shell 配置不当))。如果要执行cmd 命令,则需要执行cmd
  • 我在命令行写了cmd 然后执行程序还是不行
猜你喜欢
  • 1970-01-01
  • 2014-03-08
  • 2013-10-22
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
相关资源
最近更新 更多