我有一个文本文件,每一行都是我想传递给Node.js脚本的参数列表。以下是示例文件file.txt:
"This is the first argument" "This is the second argument"
为了演示起见,节点脚本仅为:
console.log(process.argv.slice(2));
我希望能够运行这个节点脚本来处理文本文件中的每一行,因此我制作了这个bash脚本run.sh:
while read line; do
node script.js $line
done < file.txt
当我运行这个bash脚本时,我得到了以下结果:
$ ./run.sh
[ '"This',
'is',
'the',
'first',
'argument"',
'"This',
'is',
'the',
'second',
'argument"' ]
但是当我直接运行 Node 脚本时,得到了预期的输出:
$ node script.js "This is the first argument" "This is the second argument"
[ 'This is the first argument',
'This is the second argument' ]
这是怎么回事?有没有更符合Node.js风格的方法来处理这个问题?