gpt4 book ai didi

node.js - 获取 bash 脚本来运行 node.js

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:35 25 4
gpt4 key购买 nike

我有一个接受整数参数的node.js 文件。

我正在尝试编写一个 bash 脚本,用于查找可用核心的数量并为每个可用核心启动 main.js。

对于 1 个核心,它应该调用:

node main.js 3000 &

对于 2 核:

node main.js 3000 &
node main.js 30001 &

等等...

这是我的 bash 脚本:

#!/bin/bash
numCores=`getconf _NPROCESSORS_ONLN`
i=0
j=3000
while [ $i < $numCores ]
do
j=$($j+$i)
node /myApp/main.js $j &
i=$($i+1)
done

当我尝试运行它时,出现此错误:

bash launchnode.sh
launchnode.sh: line 5: 2: No such file or directory

main.js 和 launchnode.sh 在同一目录中。

有什么帮助吗?

最佳答案

错误在这里:

while [ $i < $numCores ]

在 bash 中 <用于输入重定向。 [test 的别名命令,和 test 0 < 2表示“将名为 2 的文件中的输入发送到命令 test 0

相反,请使用 test-lt小于比较的选项。另外,不要忘记引用您的变量:

while [ "$i" -lt "$numCores" ]

或者,如果您仅针对 bash,则可以使用算术扩展:

while (( i < numCores ))

您还需要在后续行中使用双括号进行算术运算:

i=$(($j+$i))

关于node.js - 获取 bash 脚本来运行 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com