File: pipe.go

package info (click to toggle)
golang-github-junegunn-go-shellwords 0.0~git20240813.a62c48c-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 120 kB
  • sloc: sh: 9; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 637 bytes parent folder | download | duplicates (3)
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
// +build ignore

package main

import (
	"fmt"
	"log"

	"github.com/mattn/go-shellwords"
)

func isSpace(r byte) bool {
	switch r {
	case ' ', '\t', '\r', '\n':
		return true
	}
	return false
}

func main() {
	line := `
	/usr/bin/ls -la | sort 2>&1 | tee files.log
	`
	parser := shellwords.NewParser()

	for {
		args, err := parser.Parse(line)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(args)
		if parser.Position < 0 {
			break
		}
		i := parser.Position
		for ; i < len(line); i++ {
			if isSpace(line[i]) {
				break
			}
		}
		fmt.Println(string([]rune(line)[parser.Position:i]))
		line = string([]rune(line)[i+1:])
	}
}