我目前正在尝试使用以下 linux 脚本循环访问文件夹并使用函数执行计算:
for f in s*
do
echo "You are in the following folder - $s"
cd $s
# FUNCTION SHOULD BE HERE
cd /C/Users/Eric/Desktop/Files
done
问题:如何使用文件夹名找到正确的文件?例如,文件夹名称是 scan1
,我想使用名为 gaf_scan1_recording_mic.nii
的文件来实现该功能。
非常感谢,
埃里克
最佳答案
在大多数情况下,$var
和 ${var}
是相同的(大括号仅用于表达式中的歧义) :
var=test
echo $var
# test
echo ${var}
# test
echo $varworks
# prints nothing (there is no variable 'varworks')
echo ${var}works
# testworks
您可以使用这样的文件夹名称 (gaf_${f}_recording_mic.nii
):
for f in *; do
# Check if $f is a directory
if [[ -d $f ]]; then
echo "You are in the following folder - $f"
cd $f
# The filename to use for your function
do_stuff gaf_${f}_recording_mic.nii
fi
done
关于Linux:查找文件时使用文件夹名循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42377002/