This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sabrecho's profile picture

Slight confusion over os.run

Started by Sabrecho, 05 April 2014 - 10:45 PM
Sabrecho #1
Posted 06 April 2014 - 12:45 AM
Hey guys, I had the brilliant idea to "edit startup" so it hosts gps requests immediately. I've tried various things and have only learned how it's not done.

parameters = {"host", "-302", "61", "234"}
os.run({}, "/rom/programs/gps", parameters)

This is the closest I've gotten, but it just outputs the Usage description as if I'd only typed "gps" at the command prompt.

Any thoughts what I'm not doing right here?
awsmazinggenius #2
Posted 06 April 2014 - 04:40 AM
You should use shell.run() if you don't understand os.run(), so do this:

shell.run("gps host -302 61 234")
Dog #3
Posted 06 April 2014 - 09:10 AM
awsmazinggenius' solution is probably the most direct. On the off chance that you want to make use of the 'parameters' variable you could try something like this…

local parameters = {"-302", "61", "234"}
local sRun = "gps host " .. parameters[1] .. " " .. parameters[2] .. " " .. parameters[3]
shell.run(sRun)
Edited on 06 April 2014 - 07:17 AM
Sabrecho #4
Posted 06 April 2014 - 09:13 AM
That's got it, thanks genius. Still not sure what os.run does, but that's been demoted to research-'cause-I'm-bored. :)/>

And thank you Dog for giving me a different approach to consider.
Bomb Bloke #5
Posted 06 April 2014 - 09:17 AM
On the off chance that you want to make use of the 'parameters' variable you could try something like this…

An easier way involves use of "unpack", which does pretty much the same thing but is rather more dynamic:

local parameters = {"-302", "61", "234"}
shell.run( "gps host", unpack(parameters) )

The end result is akin to writing:

shell.run( "gps host", "-302", "61", "234" )
theoriginalbit #6
Posted 06 April 2014 - 09:23 AM
Still not sure what os.run does, but that's been demoted to research-'cause-I'm-bored. :)/>
os.run is a lower level version of shell.run, well actually shell.run uses os.run.
when you use shell.run the program runs just like if you were to run it from the terminal, this means it has access to the Shell API as well as all other APIs in the global table.
however when os.run is used the program only gets access to the global table and anything else you provide in the first 'environment variables' table.

basically here is the simplest implementation of shell.run, you'll notice that it uses os.run and gives the programs the Shell API via the first argument.

function shell.run( programPath, ... )
  return os.run( {['shell'] = shell}, programPath, ... )
end
obviously there's a lot more code in the real implementation, but this was a 'simplest form' example
Edited on 06 April 2014 - 07:23 AM
Sabrecho #7
Posted 08 April 2014 - 12:07 AM
Thank you very much theoriginalbit for your clarification of the different .run commands. I appreciate it.