首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bash/Linuxfor循环不像我希望的那样工作

bash/Linuxfor循环不像我希望的那样工作
EN

Stack Overflow用户
提问于 2022-06-11 17:00:13
回答 1查看 32关注 0票数 -1

有人能告诉我为什么不起作用吗?

其主要目的是找到chapterNumber的索引。

代码语言:javascript
复制
chapter_number_arr=(ch-01 ch-02 ch-03 ch-04 ch-05 ch-06 ch-07 ch-08 ch-09 ch-10)
chapterNumber=ch-09
代码语言:javascript
复制
     for chapter_number in "${chapter_number_arr[@]}" ; do
        if [[ $chapter_number == "$chapterNumber" ]] ; then 
                index_file_name=$index2; 
        fi;
     done  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-11 17:07:09

代码语言:javascript
复制
chapter_number_arr=(ch-01 ch-02 ch-03 ch-04 ch-05 ch-06 ch-07 ch-08 ch-09 ch-10)
chapterNumber=ch-09

for chapter_idx in "${!chapter_number_arr[@]}"; do
  if [[ ${chapter_number_arr[$chapter_idx]} = "$chapterNumber" ]]; then
    index_file_name=$chapter_idx
  fi
done

...will set index_file_name=8.从问题中可以看出,这就是您想要的(这是与chapter_number_arr对应的chapterNumber索引)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72586288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档