LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   If inside a for loop (https://www.linuxquestions.org/questions/linux-newbie-8/if-inside-a-for-loop-4175509735/)

jonnybinthemix 07-01-2014 04:52 AM

If inside a for loop
 
Hey Guys,

Quick one... been researching this but to no avail.

If I have a For Loop that looks something like:

Code:

for i in $XXX; do
command
done

If I want to put an if statement in there to say, if file exists move to the next file.. how would I go about it?

I've tried something like:

Code:

for i in $XXX; do
if [ -f $i ]; then next
else
command
fi
done

However it does not like the next, I've also tried skip...

Am I close?

Guttorm 07-01-2014 04:57 AM

Hi

You could use "continue" instead of "next". Or simply use ! for "not":

Code:

for i in $XXX; do
  if [ ! -f "$i" ]; then
    command
  fi
done

Also, if you put quotes around $i, it will handle filenames with spaces.

grail 07-01-2014 10:32 AM

You could also try:
Code:

[[ -f "$i" ]] && continue


All times are GMT -5. The time now is 04:00 AM.