【问题标题】:bash shell script command substitution problem - escape basefile name spaces - multiple pdf files to jpeg conversion using ghostscriptbash shell 脚本命令替换问题 - 转义基本文件名称空间 - 使用 ghostscript 将多个 pdf 文件转换为 jpeg
【发布时间】:2022-01-08 11:26:05
【问题描述】:

这个 bash shell 脚本 使用 zenity 接受多个 .pdf 文件输入并存储在一个数组中,以便将 ghostscript .pdf 转换为 .jpeg。

问题

  1. 需要将文件路径存储在带有转义空格的数组中才能进入 gs 命令 $i
  2. 在 for 循环内的 gs 命令中需要输出文件名的基本文件名
  3. gs 命令需要带空格的文件名。
  4. 无法运行gs command error command not found on line 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

【问题讨论】:

  • 对于 Ghostscript,将文件名放在引号 "" 中,因为它包含一个空格。不知道 baseFileName: command not found 是什么。
  • 通过shellcheck.net 运行它,并修复它指出的引用和间距问题。
  • baseFileName = $(basename '$i') 必须是 baseFileName=$(basename "$i")。也就是说,=$i 周围没有空格,用双引号(不是单引号)括起来。 $igs 命令中也必须用双引号引起来。
  • 显示zenity --file-selection --multiple --filename "${HOME}/"的输出。

标签: linux bash pdf jpeg


【解决方案1】:

正如其他人所评论的,您需要双引号变量,尤其是在 变量中的文件名包含空格。

请您尝试一下:

#!/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

【讨论】:

    【解决方案2】:

    这是你的脚本的固定版本,仔细检查我修复了多个错误:

    我建议您使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 2011-05-16
      • 2015-02-28
      • 2021-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多