Alberto
Alberto

Reputation: 4282

NSTask - execute echo command

I'm trying to run a simple task which has to execute an echo "Hello World"

Well here is my code:

NSTask *task;
    task = [[NSTask alloc] init];


    [task setLaunchPath:@"/bin/bash"]; 




    NSArray *arguments;

    arguments = [NSArray arrayWithObjects:@"echo","hello world" nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    [task setStandardError: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];



    [task launch];
    //...
    //Code to get task response

Keep giving me no such file or directory error.. What am I doing wrong ?

Upvotes: 2

Views: 2123

Answers (1)

kennytm
kennytm

Reputation: 523264

The right way to execute a command is

bash -c "echo 'hello world'"

which means the arguments you should pass are

arguments = [NSArray arrayWithObjects:@"-c", @"echo 'hello world'", nil];

Upvotes: 3

Related Questions