LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A for loop in an if statement tcsh (https://www.linuxquestions.org/questions/programming-9/a-for-loop-in-an-if-statement-tcsh-897904/)

leavage 08-17-2011 02:11 PM

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

kostya 08-17-2011 03:00 PM

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)

leavage 08-17-2011 11:32 PM

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.


All times are GMT -5. The time now is 09:17 AM.