Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm processing a batch of subjects data recursively, calling the script within the parent directory.

For example, I have the parent directory:

/home/subjects

and the subdirectories which contain the data:

/home/subjects/0393
/home/subjects/0389
/home/subjects/9920 (Around 250 subjects)

Each file in each subdirectory has the file extension ".nii". I have composed a code, which calls a number of commands from a neuroscience program, searching for that particular file extension as the input. The input to the first command "fslroi" is the .nii file ($file), while the output file of that command is "rawdata.nii". As you can see the output from one command, is the input to the next etc

for file in $(find ./ -name "*.nii")
do
fslroi $file rawdata.nii 0 33
gunzip rawdata.nii.gz -f
fslroi rawdata.nii rawnodif 0 1
bet rawnodif rawnodif_brain -m -g 0.2 -f 0.3
fslmaths rawnodif -mas rawnodif_brain_mask rawnodif_brain
gunzip rawnodif_brain_mask.nii.gz -f
done

However, this code is inadequate as I am unable to save the output into the particular subdirectory. Thus, as I am processing several subjects the code will not work as everything is being saved to the parent directory.

Could someone please be able to give me any hints to how I can modify the code to save the output according to the input ".nii" file?

share|improve this question
    
You should mark your own answer as the accepted answer –  Ana Apr 25 '13 at 11:14

2 Answers 2

Thank you, here is the final script. Note the $ denotes a filename, whilst the rest are commands or arguments.

#!/bin/bash

for file in $(find ./ -name "*.nii")
do
rawdata=$(dirname $file)/rawdata.nii.gz
rawnodif=$(dirname $file)/rawnodif.nii.gz
rawnodif_brain=$(dirname $file)/rawnodif_brain.nii.gz
rawnodif_brain_mask=$(dirname $file)/rawnodif_brain.nii.gz
fslroi $file $rawdata 0 33
fslroi $rawdata $rawnodif 0 1
gunzip $rawdata -f
bet $rawnodif $rawnodif_brain -m -g 0.2 -f 0.3
fslmaths $rawnodif -mas $rawnodif_brain_mask $rawnodif_brain
gunzip $rawnodif_brain_mask -f
done
share|improve this answer

You want to use dirname $file to get the directory name of your input file and prepend that to the output filename.

for file in $(find ./ -name "*.nii")
do
rawdata = $(dirname $file)/rawdata.nii
fslroi $file $rawdata 0 33
gunzip $rawdata.gz -f
fslroi $rawdata rawnodif 0 1
bet rawnodif rawnodif_brain -m -g 0.2 -f 0.3
fslmaths rawnodif -mas rawnodif_brain_mask rawnodif_brain
gunzip rawnodif_brain_mask.nii.gz -f
done

It is unclear which of the rawdata are actual command arguments or if all are filenames. Same with rawnodif_brain_mask which you might need to adapt in the same way.

share|improve this answer
    
Thankyou @Anthon it has worked. The final script is below. –  ap123 Apr 25 '13 at 1:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.