英文:
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());
如果你想配置 stderr
和 stdout
,请使用 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论