Issue
I want search in PDF files some different words; using pdfgrep https://pdfgrep.org/doc.html I can do it, but I'm not able to run many times, one for each word present in the array.
Then I want save every output in a different single file.
#!/bin/bash
function search_elements(){
array=("$@")
for i in "${array[@]}";
do
pdfgrep -Hir ' "$i" ' .
done
}
array = ("gomma" "lacca" "bromo")
search_elements "$(array[@])"
This is the output from ShellCheck:
- Line 5: SC2034
for i in "${array[@]}"; ^-- SC2034 (warning): i appears unused. Verify use (or export if used externally).
- Line 7: SC2016
pdfgrep -Hir ' "$i" ' . ^-- SC2016 (info): Expressions don't expand in single quotes, use double quotes for that.
- Line 11: SC2283 SC1036 SC1088
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?
Solution
Sigh. Here are the syntax errors fixed.
#!/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[@]}"
Putting the logic in a function seems somewhat like over-engineering; this is simple enough to just live in a script which you call with the keywords you want to scan for.
#!/bin/sh
for i; do
pdfgrep -Hir "$i" . >"$i.txt"
done
Nothing here uses Bash syntax; /bin/sh
is more portable as well as faster on many platforms.
The shellcheck warnings are clickable links with more information about each error; please read what it tells you before you ask a human to help you.
Answered By - tripleee Answer Checked By - Timothy Miller (WPSolving Admin)