Convert country code to country name from shell script

I have a parameter passed to my shell script which is an ISO 3166-2 country code, which specifies a file to process, e.g. $1 = 'be'.

I want to find a corresponding file, e.g. be.pdf, and rename it to the country name, e.g. belgium.pdf.

I don't even know where to start with this?

The lookup could be a CSV file, or a sql database, or something else. I have no preferences. I just need to be able to take the input and lookup the new value and do the rename.

1 answer

  • answered 2019-11-08 14:21 dash-o

    Assuming you have the mapping in a file, you can use bash associative arrays

    code=$1
       # Associative array, keyed of country code
    declare -A cc
    while read xcc xname ; do
      cc[$xcc]=$xname
    done < cc_codes.txt
    
      # Translate, and rename
    fullname=${cc[$code]}
    mv "$cc.pdf" "$fullname.pdf"
    

    Map file (cc_codes.txt) is simple space separated

    us united_states
    be belgium
    uk United_Kindom
    ...
    

    Please note that this uses bash specific features