
我有一個使用 child_process 啟動腳本的節點專案。該腳本循環遍歷文件,將它們轉換為圖像,將每個文件的路徑儲存在數組中並將數組傳回給節點。然後我轉換為字串以便讀取它並執行 console.log 以查看結果。但是我收到錯誤:
I/O Error: Couldn't open file `'/mnt/c/localBarcodereader/pdf/calBarcodereader/pdf/[email protected]': No such file or directory.
mv: cannot stat '/mnt/c/localBarcodereader/pdf/calBarcodereader/pdf/[email protected]': No such file or directory`
在 Linux 伺服器中運行時,我沒有看到此錯誤。僅當我在 Ubuntu for Windows 中執行此命令時。我注意到錯誤中正在複製目錄路徑:calBarcodereader/pdf
這是我的腳本:
#! /bin/bash
日期+%F_%T
ndate=
OIFS=$IFS; IFS=$'\n'; array=($(find /mnt/c/localBarcodereader/pdf -type f - size +0b)); IFS=$OIFS for item in "${array[@]}" do
file=$item
file="${file:9}"
fname="${file::-4}"
PATHTOIMG= "/mnt/c/localBarcodereader/pdfimage/${fname}_${ndate}"
if [ ${file: -4} == ".pdf" ]; then
# pdftoppm /mnt/c/localBarcodereader/pdf/[email protected] /mnt/c/localBarcodereader/pdfimage/test -png -f 1 -singlefile -rx 1500 -ry 1500
pdftoppm /mnt/c/localBarcodereader/pdf/$file $PATHTOIMG -png -f 1 -singlefile -rx 1500 -ry 1500
mv /mnt/c/localBarcodereader/pdf/$file /mnt/c/localBarcodereader/pdfarchive
echo $PATHTOIMG
else
mv /mnt/c/localBarcodereader/pdf/$file /mnt/c/localBarcodereader/pdfarchive
fi done
和我的節點代碼:
const cp = require('child_process')
try {
const data = cp.execSync('/mnt/c/localBarcodereader/barcodeScript.sh');
if (data.toString() !== "") {
console.log(data.toString())
答案1
當腳本在 Linux 伺服器上運行時,路徑名似乎要短得多。
作為快速修復,更改此行:
file="${file:9}"
對此:
file="${file:30}"
本質上,您正在更改變數file
:
/mnt/c/localBarcodereader/pdf/[email protected]
到:
calBarcodereader/pdf/[email protected]
當意圖改變file
為:
[email protected]
有更好的方法可以做到這一點,稍後我會嘗試修改答案(並重新格式化您的問題)。