![로드된 nodejs 모듈을 Bash의 변수에 할당하는 방법은 무엇입니까?](https://linux55.com/image/172909/%EB%A1%9C%EB%93%9C%EB%90%9C%20nodejs%20%EB%AA%A8%EB%93%88%EC%9D%84%20Bash%EC%9D%98%20%EB%B3%80%EC%88%98%EC%97%90%20%ED%95%A0%EB%8B%B9%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F.png)
저는 아직 Bash 언어를 모릅니다. NPM 모듈을 만드는 방법을 배우기로 결정했습니다. 파일 연결을 통해 사용하고 있습니다. 하지만 저는 콘솔을 통해 이를 가능하게 하기로 결정했습니다.
예: 파일을 실행합니다.run.sh
이 코드가 실행될 것입니다
node -e 'require ("./ node_modules/@topus009/perf/x.js")'
하지만 이 코드는 그렇지 않습니다.
var = $ {node -e "require ('./ node_modules/@topus009/perf/x.js')"}
error - bad substitution
작동 방식:
- 로드된 모듈 내에서 함수가 내보내집니다.
- 모듈을 로드합니다.
- 그런 다음 bash 스크립팅을 수행하십시오.
- 그런 다음 이 모듈을 호출합니다.
하지만 나중에 사용하고 실행하기 위해 이 모듈을 얻는 방법을 모르겠습니다. 모듈을 변수에 전달하는 방법. Stackoverflow는 도움이 되지 않습니다. 한계에 도달했습니다.
#!/bin/bash
#...code for parsing bash variables from command line (WORKS)
nm="./node_modules/@topus009/perf"
benchmarkStart=${node -e "require('${nm}/benchmarkStart.js')"} #(NOT WORKING - error - bad substitution)
benchmarkEnd=${node -e "require('${nm}/benchmarkEnd.js')"} #(NOT WORKING)
start=${node -e "${benchmarkStart()}"}
#...loop the target nodejs script file (WORKS)
end=${node -e "${benchmarkEnd(start)}"}
#...another module loading & execution to show perf comparison in terminal and show line chart
답변1
잘못된 구문을 사용하고 있기 때문에 작동하지 않습니다. ${...}
뭐 하세요Bash 매개변수 확장변수 작업을 수행합니다. 예를 들어:
foo="abc"
echo "${foo^^}"
# outputs ABC instead of abc
작동하게 하려면 백틱 ` ... `로 변경하거나$(...)
따라서 다음과 같습니다.
benchmarkStart=`node -e "require('${nm}/benchmarkStart.js')"`
or
benchmarkStart=$(node -e "require('${nm}/benchmarkStart.js')")