node.js - shelljs“未找到”错误
问题描述
我有以下 .sh 文件
// ping.sh
01: port="$1"
02: echo "pinging http://localhost:$port/ping"
03:
04: retry=0
05: while
06: sleep 1
07: api_response=$(curl --write-out %{http_code} --silent --output /dev/null "http://localhost:$port/ping")
08: echo "resp $api_response"
09: (( "$api_response" != "200" && ++retry < 100 ))
10: do :; done
当我直接用 运行它时./ping.sh 8080
,它工作正常。但是,当我使用 shelljs 运行它时,它会失败并出现以下错误。
// ping.js
require('shelljs').exec("./ping.sh 8080", {async:true});
$ node ping.js
pinging http://localhost:8080/ping
resp 000 ./ping.sh: 9: ./ping.sh: 000: not found
解决方案
它确实有效,只需添加#!/bin/bash
到ping.sh
工作概念验证:
ping.sh
#!/bin/bash
port="$1"
echo "pinging http://localhost:$port/ping"
retry=0
while
sleep 1
api_response=$(curl --write-out %{http_code} --silent --output /dev/null "http://localhost:$port/ping")
echo "resp $api_response"
(( "$api_response" != "200" && ++retry < 100 ))
do :; done
ping.js
require('shelljs').exec("./ping.sh 8080", {async:true});
终端输出:
node ping.js
pinging http://localhost:8080/ping
resp 000
resp 000
推荐阅读
- python - 我的单词比较程序忽略了我的打印功能
- macos - 无法通过 SSH 连接到在 MAC 上运行的 Docker 容器
- python - Python嵌套json解析和拆分值
- r - 使用 dplyr 的 start_with 跨特定列的行和
- uitableview - 当有baselineOffset 5的上标时,具有多行的UILabel不显示所有行
- eclipse-plugin - 在 Eclipse 中访问 ResourceException?
- java - 如何将已解析 JSON 的 Java 列表解析为大 JSON?
- c# - UWP - 如果数据源已更改,如何保存 ListViewItem 状态?
- ios - 检测 iPhone 连接到的 AirPlay 设备类型
- linux - 递归文本替换和文件扩展名重命名