diff is not working in execSync producing error when file doesnot match

huangapple go评论67阅读模式
英文:

diff is not working in execSync producing error when file doesnot match

问题

diff 命令在我的文件不匹配时会出错 `const exec_options = {
    cwd : "/home" , 
    encoding  : 'utf8',
 }
`

let {stdout,stderr,err} =    execSync(`diff  output.txt answer.txt` , exec_options);
if(err){
    console.log(err);
}
console.log(stdout);
    

上述命令在终端中使用 exec  exec sync 都可以得到期望的结果但在 exec sync 中不起作用
英文:

diff command gives error when my files does not match const exec_options = {
cwd : "/home" ,
encoding : 'utf8',
}

    let {stdout,stderr,err} =    execSync(`diff  output.txt answer.txt` , exec_options);
if(err){
    console.log(err);
}
console.log(stdout);

The above command gives desired result with exec and in terminal but does not work using exec sync

答案1

得分: 1

execSync 返回字符串或缓冲区,你的代码应该是:

const stdout = execSync(`diff output.txt answer.txt`, exec_options);
console.log(stdout.toString());

如果你想配置 stderrstdout,请使用 spawnSync

execSync 为什么失败?

因为 diff 在有差异时返回非零值,而 execSync 仅在值为 0 时确定正常终止。所以,如果有差异,diff 返回 1,execSync 将认为命令失败。

实际上,我们会得到这个消息:

try {
  let stdout = execSync(`diff a b`, exec_options)
  console.log(stdout.toString())
} catch (e: any) {
  console.log(e.message)
  console.log(e.stdout.toString())
}
Command failed: diff a b
2c2
< a
---
> b

一些测试:

try {
  const command = 'diff a c'
  console.log(`command : ${command}`)
  let stdout = execSync(command, exec_options)
  console.log(stdout.toString())
} catch (e: any) {
  console.log('prev')
  if (e.status === 1) {
    console.log('diff result:')
    console.log(e.stdout.toString())
    console.log(e.stderr.toString())
  } else {
    console.log('error message : ')
    console.log(e.stdout.toString())
    console.log(e.stderr.toString())
  }
}

将会打印:

command : diff a a // 在相同情况下,什么都不返回
~~~~~~~~
command : diff a b // 在不同情况下
prev
diff result:
2c2                // e.stdout 存在
< a
---
> b
                   // e.stderr 不存在
~~~~~~~~
command : diff a c // 在失败情况下
diff: c: No such file or directory // 在 execSync 中打印
prev
error message : 
                   // e.stdout 不存在
diff: c: No such file or directory // e.stderr 存在
英文:

execSync returns string or buffer

Your code should be..

const stdout = execSync(`diff output.txt answer.txt` , exec_options);
console.log(stdout.toString());

if you want to configure stderr and stdout, uses spawnSync

Why execSync is fail ?

https://askubuntu.com/questions/698784/exit-code-of-diff

because, diff returns non-zero when something is different.

and execSync determines normal termination only when the value is 0.

so if something is different, diff returns 1, and execSync guess command failed.

actually we get that message.

  try {
    let stdout = execSync(`diff a b`, exec_options)
    console.log(stdout.toString())
  } catch (e: any) {
    console.log(e.message)
    console.log(e.stdout.toString())
  }
Command failed: diff a b
2c2
< a
---
> b

some test

  try {
    const command = 'diff a c'
    console.log(`command : ${command}`)
    let stdout = execSync(command, exec_options)
    console.log(stdout.toString())
  } catch (e: any) {
    console.log('prev')
    if (e.status === 1) {
      console.log('diff result:')
      console.log(e.stdout.toString())
      console.log(e.stderr.toString())
    } else {
      console.log('error message : ')
      console.log(e.stdout.toString())
      console.log(e.stderr.toString())
    }
  }

will print

command : diff a a // in same case
                   // nothing returns
~~~~~~~~
command : diff a b // in diff case
prev
diff result:
2c2                // e.stdout exist
< a
---
> b
                   // e.stderr doesn't exist
~~~~~~~~
command : diff a c // in fail case
diff: c: No such file or directory // print at execSync.
prev
error message : 
                   // e.stdout doesn't exist
diff: c: No such file or directory // e.stderr exist

huangapple
  • 本文由 发表于 2023年4月17日 17:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定