1

几天前,我启动了一个小 bash 脚本,它应该总结文件夹中所有 PDF 的页数和文件大小。它现在工作得很好,但还有一件事我不明白。

shopt -s nullglob如果设置,为什么 sed 总是失败?有人知道为什么会这样吗?

我在 Ubuntu 14.04 中使用 GNU Bash 4.3 和 sed 4.2.2。

set -u
set -e

folder=$1

overallfilesize=0
overallpages=0
numberoffiles=0

#If glob fails nothing should be returned
shopt -s nullglob

for file in $folder/*.pdf
do

  # Disable empty string if glob fails
  # (Necessary because otherwise sed fails ?:|)
  #shopt -u nullglob

  # This command is allowed to fail
  set +e
  pdfinfo="$(pdfinfo "$file" 2> /dev/null)"
  ret=$? 
  set -e  

  if [[ $ret -eq 0 ]]
  then 
    #Remove every non digit in the result
    sedstring='s/[^0-9]//g'
    filesize=$(echo -e "$pdfinfo" | grep -m 1 "File size:" | sed $sedstring)
    pages=$(echo -e "$pdfinfo" | grep -m 1 "Pages:" | sed $sedstring)

    overallfilesize=$(($overallfilesize + $filesize))  
    overallpages=$(($overallpages+$pages))  
    numberoffiles=$(($numberoffiles+1))  
  fi

done

echo -e "Processed files: $numberoffiles"
echo -e "Pagesum: $overallpages"
echo -e "Filesizesum [Bytes]: $overallfilesize"
4

1 回答 1

3

这是重现问题的更简单的测试用例:

#!/bin/bash
shopt -s nullglob
pattern='s/[^0-9]//g'
sed $pattern <<< foo42

预期输出:

42

实际输出:

Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
(sed usage follows)

发生这种情况是因为s/[^0-9]//g它是一个有效的 glob(匹配类似 like 的 dir 结构s/c/g),并且您要求 bash 解释它。由于您没有匹配的文件,nullglob因此完全启动并删除该模式。

双引号可防止分词和全局解释,这几乎总是您想要的:

#!/bin/bash
shopt -s nullglob
pattern='s/[^0-9]//g'
sed "$pattern" <<< foo42

这会产生预期的输出。

你应该总是双引号你所有的变量引用,除非你有特定的理由不这样做。

于 2015-03-19T19:46:42.350 回答