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

Does doing "shell.run("currentProgram") cause some problems?

Started by Backplague, 14 April 2012 - 02:02 PM
Backplague #1
Posted 14 April 2012 - 04:02 PM
As far as i have tested, it doesnt. And i havent tested alot :)/>/>
Dirkus7 #2
Posted 14 April 2012 - 04:07 PM
It just reloads your program, it will make an infinite loop but as far as i know, it doesn't do anything bad :)/>/>
Backplague #3
Posted 14 April 2012 - 04:17 PM
Thanks!
Cloudy #4
Posted 14 April 2012 - 05:58 PM
It is not advisable to start your own program again as after enough times you will eventually run out of memory. You would be much better refactoring your code so that it is in a while loop, then return or break out of it when you want to quit.

It just reloads your program, it will make an infinite loop but as far as i know, it doesn't do anything bad :)/>/>

An infinite loop isn't bad? Yes, it is.
Backplague #5
Posted 14 April 2012 - 06:31 PM
So the idea is, i have a controller that controls many of the machines in my base. I change that controller's code many times, so i have to reboot the computer for the code to take effect. But when i reboot it, all my machines shut down and i have to start them again. I could just save the current machine states to a file and then load that when the computer starts.
Cloudy #6
Posted 14 April 2012 - 07:13 PM
Yeah, if you want to keep the state between reboots that would be the best idea. While running the program from itself a few times won't cause an issue, it will add up.
Backplague #7
Posted 14 April 2012 - 07:15 PM
Yeah, got the saving states working. Wasnt so hard now i understand the filesystem.
atomhell #8
Posted 15 May 2012 - 01:59 AM
In response to a user's code that included:

shell.run("shutdown")

A user "Kaleb702" said:

...
os.shutdown()
...
Why? Because now it's just a little bit easier to change the file and directory it is installed to. Also, it now uses the proper practice for shutting down the computer. Avoid shell.run like the plague.

is there an explanation or is this a false claim? The poster did not explain why to avoid shell.run.

http://www.computerc...findpost__p__76
MysticT #9
Posted 15 May 2012 - 02:21 AM
Well, you shouldn't use shell.run() if you can replace it with some function calls, even when it's shorter to write shell.run("program"), it adds the overhead of loading the program from the file and then running it. It might not be noticeable, but it's there.
a shell.run() call would do something like this:

function shell.run(_sCommand, ...)
	local _sPath
	if tAliases[_sCommand] ~= nil then
		_sCommand = tAliases[_sCommand]
	end
	local sStartChar = string.sub(_sCommand, 1, 1)
	if sStartChar == "/" or sStartChar == "" then
		local path = fs.combine( "", _sCommand )
		if fs.exists(path) and not fs.isDir(path) then
			_sPath = path
		end
	end
	if _sPath == nil then
		for path in string.gmatch(sPath, "[^:]+") do
			local p
			local sStartChar = string.sub( _sPath, 1, 1 )
			if sStartChar == "/" or sStartChar == "" then
				p = fs.combine("", path)
			else
				p = fs.combine(sDir, path)
			end
			path = fs.combine(p, _sCommand)
			if fs.exists(path) and not fs.isDir(path) then
				_sPath = path
			end
		end
	end
	if _sPath ~= nil then
		tProgramStack[#tProgramStack + 1] = _sPath
		   local result = os.run(tEnv, _sPath, ...)
		tProgramStack[#tProgramStack] = nil
		return result
	   else
		print( "No such program" )
		return false
	end
end
And it's a lot more that just doing os.shutdown() or term.clear().

Also, using shell.run() makes your program vulnerable to virus. If someone creates a file called "clear" in the root directory and you use shell.run("clear"), it would execute that file, wich could be a virus (or something else that it's not what you wanted). So you shouldn't rely on external programs.
MathManiac #10
Posted 17 May 2012 - 04:20 AM
wow. Sometimes, I just use shell.run() for, say, a program I made, such as "replace <source> <destination>"