Execute a system command: Difference between revisions
Content added Content deleted
No edit summary |
(Added Wren) |
||
Line 2,013: | Line 2,013: | ||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
<lang wart>system "ls"</lang> |
<lang wart>system "ls"</lang> |
||
=={{header|Wren}}== |
|||
Wren CLI doesn't currently expose a way to execute a system command. |
|||
However, if Wren is embedded in (say) a suitable Go program, then we can ask the latter to do it for us. |
|||
<lang ecmascript>/* run_system_command.wren */ |
|||
class Command { |
|||
foreign static exec(name, param) // the code for this is provided by Go |
|||
} |
|||
Command.exec("ls", "-lt") |
|||
System.print() |
|||
Command.exec("dir", "")</lang> |
|||
which we embed in the following Go program and run it. |
|||
{{libheader|WrenGo}} |
|||
<lang go>/* run_system_command.wren*/ |
|||
package main |
|||
import ( |
|||
wren "github.com/crazyinfin8/WrenGo" |
|||
"log" |
|||
"os" |
|||
"os/exec" |
|||
) |
|||
type any = interface{} |
|||
func execCommand(vm *wren.VM, parameters []any) (any, error) { |
|||
name := parameters[1].(string) |
|||
param := parameters[2].(string) |
|||
var cmd *exec.Cmd |
|||
if param != "" { |
|||
cmd = exec.Command(name, param) |
|||
} else { |
|||
cmd = exec.Command(name) |
|||
} |
|||
cmd.Stdout = os.Stdout |
|||
cmd.Stderr = os.Stderr |
|||
if err := cmd.Run(); err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
return nil, nil |
|||
} |
|||
func main() { |
|||
vm := wren.NewVM() |
|||
fileName := "run_system_command.wren" |
|||
methodMap := wren.MethodMap{"static exec(_,_)": execCommand} |
|||
classMap := wren.ClassMap{"Command": wren.NewClass(nil, nil, methodMap)} |
|||
module := wren.NewModule(classMap) |
|||
vm.SetModule(fileName, module) |
|||
vm.InterpretFile(fileName) |
|||
vm.Free() |
|||
}</lang> |
|||
=={{header|x86 Assembly}}== |
=={{header|x86 Assembly}}== |