forked from zserge/luash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
57 lines (48 loc) · 1.99 KB
/
example.lua
File metadata and controls
57 lines (48 loc) · 1.99 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
local sh = require('sh')
-- any shell command can be called as a function
print('User:', sh.whoami())
print('Current directory:', sh.pwd())
-- commands can be grouped into the pipeline as nested functions
print('Files in /bin:', sh.wc(sh.ls('/bin'), '-l'))
-- commands can be chained as in unix shell pipeline
print(sh.echo('Hello World'):sed("s/Hello/Goodbye/g"))
-- Lua allows to omit parens
print(sh.echo 'Hello World' :sed "s/Hello/Goodbye/g")
-- shelua allows you to concatenate command outputs with ..
print((sh.echo 'Hello World' :sed "s/Hello/Goodbye/g") .. " " .. sh.echo 'Hello Lua' :sed "s/Hello/Goodbye/g")
-- intermediate output in the pipeline can be stored into variables
local sedecho = sh.sed(sh.echo('hello', 'world'), 's/world/Lua/g')
print('output:', sedecho)
print('exit code:', sedecho.__exitcode)
local res = sh.tr(sedecho, '[[:lower:]]', '[[:upper:]]')
print('output+tr:', res)
-- command functions can be created dynamically. Optionally, some arguments
-- can be prepended (like partially applied functions)
local e = sh('echo')
local greet = sh('echo', 'hello')
print(e('this', 'is', 'some', 'output'))
print(greet('world'))
print(greet('foo'))
-- sh module itself can be called as a function
print(sh('type')('ls'))
print(sh 'type' 'ls')
-- changing settings for sh variable
sh.escape_args = true
print(sh.echo 'Hello World' :sed "s/Hello World/Goodbye Universe/g")
sh.escape_args = false
-- cloning sh with new settings, and "proper_pipes" setting (and others)
local nsh = sh(function (opts)
opts.proper_pipes = true
opts.escape_args = true
opts.assert_zero = true
opts.repr[opts.shell or "posix"].transforms = {
function(cmd)
print(cmd)
return cmd
end
}
return opts
end)
print(nsh.echo 'Hello world' :sed "s/Hello/Goodbye/g")
print(nsh.sed(nsh.echo 'Hello world', nsh.echo 'Hello world', "s/Hello/Goodbye/g"))
print(nsh.echo 'Hello World' :sed(nsh.echo 'Hello World', nsh.echo 'Hello World' :sed(nsh.echo 'Hello World', "s/Hello/Goodbye/g"), "s/World/Universe/g"))