0

I declared this array of values after looking at this answer.

Loop through an array of strings in 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")

This is the complete code that I wrote

#!/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

This script is reading the * as literal charater. I am getting this output, but it should be number.pdf

~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf
I/O Error: Couldn't open file '~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf': No such file or directory.

Until the file is read properly, I cannot do any of the next steps. Please help.

  • Check output of `echo ~/Documents/ElectoralRollPDFs/completed/"${consti[@]}"/*.pdf` – Cyrus Jul 07 '18 at 08:07
  • /home/aayushmalik/Documents/ElectoralRollPDFs/completed/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 Saraikella Shikaripara Silli Sindri Sisai Tundi/*.pdf –  Jul 07 '18 at 08:13
  • This is not how I want it, I want that for every run of the loop just give me one of array elements /home/aayushmalik/Documents/ElectoralRollPDFs/completed/Baharagora –  Jul 07 '18 at 08:14
  • This is a list of constituencies, I want my script to go into each folder, take each pdf files, apply the operations I have written below and give me the clean outputted file –  Jul 07 '18 at 08:17

2 Answers2

0

You can just loop twice:

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
PesaThe
  • 7,259
  • 1
  • 19
  • 43
  • Hi, I used the above syntax and this is what I got ~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf I/O Error: Couldn't open file '~/Documents/ElectoralRollPDFs/completed/Baharagora/*.pdf': No such file or directory. This shows that it is not reading the number that is associated with * i.e. 1 2 3 it's taking * to be a literal –  Jul 07 '18 at 10:46
  • Are there any `pdf` files in your `Baharagora` directory? Try the updated code. – PesaThe Jul 07 '18 at 10:48
  • Yes, there are 264 files numbered as 1.pdf 2.pdf 3.pdf ... 264.pdf etc –  Jul 07 '18 at 10:55
  • Did you by any chance quote the `~`? Put it outside the quotes. – PesaThe Jul 07 '18 at 10:55
  • Put it outside and tried again, but still, it's showing the same thing treating * as a literal –  Jul 07 '18 at 10:58
  • Can you paste here the exact line of the second `for`? Exactly as it is in your script now. – PesaThe Jul 07 '18 at 11:01
  • If the third line is being read, it means the first loop looping over folders is correct, I am sure it's a tiny mistake which I am making, but not able to get. :( –  Jul 07 '18 at 11:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174556/discussion-between-aayush-malik-and-pesathe). –  Jul 07 '18 at 11:03
-1

Basically you need to add another for loop to get the file path first and the iterate over it using another for loop. Taking advantage command substitution can be very handy in this case.

for file in `for i in ${consti[@]}; do ls ~/Documents/ElectoralPDFs/completed/$i/*.pdf; done`
rest of the code
Tejas Sarade
  • 1,144
  • 12
  • 17
  • This will be subject to word splitting and pathname expansion...Even ordinary files with spaces will break it. – PesaThe Jul 07 '18 at 10:26