FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sh/shell/example_test.go at master · sjas/sh · GitHub
sjas
/
sh
Public
forked from
mvdan/sh
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sh
/
shell
/
example_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (70 loc) · 1.55 KB
Breadcrumbs
sh
/
shell
/
example_test.go
Copy path
File metadata and controls
80 lines (70 loc) · 1.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
// Copyright (c) 2018, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package
shell_test
import
(
"context"
"fmt"
"io/ioutil"
"os"
"mvdan.cc/sh/v3/shell"
)
func
ExampleExpand
() {
env
:=
func
(
name
string
)
string
{
switch
name
{
case
"HOME"
:
return
"/home/user"
}
return
""
// leave the rest unset
}
out
,
_
:=
shell
.
Expand
(
"No place like $HOME"
,
env
)
fmt
.
Println
(
out
)
out
,
_
=
shell
.
Expand
(
"Some vars are ${missing:-awesome}"
,
env
)
fmt
.
Println
(
out
)
out
,
_
=
shell
.
Expand
(
"Math is fun! $((12 * 34))"
,
nil
)
fmt
.
Println
(
out
)
// Output:
// No place like /home/user
// Some vars are awesome
// Math is fun! 408
}
func
ExampleFields
() {
env
:=
func
(
name
string
)
string
{
switch
name
{
case
"foo"
:
return
"bar baz"
}
return
""
// leave the rest unset
}
out
,
_
:=
shell
.
Fields
(
`"many quoted" ' strings '`
,
env
)
fmt
.
Printf
(
"%#v
\n
"
,
out
)
out
,
_
=
shell
.
Fields
(
"unquoted $foo"
,
env
)
fmt
.
Printf
(
"%#v
\n
"
,
out
)
out
,
_
=
shell
.
Fields
(
`quoted "$foo"`
,
env
)
fmt
.
Printf
(
"%#v
\n
"
,
out
)
// Output:
// []string{"many quoted", " strings "}
// []string{"unquoted", "bar", "baz"}
// []string{"quoted", "bar baz"}
}
func
ExampleSourceFile
() {
src
:=
`
foo=abc
foo+=012
if true; then
bar=$(echo example_*.go)
fi
`
ioutil
.
WriteFile
(
"f.sh"
, []
byte
(
src
),
0666
)
defer
os
.
Remove
(
"f.sh"
)
vars
,
err
:=
shell
.
SourceFile
(
context
.
TODO
(),
"f.sh"
)
if
err
!=
nil
{
return
}
fmt
.
Println
(
len
(
vars
))
fmt
.
Println
(
"foo"
,
vars
[
"foo"
])
fmt
.
Println
(
"bar"
,
vars
[
"bar"
])
// Output:
// 2
// foo abc012
// bar example_test.go
}
Back
|
FazBrowse Home
|
New Git URL