我在设置复制和转换文件的脚本时遇到了麻烦。我基本上想要一个脚本,它接受一个主题,例如
folder/subjects/subject_name/mri/norm.mgz
将此文件复制到
folder/investigation_folder/fs_norms/subject_name/
并将 转换norm.mgz
为norm.nii
使用 mri_convert
,并对 +300 个主题(在主题文件夹中)执行此操作。
这应该相当简单...有人可以帮我吗?
我是个新手!这是我想做的一个例子 - 只有一个主题:
mkdir /mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/
cp /mnt/projects/Project_folder/subjects/gXXX/mri/norm.mgz /mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/
mri_convert mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/norm.mgz mnt/projects/Project_folder/thalamic_subnuclei/fs_norms/gXXX/norm.nii
我的所有主题都以 g 开头,后面跟着 3-4 个数字。现在我需要设置一个循环,对/mnt/projects/Project_folder/subjects/
文件夹中的 +300 个主题执行此操作。
答案1
下面是在 Bash 脚本中应用 FOR 循环来解决此任务的示例:
#!/bin/bash
# Execute do-done inner part for each "item" under "folder/subjects/"
for subject in folder/subjects/*
do
# Check whether the source file exists
if [[ -f ${subject}/mri/norm.mgz ]]
then
# ${subject##*/} will cut the parent path from the value of the variable '$subject'
echo "Processing: ${subject##*/}"
# Create the new subject directory
mkdir -p "folder/investigation_folder/fs_norms/${subject##*/}"
# Copy the .mgz file into the new directory
cp "${subject}/mri/norm.mgz" "folder/investigation_folder/fs_norms/${subject##*/}/"
# Do the conversion to .nii (I'm not sure this is the correct syntax of mri_convert that you are using)
mri_convert "folder/investigation_folder/fs_norms/${subject##*/}/norm.mgz" "folder/investigation_folder/fs_norms/${subject##*/}/norm.nii"
fi
done