1

I am trying to run the following command inside child process exec of nodejs and it is failing:

wget -qO- some.website | awk -F \"</*TD ALIGN=LEFT>|</*TD>|<*/A>\" \'/<TD ALIGN=LEFT>.*[A-Z]/ {print $12}\' | sed -n 1p

I tested the command from a shell script and it runs perfectly. However, when I run it from nodejs it is giving the following error:

/bin/sh: TD: No such file or directory

Please help.

EDIT:

var reportTCC = function() {
     let child = exec(configs.command, function(error, stdout, stderr) {
         if (error !== null) {
             log.error('Error executing the command to retrieve Run Info: ' + error);
             stdout = "UNKNOWN"
         }
         let run_type = stdout;
     });
 };

configs.command has the command that I am passing

EDIT2:

"command": "ssh -t user@domain 'wget -qO- some.website | awk -F \"</*TD ALIGN=LEFT>|</*TD>|<*/A>\" \"/<TD ALIGN=LEFT>.*[A-Z]/ {print $12}\" | sed -n 1p'",
Invariance
  • 313
  • 6
  • 16
  • 1
    Must be a quoting issue. Please add the code where you `exec` your command from inside your Nodejs app. – pawamoy Apr 12 '18 at 14:23
  • I made an edit to show how I am using the command. – Invariance Apr 12 '18 at 14:34
  • We need to see the value of `configs.command` now ^^' – pawamoy Apr 12 '18 at 14:37
  • You're escaping the command, what happens when you won't? 'I tested the command from a shell script and it runs perfectly', I've tested your command in shell, and it doesn't work perfectly with these escape characters, so how did you test? – kenorb Apr 12 '18 at 14:40
  • I added the escaping just for nodejs. I made edits showing the command in configs.command – Invariance Apr 12 '18 at 14:47
  • I think you don't need escaping, what happens when you drop them? – kenorb Apr 12 '18 at 15:00

1 Answers1

1

Since your escape characters has been passed into ssh command, most likely you don't need to escape awk arguments. Based on the fact that your ssh is using single quotes such as:

ssh -t user@domain 'command to invoke'

use double-quotes to wrap your arguments inside these single quotes (so make it consistent, as in your first example you're mixing both). See also: How to escape a double quote inside double quotes?

When you're not sure how the command is invoked, invoke set -x shell command to output the tracing messages, or run the command as:

ssh -t user@domain bash -x -c 'command to invoke'

Another suggestion is not to use awk and regular expressions to parse your HTML, but use relevant tools such as: pup, xpup or html-xml-utils. See: About parsing html and extract data using shell.

kenorb
  • 155,785
  • 88
  • 678
  • 743