ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I've got a script processing a timelapse stills that I'm again trying to improve and ran into a question I can't figure out with google. How do I modify this IFS loop so I list only graphic files, say case insensitive extensions of jpg, gif, png, and maybe bmp? Most of the time I'm sure i'll be dealing with Jpegs only, but I'd rather not limit it?.
Code:
IFS='
' files=( $(/bin/ls -1 --ignore=resized_to_*) )
for (( i = 0; i < ${#files[*]}; i++ ))
do
file="${files[$i]}"
<commands>
done
What do you actually want to do? Seems really quite odd what you're already doing, why are you using a numerical for loop when you can just loop through the array elements anyway? Quite probably easiest to just pass the ls output direct to xargs or a while loop, really.
find . -iname *.jpg -o -iname *.bmp ... -print | while read FILENAME
do
<stuff>
done
I'm using a numerical loop so I can display the progress via Zenity. Eventually I'd like to bring the whole process into a gui interface that is easier for others to use, but this is an incremental process and I keep running into errors on the processing when imagemagick tries to work on thumbnail.db, or a sub-directory, or etc.
Thanks osor, the magic version is exactly what I'm looking for however, I seems to be missing something. When I put either version of the if-then-if grep test into the loop it doesn't return any values. (Below is the bash -x result for the intial part of the loop using the extension grep.)
As I understand it grep is -q is quiet, -i ignores case, and -E enables RegEx. I tried this
Code:
ls -1 | grep -qiE '\.(jpg|png|gif|svg|bmp)$'
on a directory full of my timelapse images but it returns nothing. Omiting -q option it returns the files, but not being well refined in scripting techniques I'm not sure what this is doing or how it's failing. Any ideas what I'm missing?
Thanks osor, the magic version is exactly what I'm looking for however, I seems to be missing something. When I put either version of the if-then-if grep test into the loop it doesn't return any values. (Below is the bash -x result for the intial part of the loop using the extension grep.)
As acid_kewpie has stated, your script is written in a strange manner. You have given us the old script, but not the changes you made. I assume you have replaced this construct:
Code:
IFS='
' files=( $(/bin/ls -1 --ignore=resized_to_*) )
(for (( i = 0; i < ${#files[*]}; i++ ))
do
file="${files[$i]}"
let "compteur += 1"
…
with this
Code:
( IFS='
' for file in $(/bin/ls -1 --ignore=resized_to_*)
do
if ! echo "$file" | grep -qiE '\.(jpg|png|gif|svg|bmp)$'
then
continue
fi
let "compteur += 1"
…
You may (as you have probably guessed) have instead done this (but not with magic):
Code:
( IFS='
' for file in $(/bin/ls -1 --ignore=resized_to_* | grep -iE '\.(jpg|png|gif|svg|bmp)$')
do
let "compteur += 1"
…
or even this:
Code:
( ls -1 --ignore=resized_to_* | grep -iE '\.(jpg|png|gif|svg|bmp)$' | while read file
do
let "compteur += 1"
…
Quote:
Originally Posted by wonderfullyrich
As I understand it grep is -q is quiet, -i ignores case, and -E enables RegEx. I tried this
Code:
ls -1 | grep -qiE '\.(jpg|png|gif|svg|bmp)$'
on a directory full of my timelapse images but it returns nothing. Omiting -q option it returns the files, but not being well refined in scripting techniques I'm not sure what this is doing or how it's failing. Any ideas what I'm missing?
Yes there is. The point of the “grep -q” is not to print the files (although that would be one way to do it—see above) but to return a truth value which will be tested. For example:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.