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

Using shell functions within an API

Started by savior67, 28 December 2012 - 08:50 AM
savior67 #1
Posted 28 December 2012 - 09:50 AM
Hello, I'm trying to run a program from a function in an api, I've tried just about every function in the shell api and it repeatedly gives me the "attempting to index nil" error. The api is located at /API/svr and the program I'm trying to call is at /client67/files/getCommand. Here is the code, any help is greatly appreciated.

function listener()
while true do
local ID,msg = rednet.receive()
if msg == "connect" then
–Everything runs smooth till this point.
shell.run("client67/files/getCommand")
elseif msg == "birds" then
getNick(ID)
end
end
end

function run(f)
parallel.waitForAny(f,listener)
end
OmegaVest #2
Posted 28 December 2012 - 10:26 AM
shell.run does not work in an api, as all apis are outside the shell runtime environment. You might be able to use os.run("shell", prog, args), but I've never really had much luck with that. Otherwise, copy the program into its own function, and use it that way.
pbcub1 #3
Posted 28 December 2012 - 11:11 AM
Ok, this took me awhile;

So I do not know if there is a way to get

prog = --Your program here
shell.run(prog)

However, what i do know will work because i just now tested it out is;

Test (File that is running the api)


--It is important that you put in all the directories from the default --one, for instance
--test/testapi
-- when your program is in "test/test"
os.loadAPI(test/testapi)
testapi.test()
os.unloadAPI("testapi")

testapi (API itself)


function test()
prog = test2 -- could be any you wish
env = getfenv() --This is a build in function to get the envirment in --lua
os.run(env, prog, --Arg if any)
end

test 2


print("Hello world")
savior67 #4
Posted 28 December 2012 - 11:11 AM
os.run did the trick omega, thanks for the tip.
Lyqyd #5
Posted 28 December 2012 - 11:15 AM
Of course, if you're running things in the same environment as the API has, you may run into issues with any programs that attempt to make use of the shell "API".