user11527868
user11527868

Reputation:

nodeJS "exec" isn't executing a "sed" command that works from the command line

I can't figure out why my code isn't working.

I'm trying to edit this yml file: apktool.yml

...
versionInfo:
  versionCode: '1'
  versionName: '1.0'

In windows, this command line modifies versionCode to '2' successfuly:

sed -i 's/versionCode: \'1\'/versionCode: \'2\'/g' app-debug\\apktool.yml

However, executed from the same directory, this nodeJS code fails to modify the file:

const {exec} = require('child_process');
const {promisify} = require('util');
const exec_async = promisify(exec);
await exec_async("sed -i 's/versionCode: \'1\'/versionCode: \'2\'/g' app-debug\\apktool.yml")
    .catch(err => {console.log(err)});

No error is catched, and the file isn't modified. I also tried to "sleep 5" before the command to make sure it's not a timing issue. Any ideas what's wrong here?

Upvotes: 1

Views: 423

Answers (1)

user11527868
user11527868

Reputation:

Alright, that was a brain-breaker, but solved it: the sed command needs to include double quotes to contain single quotes, and the nodeJS exec argument should also be wrapped in escaped, double quotes. this worked:

await exec_async("sed -i \"s/versionCode: '1'/versionCode: '2'/g\" app-debug\\apktool.yml")

Upvotes: 1

Related Questions