2

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 ?

Alberto
  • 4,212
  • 5
  • 22
  • 36
  • 4
    Why are you trying to have the shell do this? One of the advantages of NSTask is not having to go through the shell. You can run the echo command (the stand-alone version, not bash's) directly. – Peter Hosey May 30 '12 at 16:57
  • 1
    @Peter Hosey Care to post how you can run the echo command through NSTask stand alone ? – Sentry.co Dec 15 '16 at 11:17

1 Answers1

3

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];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005