如何从 BASH 中的节点脚本访问返回值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43169002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a return value from a node script in BASH?
提问by Attilah
Let's say I have a bash script that calls a node script. I've tried to do it like this:
假设我有一个调用节点脚本的 bash 脚本。我试过这样做:
b.sh file:
b.sh 文件:
#!/bin/bash
v=$(node app.js)
echo "$v"
app.js file:
app.js 文件:
#!/usr/bin/env node
function f() {
return "test";
}
return f();
How do I access the value returned by the node script ("test") from my bash script ?
如何从我的 bash 脚本访问节点脚本(“test”)返回的值?
回答by jm666
@Daniel Lizik gave an good answer (now deleted) for the part: how to output the value, e.g. using his answer:
@Daniel Lizik 给出了一个很好的答案(现已删除):如何输出值,例如使用他的答案:
#!/usr/bin/env node
function f() {
return "test";
}
console.log(f())
And for the part how to capture the value in bash, do exactly as in your question:
对于如何在 bash 中捕获值的部分,请完全按照您的问题进行操作:
#!/bin/bash
val=$(node app.js)
echo "node returned: $val"
the above prints:
以上打印:
node returned: test