ComputerCraft: Using shell.run() command in a program

cmd picture cmd · Mar 16, 2014 · Viewed 10.3k times · Source

I am trying to use the (file , parameters) functionality of the shell.run() command by assigning each word in a string to a new variable. The only way I know to do this is with tArgs[#]. Which if at the command line and typing a name of a program and its arguments that uses tArgs to do so will work fine. But I want to do this from a prompt within a program. So the scenario is:

1.The computer starts a program using the startup file.

2.Using the "write()" command the program asks for a command to run with parameters.

3.The user types for ex. "echo yes"

4.The program then takes the word "echo" and assigns it to "var1" and then the
 word: "yes" and assigns it to "var2"

5.The program takes "var1" and "var2" and inputs it to the "shell.run()" command in the
 format: shell.run((var1),(var2))

6.The "shell.run()" calls a program named "echo" which is set up to allow
  for parameters to be entered without a prompt by using the "tArgs = {...}" command
  and the "echo" program sees that it is getting an argument "yes" and runs the
  command: "print(tArgs[1])"

I have been working furiously trying to figure this out but cannot get it to work. Here is some code I have put together.

------------------------------------------------------------

-- 1.At the CraftOS the startup file runs a program "cmd"

[[Program: startup]]

shell.run("cmd")

[[end of startup]]

--2.Runs program "cmd"

[[Program: cmd]]

-- Now in cmd

term.clear()
term.setCursorPos(1,1)

function prompt()

write(">") --assuming user typed: echo yes
tArgs = {...}

file = tArgs[1] --will get the word "echo"
param1 = tArgs[2] --will get the word "yes"

if #tArgs < 1 then

    print(Syntax: <command> <parameters>)
    prompt()
else
    print()
    shell.run((file), (param1)) --resulting command is shell.run("echo","yes")
    print()
    prompt()
end
end

prompt()

[[end of cmd]]

--3.Runs the program "echo" because it was the first word in our command.

[[Program: echo]]

tArgs = {...}
param = tArgs[1]

print(param) --prints "yes" which is what the "shell.run((file), (param1)) is referring to.

[[end of echo]]

And the result of this should be like so in program "cmd" as this is where the custom shell is.

>echo yes

yes

>

--4.Then because functions return after completion it should loop back into function prompt().

Any help is greatly appreciated, if you have any advice please supply code that shows how you are using it in a program. Thank you!

Answer

Potassium Ion picture Potassium Ion · Jun 7, 2014

It appears you want to turn the user's input "foo bar" into shell.run("foo", "bar"). If so, you want to split the "foo bar" string into a table, then unpack that table:

function split(a)
    local result = {}
    for i in a:gmatch("%a+") do
        result[#result + 1] = i
    end
    return result
end
shell.run(unpack(split(read())))

Also the main issue in the previous answer is the use of the word break as a function name. You can't use that as a variable (or function) name becuase break is an actual statement that will end the current for or while loop in which it is called. Example:

for i = 1, 10 do
    if i = 5 then
        break
    end
end