這個 bash shell 腳本使用 zenity 接受多個 .pdf 檔案輸入,并存盤在一個陣列中,以便將 ghostscript .pdf 轉換為 .jpeg。
問題
- 需要將檔案路徑存盤在帶有轉義空格的陣列中才能進入 gs 命令 $i
- 在for回圈內的gs命令中需要輸出檔案名的基本檔案名
- gs 命令需要帶有空格的檔案名。
- 無法運行 gs command error command not found 在第 20 行。
代碼:
#get list of selected files from Graphical Dialog
listOfFilesSelected=$(zenity --file-selection --multiple --filename "${HOME}/")
#echo $listOfFilesSelected
# Here pipe is our delimiter value
IFS="|" read -a listFiles <<< $listOfFilesSelected
#echo "File: ${listFiles[@]}"
# get length of an array
#arraylength=${#listFiles[@]}
#echo "${listFiles[0]}"
##echo $'\n'
#echo "Number of elements in the array: ${#listFiles[@]}"
for i in "${listFiles[@]}"
do
echo $i
baseFileName = $(basename '$i')
echo $baseFileName
gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=output%d.jpg -dJPEGQ=100 -r600 -q $i -c quit
done
輸出:錯誤
/home/q/Downloads/FinalAnsKey22COMPUTER SCIENCE.pdf
./zentest.sh: line 20: baseFileName: command not found
Error: /undefinedfilename in (/home/q/Downloads/FinalAnsKey22COMPUTER)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:732/1123(ro)(G)-- --dict:0/20(G)-- --dict:75/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.50: Unrecoverable error, exit code 1
uj5u.com熱心網友回復:
正如其他人所評論的那樣,您需要雙引號變數,特別是當變數中的檔案名包含空格時。
請您嘗試一下:
#!/bin/bash
listOfFilesSelected=$(zenity --file-selection --multiple --filename "${HOME}/")
IFS="|" read -ra listFiles <<< "$listOfFilesSelected"
for i in "${listFiles[@]}"; do
echo "$i"
baseFileName=$(basename "$i")
echo "$baseFileName"
gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile="$baseFileName"%d.jpg -dJPEGQ=100 -r600 -q "$i" -c quit
done
uj5u.com熱心網友回復:
這是您腳本的固定版本,請仔細檢查我修復了多個錯誤:
我建議您使用https://shellcheck.net/檢查您的腳本
它是對 shell 腳本的靜態分析,將極大地幫助您發現錯誤,并且通常會列出一些修復它的選項。
#!/usr/bin/env bash
#get list of selected files from Graphical Dialog
mapfile -t listFiles < <(
zenity --separator=$'\n' --file-selection --multiple --filename="$HOME/"
)
for filePath in "${listFiles[@]}"; do
printf '%s\n' "$filePath"
baseFileName=${filePath##*/}
printf '%s\n' "$baseFileName"
# remove the echo if it does what you want
echo gs -dNOPAUSE -sDEVICE=jpeg -sOutputFile=output%d.jpg -dJPEGQ=100 -r600 -q "$filePath" -c quit
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407162.html
標籤:
