概述
我想将参数传递给来自stdin的节点脚本。
一般情况下,我是为了这样的事情而开枪
nodeScript.js | node {{--attach-args??}} --verbose --dry-run它的作用和
node nodeScript.js --verbose --dry-run更详细
下面是一个简化的说明脚本,dumpargs.js
console.log("the arguments you passed in were");
console.log(process.argv);
console.log("");这样你就可以:
node dumpargs.js --verbose --dry-run file.txt
[ 'node',
'/home/bill-murray/Documents/dumpargs.js',
'--verbose',
'--dry-run',
'file.js' ]现在的问题是,这个脚本是否贯穿stdin (例如,通过cat或curl)
cat dumpars.js | node
the arguments you passed in were
[ 'node' ]有什么好办法把论点传递给它吗?
非节点:使用bash,这次使用
dumpargs.sh传入的参数为“printf "> $@”echo“ 答案看起来就像 cat dumpargs.sh \ bash -s -“
发布于 2018-12-24 13:29:55
这个用例有一个特定的语法。医生说:
- Alias for stdin, analogous to the use of - in other command line utilities, meaning
that the script will be read from stdin, and the rest of the options are passed to
that script.
-- Indicate the end of node options. Pass the rest of the arguments to the script.
If no script filename or eval/print script is supplied prior to this, then the next
argument will be used as a script filename.因此,只需做以下几点:
$ cat script.js | node - args1 args2 ...例如,这将返回"hello world“:
$ echo "console.log(process.argv[2], process.argv[3])" | node - hello world发布于 2015-08-24 01:39:46
这不是很漂亮,但很管用。
对node的调用将启动REPL,因此您的问题应该等同于从终端手动设置/使用argv。试着做这样的事情:
// argv.js
process.argv[1] = 'asdf';
process.argv[2] = '1234';做cat argv.js dumpargs.js | node。
https://stackoverflow.com/questions/32173065
复制相似问题