Hey Guys,

I am having a problem getting NSTask to launch my shell script and output the results. I believe it is launching the script correctly, however I would like to acquire the output, error messages and perhaps even input... there are no errors about invalid launch-paths, so assume it is launching, I have implemented pipes and a dozen different ways to get this problem solved, but to no avail, so any input would be appreciated.

here are the basics of my model:

Firstly the script is essentially anything... I just want to get my scaffold working before I get too bogged down in scripting again... It is located in the resources folder called hello.sh

#!/bin/sh
echo "hello"
exit

Now for the fun part..

NSArray* Array = [[NSArray alloc] init];
	Array = [NSBundle pathsForResourcesOfType:@"sh" inDirectory:[[NSBundle mainBundle] resourcePath]];
	NSLog(@"%@", Array);
	
	NSString* scriptPath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"sh"];
	NSLog(@"%@", scriptPath);
	
	if(scriptPath){
		NSArray* theArguments = [NSArray arrayWithObjects: @"/bin/sh", scriptPath, theName, nil];
		NSTask* scriptTask = [[NSTask alloc] init];
		NSPipe *pipe;
		pipe = [NSPipe pipe];
		[scriptTask setStandardOutput: pipe];
		[scriptTask setStandardError: pipe];
		NSFileHandle *file;
		file = [pipe fileHandleForReading];
		[scriptTask setLaunchPath: [theArguments objectAtIndex:0]];
		[scriptTask setArguments: [theArguments subarrayWithRange: NSMakeRange (1, ([theArguments count] - 1))]];
		[scriptTask launch];
		NSData *data;
		data = [file readDataToEndOfFile];
		NSString *string;
		string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
		NSLog (@"got\n%@", string);

	} else {
		NSLog(@"That dosn't exist");
	}

again any help will be appreciated, I have been bashing my head against a wall for days...

cheers bjohnst8

Recommended Answers

All 2 Replies

What you posted isn't C.

Objective C is a very different animal, which not so many people are familiar with.

yes well, as there is no section for objective c... I figured c was the best place to put it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.