title: Shell script with pipes fails to execute on Linux using java Runtime Process
body: <p>I want to parse the result of npm ls command, namely the tree of dependencies to extract the root level ones. I use Java 1.8 and Runtime Process and ProcessBulder to do that. </p>
  
  <p>I run the code on two platforms: 
  1. Linux xxxxxxx-ThinkPad-W541 4.15.0-55-generic #60~16.04.2-Ubuntu SMP Thu Jul 4 09:03:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  2. Linux xxx.xxx.xxx.xx 2.6.32-754.3.5.el6.x86_64 #1 SMP Thu Aug 9 11:56:22 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux Red Hat Enterprise Linux Server release 6.10 (Santiago)</p>
  
  <p>On the Ubuntu it works without problems on RHEL Linux it fails</p>
  
  <p>This is my code to do the trick</p>
  
  <pre class="lang-java prettyprint-override"><code>String cmdStr1 = "export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin; cd " + packageDir.toString() + "; npm ls --prod --depth=0  &gt; " + this.workspaceDir.resolve("dependencies_tree.tmp") +  ";";
  String cmdStr2 = "cat " + this.workspaceDir.resolve("dependencies_tree.tmp") + " | sed -e 's/^├─┬ //;s/^├── //;s/^─┬ //;s/^└─┬ //;s/^└── //;' | sed '/^[^a-zA-Z0-9@.\\/-].*/d' | sed -e 's/\\^//' | sed '/deduped/d' | sed -e 's/UNMET DEPENDENCY //' | sed -e 's/UNMET OPTIONAL DEPENDENCY //' | sed '1 d' | sed -e 's/\\@/:/g' -e 's/^:/\\@/' | sort | uniq ";
  
  String[] cmd1 = { "/bin/sh", "-c", cmdStr1};
  String[] cmd2 = { "/bin/sh", "-c", cmdStr2};
  
  try {
  ...
    logger.debug("Running command: " + cmdStr1);
    Process p1 = Runtime.getRuntime().exec(cmd1);
    logger.trace(output(p1.getInputStream()));
    int exitCode1 = p1.waitFor();         
    logger.debug("Result 1: " + exitCode1);
  
    logger.debug("Running command: " + cmdStr2);
  
    Process p2 = new ProcessBuilder(cmd2).redirectOutput(new File(dependenciesFileAbsolutePath)).start();
  
    logger.debug(output(p2.getInputStream()));
    int exitCode2 = p2.waitFor();
    logger.debug("Result 2: " + exitCode2);
  
  } catch (Exception e) {
  }
  
  </code></pre>
  
  <p>The first command returns with exit code 1 the second with exit code 0.
  For the first command - it should return like that because of UNMET DEPENDENCIES in it's result.
  I can't get the second command working on RHEL</p>
  
  <p>The expected result - is a list of root level dependencies from the output of npm ls in dependenciesFileAbsolutePath
  Actual result an empty file.</p>
  
  <p>My suspection that is something wrong with the second command but why it doesn't work on RHEL only then</p>
  
  <p>Need your help!</p>