-
-
Notifications
You must be signed in to change notification settings - Fork 450
/
exec.go
executable file
·79 lines (67 loc) · 1.63 KB
/
exec.go
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
package main
import (
"fmt"
"os/exec"
"strings"
)
func pipe_cmd(cmds ...*exec.Cmd) ([]byte, error) {
for i, cmd := range cmds[:len(cmds)-1] {
out, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
cmd.Start()
cmds[i+1].Stdin = out
}
ret, err := cmds[len(cmds)-1].Output()
if err != nil {
return nil, err
}
return ret, nil
}
func main() {
datestr, err := exec.Command("date").Output()
if err != nil {
fmt.Println(err)
}
fmt.Print("Date String: ", string(datestr))
var datecmd *exec.Cmd
var lscmd *exec.Cmd
//of course the following lines are realy useless (they are only for example purposes)
datecmd = exec.Command("date", "+%d.%m.%Y %H:%M:%S")
grep1 := exec.Command("grep", "-o", "\\d\\d.\\d\\d.\\d\\d\\d\\d")
date, err1 := pipe_cmd(datecmd, grep1)
datecmd = exec.Command("date", "+%d.%m.%Y %H:%M:%S")
grep2 := exec.Command("grep", "-o", "\\d\\d:\\d\\d:\\d\\d")
time, err2 := pipe_cmd(datecmd, grep2)
lscmd = exec.Command("ls", "-ltraR", "./")
grep3 := exec.Command("grep", "-o", "[^ \\./]\\+\\.[^ \\./]\\+")
files, err3 := pipe_cmd(lscmd, grep3)
if err1 != nil {
fmt.Print(err1)
} else {
fmt.Print("Date: ", string(date))
}
if err2 != nil {
fmt.Print(err2)
} else {
fmt.Print("Time: ", string(time))
}
counter := make(map[string]int)
var ok bool
if err3 != nil {
fmt.Print(err3)
} else {
strarray := strings.Split(string(files), "\n")
for i := 0; i < len(strarray); i++ {
if strings.Trim(strarray[i], " .") != "" {
if _, ok = counter[strarray[i]]; ok {
counter[strarray[i]]++
} else {
counter[strarray[i]] = 1
}
}
}
fmt.Println("Files: ", counter)
}
}