小编yz4*_*26f的帖子

For 循环格式化

我在格式化长字符串输入的嵌套 for 循环时遇到问题:

我目前正在使用以下嵌套的 for 循环来命名来自我省略的单独程序的输出文件:

#!/bin/bash
for hemi in lh rh
do
    for measure in thickness area volume meancurv gauscurv foldind curvind
    do
        echo ${hemi}"--"${measure}
    done
done
Run Code Online (Sandbox Code Playgroud)

我希望能够像下面这样格式化循环,以方便运行多个不同的循环,其中包含可能需要修改的更长的数组列表。只需要在一个地方修改列表将大大减少错误/遗漏等的机会:

#!/bin/bash

hemi=( lh rh )
measure=( thickness area volume meancurv gauscurv foldind curvind )

for((i=${hemi[${i}]};i<2;i++));do
    for((j=${measure[${j}]};j<7;j++));do
        echo ${hemi[${i}]}"--"${measure[${j}]}
    done
done
Run Code Online (Sandbox Code Playgroud)

这是我得到的输出,我无法弄清楚我做错了什么:

lh--thickness
lh--area
lh--volume
lh--meancurv
lh--gauscurv
lh--foldind
lh--curvind
./for_loop_testing.sh: line 7: ((: j=: syntax error: operand expected (error token is "=")
Run Code Online (Sandbox Code Playgroud)

bash shell-script

6
推荐指数
2
解决办法
1678
查看次数

标签 统计

bash ×1

shell-script ×1