Get system command output: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 674:
Close #1
End</lang>
 
 
=={{header|FutureBasic}}==
Simple system commands can be run in FB's open "UNIX" function. However, more common are functions such as this example which uses a notification observer to return watch as data is downloaded in the background. The command in this example returns the mdls manpage.
<lang futurebasic>
include "NSLog.incl"
 
local fn ObserverOne( ref as NotificationRef )
FileHandleRef fh = fn NotificationObject( ref )
CFDataRef dta = fn FileHandleAvailableData( fh )
 
if ( fn DataLength( dta ) > 0 )
CFStringRef string = fn StringWithData( dta, NSUTF8StringEncoding )
NSLog( @"%@", string )
FileHandleWaitForDataInBackgroundAndNotify( fh )
else
NotificationCenterRemoveObserver( @fn ObserverOne, NSFileHandleDataAvailableNotification )
end if
end fn
 
local fn RunCommand( cmdStr as CFStringRef )
'~'1
TaskRef task = fn TaskInit
TaskSetExecutableURL( task, fn URLFileURLWithPath( @"/bin/sh" ) )
CFArrayRef arguments = fn ArrayWithObjects( @"-c", cmdStr, NULL )
TaskSetArguments( task, arguments )
PipeRef p = fn PipeInit
TaskSetStandardOutput( task, p )
FileHandleRef fh = fn PipeFileHandleForReading( p )
NotificationCenterAddObserver( @fn ObserverOne, NSFileHandleDataAvailableNotification, (FileHandleRef)fh )
fn TaskLaunch( task, NULL )
FileHandleWaitForDataInBackgroundAndNotify( fh )
end fn
 
fn RunCommand( @"man mdls | col -b" )
 
HandleEvents
</lang>
{{output}}
<pre>
 
MDLS(1) BSD General Commands Manual MDLS(1)
 
NAME
mdls -- lists the metadata attributes for the specified file
 
SYNOPSIS
mdls [-name attributeName] [-raw [-nullMarker markerString]] file ...
 
DESCRIPTION
The mdls command prints the values of all the metadata attributes associ-
ated with the files provided as an argument.
 
The following options are available:
 
-name Print only the matching metadata attribute value. Can be
used multiple times.
 
-raw Print raw attribute data in the order that was requested.
Fields will be separated with a ASCII NUL character, suit-
able for piping to xargs(1) -0.
 
-nullMarker Sets a marker string to be used when a requested attribute
is null. Only used in -raw mode. Default is "(null)".
 
SEE ALSO
mdfind(1), mdutil(1) xargs(1)
 
Mac OS X June 3, 2004 Mac OS X
</pre>
 
 
 
 
 
 
 
=={{header|Gambas}}==
715

edits