-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.lua
More file actions
75 lines (67 loc) · 1.71 KB
/
shell.lua
File metadata and controls
75 lines (67 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
local fs = filesystem or require("filesystem")
local computer = computer or require("computer")
local component = component or require("component")
local event = event or require("event")
local args = {...}
local function split(inputstr)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, '([^|]+)') do
t[i] = str
i = i + 1
end
return t
end
local function exec(script,arg)
local arg = arg or nil
assert(dofile(tostring(script))(arg))
end
local commands = {
echo = print,
exit = "exit",
exec = exec,
}
local function runCommand(command,cargs)
cargs = cargs or ""
if command == "exit" then
for k,_ in pairs(commands) do
if commands.k == "exit" then
os.exit(0)
end
end
else
return commands[command](cargs)
end
end
local usage = [[USAGE:
how to layout a command
<command>|<arg>
example from file
echo|hello world
example from command line
echo|"hello world"
shell -h : shows this message
shell -c [command] : runs single command
shell -f [filename.sh] : runs all commands in a file(do not include .sh)
]]
if args[1] == "-c" then
local rCommArgs = split(args[2])
runCommand(rCommArgs[1],rCommArgs[2])
elseif args[1] == "-f" then
local file = assert(io.open(args[2]..".sh","r"),"File Not Found")
local com = {}
for line in file:lines() do
table.insert (com, line);
end
file:close()
for i,v in ipairs(com) do
local rCommArgs = split(com[i])
runCommand(rCommArgs[1],rCommArgs[2])
end
elseif args[1] == "-h" then
print(usage)
else
print(usage)
end