Re: NSTask and piped commands
Re: NSTask and piped commands
- Subject: Re: NSTask and piped commands
- From: Greg Guerin <email@hidden>
- Date: Tue, 18 May 2010 18:37:35 -0700
appledev wrote:
arguments = [NSArray arrayWithObjects: @"-c", @"/bin/df -k| /
usr/bin/grep /dev/ | /usr/bin/awk '{print $1 $4 $5 $6;}'",nil];
Your awk syntax is somewhere between quirky and wrong. Since you
didn't mention what the problem was, I will assume the output you
want is not the output you're getting.
I will also assume that you ARE getting some output, despite Alastair
Houghton previously noted comment that waiting for termination before
reading stdout can be unsafe. Unless you have a whole lot of mounted
disks, the pipe buffer won't fill up and cause deadlock (it's about
16 KB, empirically determined, in all Mac OS X versions I've tested,
since 10.0).
Here are the awk problems...
String concatenation in awk is indicated by whitespace. So this:
print $1 $4 $5 $6
concatenates the strings together, then prints them as a single
value, followed by the output record separator (newline by default).
If you want the default output field separator, you need this awk line:
print $1, $4, $5, $6
The default output field separator is defined by the awk variable
named OFS. To use tab as OFS:
{ OFS="\t"; print $1, $4, $5, $6;}
You can discover all this simply by reading awk's man page.
The resulting bash command-line is:
/bin/df -k| /usr/bin/grep /dev/ | \
/usr/bin/awk '{ OFS="\t"; print $1, $4, $5, $6;}'
I have inserted a \ to force a continuation line, so mail doesn't
line-fold badly.
To encode that properly as an Objective-C string literal, you need to
escape both the double-quotes and the backslash:
arguments = [NSArray arrayWithObjects:
@"-c",
@"/bin/df -k| /usr/bin/grep /dev/ | /usr/bin/awk '{ OFS=\"\\t\";
print $1, $4, $5, $6;}'",
nil];
(The Objective-C was written in mail and not compiled. The command-
line with the modified awk code was tested in bash.)
And I should note that awk is perfectly capable of matching the "/
dev/" pattern by itself with no assistance from grep. This is left
as an exercise for the interested reader.
-- GG
_______________________________________________
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