2

I have a very odd issue with a method that contains an NSTask. In 10.7+ the functionality works absolutely fine and successfully executes, returning the result and copying the file to the desired directory.

When trying my application in 10.6.8 the NSTask seems to do nothing at all, in-fact no errors, or anything to give me a hint as to why it's not working. I've tried every possible angle to determine where the problem is and I'm tapped out of possibilites. :-/

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];

If try the command via Terminal in 10.6.8 I get the results back that I would expect to get in my application. I'm at a total loss here and really hope someone can shed some light onto this issue and how to fix it.

Jack Handy
  • 21
  • 1
  • 1
    I see that you are using setSTandardInput and setStandardOutput. Have you tried setStandardError to see if anything appears? – ericg Jan 02 '13 at 01:47
  • @ericgorr, hi - i've tried `setStandardError` and didn't get anything different, however, i looked back in my console logs and noticed this showing up: `/Data/*.jug: Not a directory` - (which is a directory, although 10.6.8 doesn't see it that way probably from the extension on the end?). – Jack Handy Jan 02 '13 at 03:11
  • If you do an ls -l on /Data, what does the entry look like for the directory? – ericg Jan 07 '13 at 00:23

2 Answers2

0

Try it with quotes like:

'/Data/*.jug/files/' 

or

 \"/Data/*.jug/files/\"
Alan
  • 1
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];
marco
  • 1