I can not make this simple script work in bash
# Works
for f in *; do
for j in $f/Attachments/*.pdf; do
echo "$j"
done;
done
# Doesn't work
for f in *; do
for j in $f/Attachments/*.pdf; do
if [ ! pdfinfo "$j" &> /dev/null ]; then
echo "$j"
fi
done;
done
I have read 10 guides, and I cannot understand why this script lists a bunch of random directories.
It should:
- List folders in the current directory
- In each folder it should list all PDF-files in the subdirectory Attachments
- For each file it should check if it is corrupt, and if so print it
CodePudding user response:
What you want could be achieved by this code snippet:
for f in */Attachments/*.pdf; do
if ! pdfinfo "$f" &>/dev/null; then
echo "$f"
fi
done
In your code, for f in *
iterates through all files (including directories). If you want directories only, you must have used for f in */
. Like that:
for d in */; do
for f in "$d"Attachments/*.pdf; do
[[ -f $f ]] || continue
if ! pdfinfo "$f" &>/dev/null; then
echo "$f"
fi
done
done