在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):zserge/luash开源软件地址(OpenSource Url):https://github.com/zserge/luash开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):luashTiny library for shell scripting with Lua (inspired by Python's sh module). InstallVia luarocks:
Or just clone this repo and copy sh.lua into your project. Simple usageEvery command that can be called via os.execute can be used a global function. All the arguments passed into the function become command arguments. require('sh')
local wd = tostring(pwd()) -- calls `pwd` and returns its output as a string
local files = tostring(ls('/tmp')) -- calls `ls /tmp`
for f in string.gmatch(files, "[^\n]+") do
print(f)
end Command input and pipelinesIf command argument is a table which has a The each command function returns a structure that contains the Note that the commands are not running in parallel (because Lua can only handle one I/O loop at a time). So the inner-most command is executed, its output is read, the the outer command is execute with the output redirected etc. require('sh')
local words = 'foo\nbar\nfoo\nbaz\n'
local u = uniq(sort({__input = words})) -- like $(echo ... | sort | uniq)
print(u) -- prints "bar", "baz", "foo" Pipelines can be also written as chained function calls. Lua allows to omit parens, so the syntax really resembles unix shell: -- $ ls /bin | grep $filter | wc -l
-- normal syntax
wc(grep(ls('/bin'), filter), '-l')
-- chained syntax
ls('/bin'):grep(filter):wc('-l')
-- chained syntax without parens
ls '/bin' : grep filter : wc '-l' Partial commands and commands with tricky namesYou can use local sh = require('sh')
local truecmd = sh.command('true') -- because "true" is a Lua keyword
local chrome = sh.command('google-chrome') -- because '-' is an operator
local gittag = sh.command('git', 'tag') -- gittag(...) is same as git('tag', ...)
gittag('-l') -- list all git tags
Exit status and signal valuesEach command function returns a table with SInce Command arguments as a tableKey-value arguments can be also specified as argument table pairs: require('sh')
-- $ somecommand --format=long --interactive -u=0
somecommand({format="long", interactive=true, u=0}) It becomes handy if you need to toggle or modify certain command line argumnents without manually changing the argumnts list. LicenseCode is distributed under the MIT license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论