• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTask and piped commands
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTask and piped commands


  • Subject: Re: NSTask and piped commands
  • From: Alastair Houghton <email@hidden>
  • Date: Tue, 18 May 2010 21:27:12 +0100

On 18 May 2010, at 21:13, Jens Alfke wrote:

>
> On May 18, 2010, at 12:33 PM, appledev wrote:
>
>> I dont want to use a call to a bash script, because of sneaking in bad commands.
>
> As others said, it’s not a problem here because the command line is entirely hardcoded.
>
> If you wanted to avoid using a shell, you’d have to start three separate NSTasks for the three commands (df, grep, awk) and hook the output pipe of one to the input of the next. I’m not sure how to do that.

Something like the following should work, right?

  NSTask *task1 = [[[NSTask alloc] init] autorelease];
  NSTask *task2 = [[[NSTask alloc] init] autorelease];
  NSTask *task3 = [[[NSTask alloc] init] autorelease];
  NSPipe *t1t2pipe = [NSPipe pipe];
  NSPipe *t2t3pipe = [NSPipe pipe];
  NSPipe *outputPipe = [NSPipe pipe];

  [task1 setLaunchPath:@"/bin/df"];
  [task1 setArguments:[NSArray arrayWithObjects:@"-k", nil]];
  [task1 setStandardOutput:t1t2pipe];

  [task2 setLaunchPath:@"/usr/bin/grep"];
  [task2 setArguments:[NSArray arrayWithObjects:@"/dev/", nil]];
  [task2 setStandardInput:t1t2pipe];
  [task2 setStandardOutput:t2t3pipe];

  [task3 setLaunchPath:@"/usr/bin/awk"];
  [task3 setArguments:[NSArray arrayWithObjects:@"{print $1 \"\t\" $4 \"\t\" $5 \"\t\" $6;}", nil]];
  [task3 setStandardInput:t2t3pipe];
  [task3 setStandardOutput:outputPipe];

  [task1 launch];
  [task2 launch];
  [task3 launch];

Kind regards,

Alastair.

--
http://alastairs-place.net



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSTask and piped commands
      • From: Jens Alfke <email@hidden>
References: 
 >NSTask and piped commands (From: appledev <email@hidden>)
 >Re: NSTask and piped commands (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: NSTask and piped commands
  • Next by Date: Re: NSTask and piped commands
  • Previous by thread: Re: NSTask and piped commands
  • Next by thread: Re: NSTask and piped commands
  • Index(es):
    • Date
    • Thread