shell 是否可以使用存储在数组中的关键字运行pdfgrep?[关闭]

bq9c1y66  于 11个月前  发布在  Shell
关注(0)|答案(1)|浏览(128)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
18天前关门了。
Improve this question
我想在PDF文件中搜索一些不同的词;使用pdfgrep https://pdfgrep.org/doc.html我可以做到这一点,但我不能运行很多次,一个为数组中存在的每个词。
然后我想保存在一个不同的单个文件的每一个输出。

#!/bin/bash

function search_elements(){
   array=("$@")
   for i in "${array[@]}";
       do
              pdfgrep -Hir ' "$i" ' . 
       done
}

array = ("gomma" "lacca" "bromo")

search_elements "$(array[@])"

字符串
这是ShellCheck的输出:

for i in "${array[@]}";
^-- SC2034 (warning): i appears unused. Verify use (or export if used externally).
pdfgrep -Hir ' "$i" ' .
                         ^-- SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.
array = ("gomma" "lacca" "bromo")
      ^-- SC2283 (error): Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).
      ^-- SC1036 (error): '(' is invalid here. Did you forget to escape it?
      ^-- SC1088 (error): Parsing stopped here. Invalid use of parentheses?
vptzau2j

vptzau2j1#

这里是语法错误修复.

#!/bin/bash

# "function" keyword is unnecessary and ugly
search_elements(){
   # no need for an array
   for i; do
        # fix quoting and crazy indentation; add redirect
        pdfgrep -Hir "$i" . >"$i.txt"
   done
}

# remove spurious spaces
array=("gomma" "lacca" "bromo")

# braces, not parentheses
search_elements "${array[@]}"

字符串
将逻辑放在函数中似乎有点像过度工程;这很简单,只需在脚本中使用您想要扫描的关键字调用。

#!/bin/sh
for i; do
    pdfgrep -Hir "$i" . >"$i.txt"
done


这里没有使用Bash语法; /bin/sh在许多平台上更可移植,速度也更快。
shellcheck警告是可点击的链接,其中包含有关每个错误的更多信息;请在请求人工帮助之前阅读它告诉您的内容。

相关问题