在bash中的for循环中读取字符串数组

时间:2018-07-07 07:58:44

标签: python linux bash

我看了这个答案之后就声明了这个值数组。

Loop through an array of strings in Bash?

select GROUP_CONCAT(select 'a' union select 'b' separator ',')

这是我编写的完整代码

declare -a consti=("Baharagora" "Barhait" "Barkagaon" "Bishunpur" "Borio" "Chaibasa" "Chakradharpur" "Dumka" "Dumri" "Gandey" "Ghatsila" "Giridih" "Gomia" "Gumla" "Hatia" "Ichagarh" "Jama" "Jarmundi" "Jugsalai" "Kharasawan" "Khunti" "Littipara" "Lohardaga" "Madhupur" "Maheshpur" "Majhgaon" "Manoharpur" "Nala" "Nirsa" "Pakur" "Potka" "Shikaripara" "Silli" "Sindri" "Sisai" "Tundi")

此脚本正在读取*作为文字字符。我得到此输出,但它应该是number.pdf

#!/bin/bash

declare -a consti=("Baharagora" "Barhait" "Barkagaon" "Bishunpur" "Borio" "Chaibasa" "Chakradharpur" "Dumka" "Dumri" "Gandey" "Ghatsila" "Giridih" "Gomia" "Gumla" "Hatia" "Ichagarh" "Jama" "Jarmundi" "Jugsalai" "Kharasawan" "Khunti" "Littipara" "Lohardaga" "Madhupur" "Maheshpur" "Majhgaon" "Manoharpur" "Nala" "Nirsa" "Pakur" "Potka" "Shikaripara" "Silli" "Sindri" "Sisai" "Tundi")


for folder in "${consti[@]}"; do
  for file in "~/Documents/ElectoralRollPDFs/completed/"$folder/*.pdf; do
    echo "$file"
    ...
  done
done

在正确读取文件之前,我无法执行任何后续步骤。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以循环两次:

shopt -s nullglob   

declare dirs=(aaa bbb ccc)

for folder in "${dirs[@]}"; do
    for file in "/some/path/$folder/"*.pdf; do
        echo "$file"
        # do stuff here ...
    done
done

答案 1 :(得分:-1)

基本上,您需要添加另一个for循环以首先获取文件路径,并使用另一个for循环对其进行迭代。在这种情况下,利用命令替换非常方便。

for file in `for i in ${consti[@]}; do ls ~/Documents/ElectoralPDFs/completed/$i/*.pdf; done`
rest of the code