tags:

views:

11

answers:

1

Hi,

How can i run a shell command in objective c with a variable E.G system("thisCMD thisParam %@", username);

A: 
[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:username]];

Or if username isn't already a string:

[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:[username description]]];

Or if you want to be able to change the format specifier for each argument to something more complicated later:

[NSTask launchedTaskWithLaunchPath:@"thisCMD" arguments:[NSArray arrayWithObject:[NSString stringWithFormat:@"%@", username]]];
David
lets say the command someCommand usernameShouldBeHere anotherparam how do i do it then?
Daniel
Change `thisCMD` to `someCommand` and construct the `NSArray` using the `arrayWithObjects` constructor.
David