func ExampleQuote() { filename := "myfile; rm -rf /" prog := "/bin/ls" unescapedCommand := strings.Join([]string{prog, filename}, " ") escapedCommand := strings.Join([]string{prog, shellescape.Quote(filename)}, " ") fmt.Println(unescapedCommand) fmt.Println(escapedCommand) // Output: // /bin/ls myfile; rm -rf / // /bin/ls 'myfile; rm -rf /' }
func main() { firstScan := true scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { if firstScan { firstScan = false } else { fmt.Printf(" ") } fmt.Printf("%s", shellescape.Quote(scanner.Text())) } }
func TestUnquotedString(t *testing.T) { s := shellescape.Quote(`no quotes`) expected := `'no quotes'` assertEqual(t, s, expected) }
func TestSingleQuotedString(t *testing.T) { s := shellescape.Quote(`'single quoted'`) expected := `''"'"'single quoted'"'"''` assertEqual(t, s, expected) }
func TestDoubleQuotedString(t *testing.T) { s := shellescape.Quote(`"double quoted"`) expected := `'"double quoted"'` assertEqual(t, s, expected) }
func TestEmptyString(t *testing.T) { s := shellescape.Quote("") expected := "''" assertEqual(t, s, expected) }