[英]Why won't this node.js script stay open in the terminal?
I'd like to know how to run this script without entering node script_name.js
into the terminal. 我想知道如何在不将
node script_name.js
输入终端的情况下运行此脚本。 The prompt is displayed when I start the script from the terminal, but when I try to run the script as a program (from the file manager, not the command line), it simply opens and closes the terminal, without displaying a prompt. 当我从终端启动脚本时会显示提示,但是当我尝试将脚本作为程序运行时(从文件管理器而不是命令行),它只是打开并关闭终端,而不显示提示。
#!/usr/local/bin/node
var prompt = require('prompt');
//var stuff = require("./stuff");
prompt.start();
prompt.get(['username', 'email'], function (err, result) {
if (err) { return onErr(err); }
console.log('Command-line input received:');
console.log(' Username: ' + result.username);
console.log(' Email: ' + result.email);
});
function onErr(err) {
console.log(err);
return 1;
}
Here is the expected output (with input) for this program: 以下是此程序的预期输出(带输入):
anderson@anderson-Ideapad-Z560:~/AeroFS/node.js examples$ node node_prompt_demo.js
prompt: username: blah
prompt: email: blah@example.com
Command-line input received:
Username: blah
Email: blah@example.com
This almost certainly has to do with the controlling tty. 这几乎肯定与控制tty有关。 I'm guessing the file manager you're using doesn't give you a
stdin
that's attached to anything and it just exits without any input. 我猜你正在使用的文件管理器没有给你一个附加到任何东西的
stdin
,它只是在没有任何输入的情况下退出。
Your best bet would be to wrap this in a program that would give it a valid stdin
. 你最好的选择是将它包装在一个能给它一个有效
stdin
。 For example, on a Mac, I run the node script within Terminal.app so it always gets a valid stdin. 例如,在Mac上,我在Terminal.app中运行节点脚本,因此它总是获得有效的标准输入。 Should be able to do something similar in XWindows.
应该能够在XWindows中做类似的事情。 If you're not using a window manager, you could also try using "
expect
" and ' screen
'. 如果您没有使用窗口管理器,您也可以尝试使用“
expect
”和“ screen
”。 The combination of these should get you what you want. 这些组合应该可以满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.