Linux:查找文件时使用文件夹名循环

标签 linux loops directory

我目前正在尝试使用以下 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/

相关文章:

java - 当循环后有 `println` 时,为什么这个循环多次的程序需要时间?

java - 我的 for 循环无法正确检查数组中是否存在 0 或 1

java - 解析 Xml 仅返回 1 项

MacOS 终端转到带有空格和括号的文件夹

c - 在 XeonPhi 上使用 AVX 内联汇编的 vector 和

linux - 当用户指定要在unix中创建的编号时如何创建文件夹?

linux - Linux 和 Mac 上的 Azure CLI : certificate trouble

linux - 使用 chkservd 的 cassandra nosql 自动重启服务

c++ - 获取当前目录,我可以在不知道最大缓冲区大小的情况下进行吗?

linux - 在 Perl 中打印目录中的所有文件 - 将不起作用