0

Using bash to list the files that match a find command and loop through them one at a time. Problem is, the loop displays all elements of the array instead of single element.

    declare poslogs=$(find /opt/upload/archive/ -name "pospoll*" -mtime -3 -printf '%f\n')
    for i in ${poslogs[@]}; do
        unzip -p /opt/upload/archive/$i $poslogfn.${reg}* \
        | grep -B5 "$trans" | grep -o "TransactionType.*" > $resultfile.poslog
    done

Result is

+ declare 'poslogs=pospoll.1699.20211011230928.zip
pospoll.1699.20211012233221.zip
pospoll.1699.20211013232932.zip'
  for i in '"${poslogs[@]}"'
+ unzip -p '/opt/upload/archive/pospoll.1699.20211011230928.zip
pospoll.1699.20211012233221.zip
pospoll.1699.20211013232932.zip' 'PosLog.1.*'

Result should be

++ find /opt/upload/archive/ -name 'pospoll*' -mtime -3 -printf '%f\n'
+ declare 'poslogs=pospoll.1699.20211011230928.zip
pospoll.1699.20211012233221.zip
pospoll.1699.20211013232932.zip'
  for i in '"${poslogs[@]}"'
+ unzip -p '/opt/upload/archive/pospoll.1699.20211011230928.zip 'PosLog.1.*'
  • Arrays are created with `declare -a`. – choroba Oct 14 '21 at 16:22
  • `var=$(...)` creates a string; you want `var=($(...))`. Or, even better, a technique from the duplicate. – Benjamin W. Oct 14 '21 at 16:22
  • @choroba - Thanks, missed that when typing this up - even with declare -a - it still fails in the exact same way. I am using bash 4.3 so I am going to take SO's suggested answer and explore over here https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash – Gingerbeard Oct 14 '21 at 16:27
  • 1
    You also need the parentheses `var=(...)` and double quote the `"${poslogs[@]}"`. – choroba Oct 14 '21 at 16:47

0 Answers0