Replies: 5 comments
-
|
Hello, https://iolanguage.org/reference/index.html#Core.System Looks like: results := System runCommand("ls") // for example Should work. |
Beta Was this translation helpful? Give feedback.
-
|
Btw, it does seem confusing to have two methods that basically do the same thing. I think this was done to mirror C or maybe other languages, which isn't a good reason. We should clean this up at some point. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, it works perfectly and the code is minimal. I hadn't noticed the "Returns object with exitStatus, stdout and stderr slots" in the reference. |
Beta Was this translation helpful? Give feedback.
-
|
One thing to note is that the "runCommand" method doesn't have any arguments listed in the reference manual, which could lead to confusion. Another point is that it creates two temporary files in the script's current directory. Is there any way to prevent this behavior? Or at least return the exact name of these so I can delete them manually. |
Beta Was this translation helpful? Give feedback.
-
|
That's a good point. It was missing in the docs. Here's the method and updated docs. It looks like it should use a the TMPDIR environment variable to find your system's /tmp directory. Not sure how that works on all OSes. //doc System runCommand(cmd, successStatus) Calls system and redirects stdout/err to tmp files. Returns object with exitStatus, stdout and stderr slots.
runCommand := method(cmd, successStatus,
successStatus := if(successStatus, successStatus, 0)
tmpDirPath := System getEnvironmentVariable("TMPDIR")
outPath := method(suffix,
Path with(tmpDirPath, list(System thisProcessPid, Date clone now asNumber, suffix) join("-"))
)
stdoutPath := outPath("stdout")
stderrPath := outPath("stderr")
exitStatus := System system(cmd .. " > " .. stdoutPath .. " 2> " .. stderrPath)
result := Object clone
result successStatus := successStatus
result exitStatus := exitStatus
result failed := method(exitStatus != successStatus)
result succeeded := method(exitStatus == successStatus)
stdoutFile := File with(stdoutPath)
stderrFile := File with(stderrPath)
result stdout := if(stdoutFile exists, stdoutFile contents, nil)
result stderr := if(stderrFile exists, stderrFile contents, nil)
result
) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, to execute a command I use the "system" method (of "System") which returns a number (exit status) but, is there a way to capture/extract the output as a string or sequence? I need to manipulate the output.
Beta Was this translation helpful? Give feedback.
All reactions