LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2014, 04:52 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
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?
 
Old 07-01-2014, 04:57 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,267

Rep: Reputation: 298Reputation: 298Reputation: 298
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.
 
1 members found this post helpful.
Old 07-01-2014, 10:32 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
You could also try:
Code:
[[ -f "$i" ]] && continue
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
Q: count inside a loop Dr_Death_UAE Linux - General 6 03-04-2013 12:24 AM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM


All times are GMT -5. The time now is 02:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration