2

我对包含 NSTask 的方法有一个非常奇怪的问题。在 10.7+ 中,该功能运行良好并成功执行,返回结果并将文件复制到所需目录。

在 10.6.8 中尝试我的应用程序时,NSTask 似乎什么也没做,实际上没有错误,或者任何提示我为什么它不工作的东西。我已经尝试了所有可能的角度来确定问题出在哪里,并且我已经排除了可能性。:-/

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/sh"];

NSArray *arguments = [NSArray arrayWithObjects: @"-c",
                     @"find /Data/*.jug/files/ -name thefile | head -n 1 | awk -v dir=\"$HOME/path/to/copy/to\" '{printf \"cp \\\"%s\\\" \\\"%s\\\"\\n\", $1, dir }' | sh", nil];

[task setArguments: arguments];

NSPipe *thePipe = [NSPipe pipe];
[task setStandardInput:[NSPipe pipe]];
[task setStandardOutput:thePipe];

[task launch];
[task waitUntilExit];
[task release];

如果在 10.6.8 中通过终端尝试该命令,我会得到我希望在我的应用程序中得到的结果。我在这里完全不知所措,真的希望有人能对这个问题以及如何解决它有所了解。

4

2 回答 2

0

The /Data/*.jug: Not a directory error message typically occurs when there are no files to be matched by the shell's glob expansion and the literal /Data/*.jug is passed to a command that requires an existing file or directory as an argument.

Using the Bash shell you can avoid this kind of error by using shopt -s nullglob.

In addition, you should make sure that glob expansion is enabled (set +f in Bash).

Therefore explicitly use the Bash shell with [task setLaunchPath: @"/bin/bash"]; and start testing your shell code with:

@"set +f; shopt -s nullglob; ls -ld /; find /Data/*.jug/files/ -name thefile"

Then add & test each further command of your given shell code step by step.

You may also use complete executable paths to be absolutely sure, e. g. /usr/bin/find.

In your awk command replace $1 with $0 in case there is a file name containing a space.

A possible source of confusion for the compiler may be the C-style comment start /* in /Data/*.jug/files/.

To get more output information use shell commands in verbose mode if possible, e. g. cp -v and bash -xv -c.

Your code worked for me on my machine with Mac OS X 10.6.8 using asynctask.m from NSPipe - CocoaDev and /usr/bin/gcc-4.2 as compiler.

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];

NSArray *arguments = [NSArray arrayWithObjects: 
   @"-xv",
   @"-c",
   @"set +f; shopt -s nullglob; ls -ld /noSuchFile; find /Data/*.jug/files/ -name thefile | head -n 1 | awk -v dir=\"$HOME/path/to/copy/to\" '{printf \"cp -v \\\"%s\\\" \\\"%s\\\"\\n\", $1, dir }' | bash -xv", nil];
于 2013-01-18T14:07:39.920 回答
0

尝试使用以下引号:

'/Data/*.jug/files/' 

或者

 \"/Data/*.jug/files/\"
于 2013-01-02T03:52:29.950 回答