1,105,263 Community Members

Objective C launching NSTask to run a shell script and get output

Member Avatar
bjohnst8
Newbie Poster
2 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 

What you posted isn't C.

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

Member Avatar
bjohnst8
Newbie Poster
2 posts since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article