此API提供了与shell相关的功能,例如shell的当前工作目录、程序查找目录和别名。
shell.getAlias(alias: string): string
nil
。shell.setAlias(alias: string, value: string or nil)
nil
作为参数以移除一条别名。请注意别名不仅限于程序名,也可以包含参数。例如,view
就是 edit -r
的默认别名。shell.aliases(): function
shell.getWorkingDirectory(): string
os.getenv("PWD")
的别名。shell.setWorkingDirectory(dir: string)
os.setenv("PWD", dir)
的经校验版本。shell.getPath(): string
shell.resolve
使用的搜索路径。此结果可能包含多条路径,中间用冒号(:
)隔开。
此函数为 os.getenv("PATH")
的别名。shell.setPath(value: string)
shell.setPath(shell.getPath() .. ":/some/path")
os.setenv("PATH", value)
的别名。shell.resolve(path: string[, ext: string]): string
path
只应当包含文件名(不含后缀)。此函数会首先搜索工作目录,然后是搜索目录的所有条目(参见 getPath
/setPath
)。
若不存在名称与给定值精确匹配的文件,并且给出了扩展名,此函数还会检查是否存在名称为给定名称加上给定后缀的文件,即 path .. "." .. ext
。shell.execute(command: string, env: table[, ...]): boolean ...
os.getenv("SHELL")
)并将命令作为参数传递给它。 env
是提供给shell使用的环境表,以备你希望够在沙盒环境运行被调用程序,或者防止它污染调用者的命名空间。附加参数会被直接传递给基于给定命令启动的第一个程序,因此你可以向程序传递非字符串值。
函数的返回值类似于 pcall
和 coroutine.resume
:第一个返回值为布尔值,代表了执行是否成功。当发生错误时,第二个返回值为详细报错信息。其他情况下第一条后的返回值均为指定程序结束后返回的返回值。shell.parse(...): table, table
-
进行标记,所有的选项都只能为单个字母长,因为一个-
后面跟着多个字母会被认为是多个选项。以两个破折号开头的选项不会被拆分,也就可以有多个字符长。并且,双破折号的选项还可以使用等于号赋值。下列样例均假定脚本 program
使用 local args, ops = shell.parse(...)
解析选项。
例1:program
# args
为 {}
# ops
为 {}
.
例2:program -abC -d arg1 arg2
# args
为 {"arg1", "arg2"}
# ops
为 {a=true,b=true,C=true,d=true}
.
例3:program -abC --dog arg1 arg2
# args
为 {"arg1", "arg2"}
# ops
为 {a=true,b=true,C=true,dog=true}
.
例4:program -abC --dog=foo arg1 arg2
# args
为 {"arg1", "arg2"}
# ops
为 {a=true,b=true,C=true,dog="foo"}
.
在下一个样例中,请注意 dog
前面的单破折号。这使得后续所有字符均被解析为单独字符。
例5:program -abC -dog=foo arg1 arg2
# args
为 {"arg1", "arg2"}
# ops
为 {a=true,b=true,C=true,d=true,g=true,["="]=true,f=true,o=true}
.