Reputation: 553
This will no doubt be simple for someone, but I'm not a bash wizard by any means. I tend to learn as I go when I need to do something. I've got a script that optimizes images and requires only that it be called with the image folder as an argument. It will determine which files can be used and travel down each subfolder as well.
I've created a simple loop that reads lines (paths to the image folders) from a txt file and feeds them to the optimization script one by one. My problem is that some of the folder trees contain a folder and subfolder branch that I want to skip, say a cache which contains a mirror of the image folder structure. What I need is a way to easily add that folder name, say to an exclude txt file, and have the bash loop ignore that and everything under it.
Here is the loop:
#!/bin/bash
filename='imgopt_sites.txt'
while read p; do
nice -n 10 bash /usr/local/bin/imgopt.sh $p > /dev/null 2>&1
done < $filename
Here is the txt file with folders to run on:
/srv/www/domain1.com/htdocs/wp-content/uploads
/srv/www/domain2.com/htdocs/wp-content/uploads
/srv/www/domain3.com/htdocs/wp-content/uploads
/srv/www/domain4.com/htdocs/wp-content/uploads
/srv/www/domain5.com/htdocs/wp-content/uploads
/srv/www/domain6.com/htdocs/image
An example of what would be excluded would be:
/srv/www/domain6.com/htdocs/image/cache (and all under that)
This could be in another txt file and would need to be looped as there will be more. Or, if there is an easy way to put them all in the same file and distinguish the exclude lines in some way that would be fine too. I've searched and nothing I've found really hits this. Thanks.
The OS is Ubuntu 14.04 and the loop is called by a cron job.
Last, here is a sample directory tree to give perspective:
/srv/www/domain6.com/htdocs/image
├── cache
│ ├── catalog
│ │ ├── apparel
│ │ ├── art
│ │ ├── banners
│ │ ├── commission
│ │ ├── demo
│ │ │ ├── banners
│ │ │ └── manufacturer
│ │ ├── illustrations
│ │ ├── jewelry
│ │ ├── misc
│ │ ├── news
│ │ ├── pen_ink
│ │ └── sample_data
│ │ ├── gallery
│ │ ├── logos
│ │ ├── patterns
│ │ ├── payments
│ │ └── slider
│ └── tb
├── catalog
│ ├── apparel
│ ├── art
│ ├── banners
│ ├── commission
│ ├── illustrations
│ ├── jewelry
│ ├── misc
│ ├── news
│ ├── pen_ink
│ └── sample_data
│ ├── banners
│ ├── features
│ ├── gallery
│ ├── logos
│ ├── patterns
│ │ └── _original
│ ├── payments
│ ├── slider
│ └── wallpaper
├── data
│ └── productimage
├── flags
└── templates
Any suggestions are appreciated.
Update: This is the section of code in the script itself that handles the input directories.
do_dir () {
# $1 is name of dir
local ret_val="0"
while IFS= read -r -u 3 -d $'\0' file; do
do_file "${file}"
ret_val=$?
done 3< <(find "${1}" -type f \( -iregex '.*\.[jp][pn]g$' -o -iregex '.*\.jpeg$' \) -print0)
return $ret_val
}
I'm a little confused over what the "-u 3" does.
Update 2: New version of loop with errors in response to @shellter
#!/bin/bash
filename='imgopt_sites.txt'
excludes='imgopt_excludes.txt'
set -vx; while read p ; do nice -n 10 bash /usr/local/bin/imgopt/imgopt-ng.sh -new -progressive -sqlite $p; done <( grep -v -F -f "$excludes" "$filename" )
Error:
/usr/local/bin/imgopt/imgopt_loop.sh: line 6: syntax error near unexpected token `<( grep -v -F -f "$excludes" "$filename" )'
/usr/local/bin/imgopt/imgopt_loop.sh: line 6: `set -vx; while read p ; do nice -n 10 bash /usr/local/bin/imgopt/imgopt-ng.sh -new -progressive -sqlite $p; done <( grep -v -F -f "$excludes" "$filename" )'
Upvotes: 1
Views: 376