LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2011, 02:11 PM   #1
leavage
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Rep: Reputation: 0
A for loop in an if statement tcsh


I have a program that runs the following commmands

if (-e ${CALIBPLOTS}/y*.eps) then

foreach file ($CALIBPLOTS/y*.eps)
set jpgfile = `echo $file | sed s/\.eps//g`
convert $file ${jpgfile}.jpg
end

endif


The problem I have is taht with that foreach loop, if no files exist the program outputs that no file exists and stops running. So I wanted to put that if statement there so taht it only runs if there is a file. But for some reason if there isn't a file it still runs the foreach loop and stops running.

Everything works fine if there is a file there.
Any ideas how I can get this to run properly
 
Old 08-17-2011, 03:00 PM   #2
kostya
Member
 
Registered: Mar 2010
Location: Moscow, Russia
Distribution: Ubuntu Studio, antix(mepis), Fedora, FreeBSD
Posts: 174
Blog Entries: 5

Rep: Reputation: 18
Here is a reference.
And why do people use tcsh and not bash? I find much more help with bash.

for example, in bash the name substitution will be shorter and more effective:
Quote:
convert $file ${file%.eps}.jpg
The form "${file%.eps}" cuts the shortest match of ".eps" from the end of the variable name $file (and symbol %% will cut the longest match of the expression that follows). On the other hand, "${file#.eps}" would do the same from the beginning.
Isn't it cool?

Sorry that I can't help about tcsh.
So "${CALIBPLOTS}/y*.eps" is expanded as any "y*.eps" file in the "$CALIBPLOTS" directory, right?
In that case, it seems, in the expression "foreach file" it must be present in the same form in order to prevent the "/.eps" part from being added to the variable name?
Quote:
foreach file (${CALIBPLOTS}/y*.eps)

Last edited by kostya; 08-17-2011 at 04:09 PM.
 
Old 08-17-2011, 11:32 PM   #3
leavage
LQ Newbie
 
Registered: Dec 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Well I am using tcsh because I am editing a program that was originally written in tcsh hah. But I added {} to the variable name and it still doesn't work. I get an error telling me that there is no y*.eps.
 
  


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
Need help with for loop + if statement Thaidog Programming 5 05-06-2011 09:54 PM
Defining loop statement for creating new file each time neo009 Linux - Newbie 6 04-12-2009 06:49 AM
can't put an if statement as an increment in a for loop. Why? japhy Programming 2 02-07-2009 06:07 AM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 04:45 PM
BASH Need help figuring out how to loop SELECT statement mcdrr Programming 2 05-08-2007 04:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 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
Open Source Consulting | Domain Registration