Reputation: 39
For a reduced version of the problem, please first read Update1 below.
I've made a tcsh file to save to a file the list of all files/folders in the folder it's run (I'm working on a neuroimaging project, and I'm having the folder RawData containing a number of subjects each having a folder; I've written this file genSubjList.tcsh to save a list of subjects to the file subjList.txt). The code inside the file is:
foreach subj (*)
echo $subj >> subjList.txt
end
When I change to the RawData directory in Terminal and write this piece of code in Terminal, it generates the list of the subjects without any problem, but when I call genSubjList.tcsh using source command:
source ../scripts/genSubjList.tcsh
it just writes the name of the first folder (subject) in RawData to subjList.txt; I've already used chmode +x to make genSubjList.tcsh executable (the "scripts" folder is in the parent folder of RawData and contains genSubjList.tcsh)
The output when I run the code inside terminal is the file subjList.txt containing:
subject1 subject2 subject3 . . . subject n
The output when I run the code using source ../scripts/genSubjLits.tcsh is the file subjList.txt containig:
subject1
I would highly appreciate any help in figuring out where I'm making a mistake. Thank you!
Additional explanation about why I want to figure out what the problem is:
The problem is not just about generating a list of subjects; I want to understand why this tcsh script doesn't work because the main problem is that I'm using FreeSurfer (surfer.nmr.mgh.harvard.edu) to analyze a number of subjects; I want to analyze them in parallel so I've developed some scripts to divide up the subjects in 5 groups (stored in 5 files named subjList1.txt .. subjList5.txt, each having a number of subjects). Then, I want to run the following code in 5 Terminals in parallel:
foreach subj (`cat subjList1.txt`)
echo $subj
recon-all -i $RAW_DATA/$subj/T1/*0001.dcm -subject $subj -all
end
where subjList1.txt is substituted with subjList2, 3, etc for the parallel Terminals; the recon-all line does the analyses for each subject. Again, when I run this piece of code inside the Terminal, all subjects in each subjList are analyzed, but when I save the same piece of code in a number of script files (recon1.tcsh .. recon5.tcsh) and call them using:
source recon1.tcsh
just the first subject in each subjList is analyzed.
Update1:
Interestingly, if this simple loop:
foreach i (1 2 3 4 5)
echo $i
end
is stored in the file loop.tcsh, and is called using:
source loop.tcsh
the output will be just 1, meaning that it just goes through the first round of the loop.
The same problem does not exist in BASH; if this loop:
for i in {1..5} ; do
echo $i
done
is stored in the file loop2.bash and is called using:
source loop2.bash
it works without any problem!
Update2:
The problem does not exist in BASH; if this code:
for subj in * ; do
echo $subj >> subjList.txt
done
is stored in the file genSubjList.bash and is called using:
source ../scripts/genSubjList.bash
in bash environment, it generates the list of the subjects without any problem!
Upvotes: 3
Views: 38587
Reputation: 71
I know this is old but I came across this because I had the same problem just now and I solved it for myself.
I added a blank line after the 'end' in the tcsh foreach script and it worked fine. Before that it would only run through the first item in the list and then stop. Bash scripts worked fine for me, just like the original poster. Why would the blank line matter?
Upvotes: 7
Reputation: 1
Hmm...rather than pwd, maybe try and write the whole path to where you would like the script to start from/return to. If the issue is due to not returning to the proper directory, than a cd pwd would keep you in the same place and not solve the issue.
Upvotes: 0
Reputation: 11
Considering your output when you run this from the source command generates only the first subject in your list, I would check to see if there is a looping issue. Because you are no longer within the same directory as when you ran it from the terminal, the script may not be returning to the same homedir. I would add in a CD command as the last line of your for loop and see if that fixed the problem.
Upvotes: 0