I am trying to create a script which accepts inputs from the files listed under a certain directory, and based on the contents of those files, processes certain commands. Here is the scenario:

I have 5 files listed under /tmp/DIR/ - File1 File2 File3 File4 File5 Each file has different data/strings which needs to be compared to a certain value and return output.

I know this can be done by easier methods like writing a separate script for each file or by a long list of if/else to compare, but what I looking for some kind of dynamic function that cane return multiple values based on file names and then can be submitted to another function to process.


I usually do this with associated/hash arrays in bash (other shells are different).

declare -A file_values=( [file1]="value1" [file2]="value2" )
mapfile files < <( find /tmp/DIR -name "File?" -printf "%f\n" )
for f in "${files[@]}"
    output="output from file $f"
    if [ "${output}" = "${file_values[${f}]}" ]; then 
  • Yes, I used the array to get it done, however, did not consider mapfile. – PratiqueManUTD May 7 '18 at 15:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.