PDA

View Full Version : for / foreach loop in tcsh?



aytacd
September 23rd, 2009, 12:07 PM
Hello to all,

I have a for loop for accesing files in a folder, convert them into another format (.mgz) and save them into another folder.

So here are the algorithmic steps:

Read a list of files in a folder.
Convert file extensions with (.mgz)
save files into a different folder.


here is the code that i used in bash script:

#!/bin/bash

cd /usr/local/freesurfer/subjects/$1/data_temp

temp=1;

for i in $(ls); do
mri_convert $i 00$temp.mgz
let temp=temp+1;
done



i have to write this in tcsh after hours of googling what i have found is the insufficient documentation about foreach command:



#!/bin/bash

cd /usr/local/freesurfer/subjects/$1/data_temp

set count=1

foreach i ( ` /usr/local/freesurfer/subjects/$1/data_temp * ` )

foreach? echo $i

foreach? do mri_convert $i /usr/local/freesurfer/subjects/$1/mri/orig/00$count.mgz

foreach? let count=count+1

foreach? done




final errors that we are taking are:

/usr/local/freesurfer/subjects/rhsr/data_temp: Permission denied.
foreach: end not found.

any help??

Arndt
September 23rd, 2009, 12:57 PM
Hello to all,

I have a for loop for accesing files in a folder, convert them into another format (.mgz) and save them into another folder.

So here are the algorithmic steps:

Read a list of files in a folder.
Convert file extensions with (.mgz)
save files into a different folder.


here is the code that i used in bash script:

#!/bin/bash

cd /usr/local/freesurfer/subjects/$1/data_temp

temp=1;

for i in $(ls); do
mri_convert $i 00$temp.mgz
let temp=temp+1;
done



i have to write this in tcsh after hours of googling what i have found is the insufficient documentation about foreach command:



#!/bin/bash

cd /usr/local/freesurfer/subjects/$1/data_temp

set count=1

foreach i ( ` /usr/local/freesurfer/subjects/$1/data_temp * ` )

foreach? echo $i

foreach? do mri_convert $i /usr/local/freesurfer/subjects/$1/mri/orig/00$count.mgz

foreach? let count=count+1

foreach? done




final errors that we are taking are:

/usr/local/freesurfer/subjects/rhsr/data_temp: Permission denied.
foreach: end not found.

any help??

After seconds of googling, I found the man page for tcsh, and the section about 'foreach' leads me to believe tcsh means it when it says "foreach: end not found".

aytacd
September 24th, 2009, 11:30 AM
After seconds of googling, I found the man page for tcsh, and the section about 'foreach' leads me to believe tcsh means it when it says "foreach: end not found".


meaning?

i need a solution and i cannot find anything sufficient?
Can you guide me to somewhere a link or a command line?

Arndt
September 24th, 2009, 02:09 PM
meaning?

i need a solution and i cannot find anything sufficient?
Can you guide me to somewhere a link or a command line?

I don't understand how you could google for hours without encountering the documentation for tcsh. Google again, for "documentation tcsh". You will find what is called a "manual page". In it, look for "foreach".

aytacd
September 25th, 2009, 08:59 AM
I don't understand how you could google for hours without encountering the documentation for tcsh. Google again, for "documentation tcsh". You will find what is called a "manual page". In it, look for "foreach".

i tried several ways to make this algorithm work. But couldn't find a working solution, including your "manual page". So i'm trying to use this forum for help. But all you doing is: critisim. I don't need that, i need a direction for my algorithm. If you're not helping i prefer not to write anything at all..

Arndt
September 25th, 2009, 09:06 AM
i tried several ways to make this algorithm work. But couldn't find a working solution, including your "manual page". So i'm trying to use this forum for help. But all you doing is: critisim. I don't need that, i need a direction for my algorithm. If you're not helping i prefer not to write anything at all..

Yes, it's criticism. It's also telling you what to do.

1) Did you do what I suggested? 2) Did you find the manual page? 3) Did you find the section about 'foreach'? 4) Did you compare what it says with what you have written?

At which step did you fail? That's where we can begin helping you.

aytacd
September 25th, 2009, 12:24 PM
if you want to help you could easily copy the link to this thread but you still prefer not to.
I solve my problems finally.
Can somebody lock this Thread!


Yes, it's criticism. It's also telling you what to do.

1) Did you do what I suggested? 2) Did you find the manual page? 3) Did you find the section about 'foreach'? 4) Did you compare what it says with what you have written?

At which step did you fail? That's where we can begin helping you.

Arndt
September 25th, 2009, 12:43 PM
if you want to help you could easily copy the link to this thread but you still prefer not to.
I solve my problems finally.
Can somebody lock this Thread!

So where did you find the information, if not where I pointed? That may be useful to know for others in the future.

aytacd
October 7th, 2009, 09:55 AM
go and ask someone personally!



So where did you find the information, if not where I pointed? That may be useful to know for others in the future.

cebi000
November 11th, 2009, 05:52 PM
You use
#!/bin/bash
in your tcsh code, so maybe you accidentally try to run it with bash?

(Hello Ubuntu community! 1st post here :) )

aregjan
March 26th, 2010, 09:09 PM
aytacd, I believe Arndt's point is this -- if you are gonna try Linux, you might as well learn its main principle: rtfm (google that:) ).

superarthur
March 26th, 2010, 11:44 PM
I thought the Ubuntu community gave people an impression that is friendlier than other Linux community and won't ask people to rtfm. Maybe I am wrong.

Ratzlaff
April 13th, 2010, 04:43 PM
Since this rather useless thread is one of the top google hits for 'tcsh loops' I would direct people to this page: http://www.csem.duke.edu/Cluster/csh_basics.htm that helped me solve my issue.