Execute a system command: Difference between revisions

m
(Omitted EasyLang)
m (→‎{{header|Wren}}: Minor tidy)
(7 intermediate revisions by 3 users not shown)
Line 690:
 
=={{header|FutureBasic}}==
FB 7.0.23+
This simple example prints the output to a console window. With its open "Unix" command, FB has robust capability as a system interface to the Free BSD Unix core of Macintosh OS X 10.x.
<syntaxhighlight lang="futurebasic">
print unix(@"ls -A")
include "ConsoleWindow"
</syntaxhighlight>
 
Classic FB using Pascal strings
<syntaxhighlight>
local fn DoUnixCommand( cmd as str255 )
dim as str255 s
 
open "Unix", 2, cmd
Line 735 ⟶ 737:
usr
var
</pre>
 
Modern FB using CFStrings
<syntaxhighlight>
 
include "NSLog.incl"
 
// For remote uses like curl
// #plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
local fn RunTerminalCommand( cmd as CFStringRef ) as CFStringRef
————————————————————————————————————————————————————————————————————————————————————————————————————
ErrorRef err = NULL
CFStringRef outputStr = NULL
TaskRef task = fn TaskInit
TaskSetExecutableURL( task, fn URLFileURLWithPath( @"/bin/zsh" ) )
CFStringRef cmdStr = fn StringWithFormat( @"%@", cmd )
CFArrayRef args = fn ArrayWithObjects( @"-c", cmdStr, NULL )
TaskSetArguments( task, args )
PipeRef p = fn PipeInit
TaskSetStandardOutput( task, p )
TaskSetStandardError( task, p )
FileHandleRef fh = fn PipeFileHandleForReading( p )
fn TaskLaunch( task, NULL )
TaskWaitUntilExit( task )
CFDataRef dta = fn FileHandleReadDataToEndOfFile( fh, @err )
if err then NSLog( @"Error reading file: %@", fn ErrorLocalizedDescription( err ) ) : exit fn
fn FileHandleClose( fh, @err )
if err then NSLog( @"Error closing file: %@", fn ErrorLocalizedDescription( err ) ) : exit fn
outputStr = fn StringWithData( dta, NSUTF8StringEncoding )
end fn = outputStr
 
CFStringRef cmd
 
cmd = @"cal 2023"
NSLog( @"%@", fn RunTerminalCommand( cmd ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
2023
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25
29 30 31 26 27 28 26 27 28 29 30 31
 
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17
16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24
23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30
30
 
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9
9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16
16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23
23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30
30 31
 
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9
15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16
22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23
29 30 31 26 27 28 29 30 24 25 26 27 28 29 30
31
</pre>
 
Line 1,592 ⟶ 1,678:
write(stdout->read() + "\n");
}</syntaxhighlight>
 
=={{header|Plain English}}==
<syntaxhighlight lang="text">
A command is a string.
A parameter is a string.
 
To run:
Start up.
Execute "dir" on the command line.
Shut down.
 
To execute a command on the command line:
Put "/c " then the command into a parameter.
Null terminate the parameter.
Put "cmd" into a string.
Null terminate the string.
Call "shell32.dll" "ShellExecuteA" with nil and nil and the string's first and the parameter's first and nil and 1.
</syntaxhighlight>
 
=={{header|Pop11}}==
Line 2,134 ⟶ 2,238:
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us.
 
<syntaxhighlight lang="ecmascriptwren">/* run_system_commandExecute_a_system_command.wren */
class Command {
foreign static exec(name, param) // the code for this is provided by Go
Line 2,145 ⟶ 2,249:
which we embed in the following Go program and run it.
{{libheader|WrenGo}}
<syntaxhighlight lang="go">/* run_system_commandExecute_a_system_command.go*/
package main
 
Line 2,176 ⟶ 2,280:
func main() {
vm := wren.NewVM()
fileName := "run_system_commandExecute_a_system_command.wren"
methodMap := wren.MethodMap{"static exec(_,_)": execCommand}
classMap := wren.ClassMap{"Command": wren.NewClass(nil, nil, methodMap)}
9,476

edits