PIG-5362: Parameter substitution of shell cmd results doesn't handle backslash (wlauer via rohini)

git-svn-id: https://svn.apache.org/repos/asf/pig/trunk@1843692 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 862d9e6..d240afa 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -88,6 +88,8 @@
  
 BUG FIXES
 
+PIG-5362: Parameter substitution of shell cmd results doesn't handle backslash (wlauer via rohini)
+
 PIG-5355: Negative progress report by HBaseTableRecordReader (satishsaley via knoguchi)
 
 PIG-5341: PigStorage with -tagFile/-tagPath produces incorrect results with column pruning (knoguchi)
diff --git a/src/org/apache/pig/tools/parameters/PreprocessorContext.java b/src/org/apache/pig/tools/parameters/PreprocessorContext.java
index 2d61a3c..f44c645 100644
--- a/src/org/apache/pig/tools/parameters/PreprocessorContext.java
+++ b/src/org/apache/pig/tools/parameters/PreprocessorContext.java
@@ -352,9 +352,7 @@
                     throw new ParameterSubstitutionException(message);
                 }
                 val = paramval_get(key);
-                if (val.contains("$")) {
-                    val = val.replaceAll("(?<!\\\\)\\$", "\\\\\\$");
-                }
+                val = Matcher.quoteReplacement(val);
                 replaced_line = replaced_line.replaceFirst("\\$\\{"+key+"\\}", val);
             }
         }
@@ -379,9 +377,7 @@
                     throw new ParameterSubstitutionException(message);
                 }
                 val = paramval_get(key);
-                if (val.contains("$")) {
-                    val = val.replaceAll("(?<!\\\\)\\$", "\\\\\\$");
-                }
+                val = Matcher.quoteReplacement(val);
                 replaced_line = replaced_line.replaceFirst("\\$"+key, val);
             }
         }
diff --git a/test/org/apache/pig/tools/parameters/TestPreprocessorContext.java b/test/org/apache/pig/tools/parameters/TestPreprocessorContext.java
index f6aea7c..13f75c5 100644
--- a/test/org/apache/pig/tools/parameters/TestPreprocessorContext.java
+++ b/test/org/apache/pig/tools/parameters/TestPreprocessorContext.java
@@ -66,4 +66,15 @@
             );
         }
     }
+    
+    @Test
+    public void testEscaping() throws ParameterSubstitutionException, FrontendException {
+        PreprocessorContext ctx = new PreprocessorContext(0);
+        // quote argument to echo so that the shell doesn't treat the backslash as an escape and consume it
+        String cmd = "echo '$\\stuff'";
+        ctx.processShellCmd("some_value", "`" + cmd + "`");
+
+        String subVal = ctx.substitute("$some_value");
+        assertEquals("$\\stuff", subVal);
+    }
 }