From 9dc32850fea51a7b062fd91fb21baa422c26bf64 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 25 Feb 2019 11:43:52 +0900 Subject: [PATCH 1/4] Handle FOO=$(command) --- shellwords.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shellwords.go b/shellwords.go index f93e264..4da62f1 100644 --- a/shellwords.go +++ b/shellwords.go @@ -4,6 +4,7 @@ import ( "errors" "os" "regexp" + "strings" ) var ( @@ -131,7 +132,7 @@ loop: } case '(': if !singleQuoted && !doubleQuoted && !backQuote { - if !dollarQuote && len(buf) > 0 && buf == "$" { + if !dollarQuote && len(buf) > 0 && strings.HasSuffix(buf, "$") { dollarQuote = true buf += "(" continue From a46243cae42cf3eaedebabce33a1f71ea8c4c831 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 25 Feb 2019 11:46:07 +0900 Subject: [PATCH 2/4] Add test case --- shellwords_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shellwords_test.go b/shellwords_test.go index 2bacc42..823c802 100644 --- a/shellwords_test.go +++ b/shellwords_test.go @@ -124,6 +124,10 @@ func TestBacktickError(t *testing.T) { if err == nil { t.Fatal("Should be an error") } + _, err = parser.Parse(`echo FOO=$(echo1)`) + if err == nil { + t.Fatal("Should be an error") + } _, err = parser.Parse(`echo $(echo1`) if err == nil { t.Fatal("Should be an error") From 04f810b90a2d8d0ad58902243fc43bb517b9a352 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 25 Feb 2019 11:59:29 +0900 Subject: [PATCH 3/4] Fix $(command) --- shellwords.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shellwords.go b/shellwords.go index 4da62f1..41429d8 100644 --- a/shellwords.go +++ b/shellwords.go @@ -121,7 +121,11 @@ loop: if err != nil { return nil, err } - buf = out + if r == ')' { + buf = buf[:len(buf)-len(backtick)-2] + out + } else { + buf = buf[:len(buf)-len(backtick)-1] + out + } } backtick = "" dollarQuote = !dollarQuote @@ -132,7 +136,7 @@ loop: } case '(': if !singleQuoted && !doubleQuoted && !backQuote { - if !dollarQuote && len(buf) > 0 && strings.HasSuffix(buf, "$") { + if !dollarQuote && strings.HasSuffix(buf, "$") { dollarQuote = true buf += "(" continue From f077c0cda7ff490d1cd1ec6dc54ab07179d8213f Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 25 Feb 2019 12:01:02 +0900 Subject: [PATCH 4/4] Fix test --- shellwords_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/shellwords_test.go b/shellwords_test.go index 823c802..65577fa 100644 --- a/shellwords_test.go +++ b/shellwords_test.go @@ -93,6 +93,15 @@ func TestBacktick(t *testing.T) { t.Fatalf("Expected %#v, but %#v:", expected, args) } + args, err = parser.Parse(`echo bar=$(echo 200)cm`) + if err != nil { + t.Fatal(err) + } + expected = []string{"echo", "bar=200cm"} + if !reflect.DeepEqual(args, expected) { + t.Fatalf("Expected %#v, but %#v:", expected, args) + } + parser.ParseBacktick = false args, err = parser.Parse(`echo $(echo "foo")`) expected = []string{"echo", `$(echo "foo")`}