Execute a system command: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
(15 intermediate revisions by 6 users not shown)
Line 274:
=={{header|APL}}==
<syntaxhighlight lang="apl">
∇ system s;handle
h ← ⎕fio['fork_daemon'] '/bin/ls /var'
⍝⍝ NOTE: one MUST give the full absolute path to the program (eg. /bin/ls)
⍝⍝ Exercise: Can you improve this by parsing the value of
⍝⍝ ⎕ENV 'PATH' ?
⍝⍝
handle ← ⎕fio['fork_daemon'] s
⎕fio['fclose'] handle
system '/bin/ls /var'
backups games lib lock mail run tmp
cache gemini local log opt spool
⎕fio['fclose'] h
0
</syntaxhighlight>
 
Line 684 ⟶ 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 729 ⟶ 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,171 ⟶ 1,263:
local f = io.popen("ls") -- store the output in a "file"
print( f:read("*a") ) -- print out the "file"'s content</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
Locale 1033 // for the chr$(string) : converτ ANSI to UTF16LE
Dos "chdir "+quote$(dir$)+"&& dir /w > out.txt";
Wait 100
Print "Press Space or Mouse to see next page"
A$=chr$(eval$(buffer("out.txt")))
Report a$ // view text using proportional typing, and at pages, with 3/4height scroll
</syntaxhighlight>
 
 
 
=={{header|M4}}==
Line 1,573 ⟶ 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,096 ⟶ 2,219:
</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
import os
 
Line 2,115 ⟶ 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,126 ⟶ 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,157 ⟶ 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)}
Line 2,208 ⟶ 2,331:
<syntaxhighlight lang="zxbasic">PAUSE 100</syntaxhighlight>
 
{{omit from|EasyLang}}
{{omit from|Retro}}
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have an external OS/command processor. -->
9,476

edits