Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It is recommended to use 'stat' rather than 'ls' for getting filenames in scripts.
I must be oblivious to something because I could not get it to work. Thus, I have recorded a bit of my process. Perhaps someone here can figure out what I'm doing wrong. The bit towards the end (around 3:50 min mark) is me doing the process using the program GUI instead of the script. But ultimately, I want to use a script to make the process faster/easier.
The script in the video does not seem to match what has been suggested. (I have attached a still from the video).
I was hoping that you would finish the script from the snippets provided.
Code:
#!/bin/bash
# Top of directory structure containing files to be processed.
topdir="/home/natasha/Documents/DWI"
cd "$topdir"
# Set shopt to also look in subdirectories
shopt -s globstar
for f in **/*_eddy_corrected_brain.nii.gz; do
# Change to subdirectory
cd "${f%/*}"
# Set variables for program
# TODO - Add error checking for missing or duplicate files
data="$(stat -c %n *_eddy_corrected_brain.nii.gz)"
mask="$(stat -c %n *_eddy_corrected_brain_mask.nii.gz)"
bvecs="$(stat -c %n *.bvec)"
bvals="$(stat -c %n *.bval)"
# Build the output file name
output="${f##/}"
output="${output/_eddy_corrected_brain.nii.gz/}_dtifit"
# Run the program
dtifit -k "$data" -m "$mask" -r "$bvecs" -b "$bvals" -o "$output"
# Change back to top directory
cd "$topdir"
done
# Undo change to shopt
shopt -u globstar
The script in the video does not seem to match what has been suggested. (I have attached a still from the video).
I was hoping that you would finish the script from the snippets provided.
Code:
#!/bin/bash
# Top of directory structure containing files to be processed.
topdir="/home/natasha/Documents/DWI"
cd "$topdir"
# Set shopt to also look in subdirectories
shopt -s globstar
for f in **/*_eddy_corrected_brain.nii.gz; do
# Change to subdirectory
cd "${f%/*}"
# Set variables for program
# TODO - Add error checking for missing or duplicate files
data="$(stat -c %n *_eddy_corrected_brain.nii.gz)"
mask="$(stat -c %n *_eddy_corrected_brain_mask.nii.gz)"
bvecs="$(stat -c %n *.bvec)"
bvals="$(stat -c %n *.bval)"
# Build the output file name
output="${f##/}"
output="${output/_eddy_corrected_brain.nii.gz/}_dtifit"
# Run the program
dtifit -k "$data" -m "$mask" -r "$bvecs" -b "$bvals" -o "$output"
# Change back to top directory
cd "$topdir"
done
# Undo change to shopt
shopt -u globstar
Hi allend,
I ran the modified script you so kindly posted and it seemed to have started working but I get the following error at the end. I'm not sure if it's something in the script or the actual files themselves. If you could be so kind as to again take a look and help with figure this out, I would really be grateful.
When you were doing it manually? Do you need to extract ".gz" before you processed? Or what?
No, when I did it manually, I didn't extract the .gz.
I think it might be something with how the output file is named. Is there anyway to write the code so that it names the output as something along the lines of 'subfolder name'_dti?
This is the code Iin terminnal when I successfully run it manually
From what I can tell, the error has something to do with how the output file is saved/named.
Code:
Error: failed to open file 111440-100/3t_2016-01-27_20-58/003_DTI_siemens_TClessdistort/111440-100_eddy_corrected.nii.gz/_dtifit_FA.nii.gz
Image Exception : #22 :: ERROR: Could not open image 111440-100/3t_2016-01-27_20-58/003_DTI_siemens_TClessdistort/111440-100_eddy_corrected.nii.gz/_dtifit_FA
terminate called after throwing an instance of 'RBD_COMMON::BaseException'
./scriptb.sh: line 11: 6377 Aborted (core dumped) dtifit -k "$data" -o "$output" -m "$mask" -r "$bvecs" -b "$bvals"
instead it has to omit the bolded part and to save/name it like so
try changing this line: output="${output/_eddy_corrected_brain.nii.gz/}_dtifit"
change to: output="${output/dti}_dtifit"
Okay, so it's most probable that it's the bit of code that deals with the output name.
I modified it a bit to test my hypothesis and it sort of worked. Now I just have to figure out the correct syntax to put in so that I get the file name as the program requires it.
As you will see in the screenshot, I've experimented so that I got the output to be saved/named as "0_FA.nii.gz' etc. Now the goal is to change the code accordingly so that the output gets saved/named as "dti_fa.nii.gz" instead of the 0 in front. It feels like we're so close to getting this to work, I just want to be able to finally get it right.
The script is successful and I very much appreciate everyone's assistance and suggestions. If it's not too much to ask, I would like to understand the script line by line so that I can do this on my own in the future. I would like to get a good grasp of the basic foundation of writing scripts that, as this one, go into subfolders, look for files, and carry out commands but my knowledge is still not at a level where I can do this on my own. I was hoping that better understanding what each line does in the script allend provided would help me write simple scripts in the future. If anyone can write the explanations, please?
Code:
#!/bin/bash
# Top of directory structure containing files to be processed.
topdir="/home/natasha/Documents/DWI"
cd "$topdir"
# Set shopt to also look in subdirectories
shopt -s globstar
for f in **/*_eddy_corrected_brain.nii.gz; do
# Change to subdirectory
cd "${f%/*}"
# Set variables for program
# TODO - Add error checking for missing or duplicate files
data="$(stat -c %n *_eddy_corrected_brain.nii.gz)"
mask="$(stat -c %n *_eddy_corrected_brain_mask.nii.gz)"
bvecs="$(stat -c %n *.bvec)"
bvals="$(stat -c %n *.bval)"
# Build the output file name
output="${f##/}"
output="${output/_eddy_corrected_brain.nii.gz/}_dtifit"
# Run the program
dtifit -k "$data" -m "$mask" -r "$bvecs" -b "$bvals" -o "$output"
# Change back to top directory
cd "$topdir"
done
# Undo change to shopt
shopt -u globstar
Well, most of it is buried in the "bash" manual page as shopt, for, and cd are built-in commands. I'd recommend following everything up with a browser through it to learn it as a reference source. No one can remember it all, or even most of it, but you can remember where you last saw something and be able to look it up as needed. "just in time" rather than "just in case". If you use features often enough, they will sink in.
"shopt -s globstar" is where the important change is. That allows ** to mean a recursive descent into directories and subdirectories. It is about the equivalent of "find" offered in #5 above.
The ${f%/*} trims the variable $f using substring removal matching from the end.
The "stat -c %n" prints just the file name and using the $( ... ) for command substitution the resulting output from the program "stat" is treated as if it were a string, allowing it to be assigned to a variable.
The ${f##/} is more substring removal but matching from the beginning. (Note that the slash is being matched it is not an operator.)
The second output=" line is just just a search and removal of '_eddy_corrected_brain.nii.gz/' and appending '_dtifit'
So, yes, "bash" is a scripting language.
Edit: JJJCR was more concise and posted the relevant links while I was slowly drafting the above.
Last edited by Turbocapitalist; 06-14-2016 at 09:45 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.