Search code examples
mysqlsqlbashshellconfiguration-files

Config files for a script


I have a script in bash in linux that is supposed to import data from csv into mysql database. I need to make this script as generic as possible so I need to control it using config files that will specify which data goes to which table, etc. so that I don't change the script in the future when I need to make modifications to the tables for example.

How can I start and how can I do it ? as I am completely new to this topic.


Solution

  • When you are completely new, first try make it running without the generic approach. Perhaps you can already set the dynamic fields (that you want to move to a config file later) using variables.
    You will have to rewrite the code few times when you are learning scripting and notice where you need common functions or config files.
    Once you have a running prototype, copy the solution to a new directory and go on from there. One way would be writing all dynamic variables in config files:

    field1="value1"
    field2="value2 with spaces, that is why I use quotes"
    field3="3"
    field4=""
    

    In the main script you can read the config file with source yourfile.cfg. Perhaps you know which set of variables you want when you start your script. Then you can use a parameter for it:

    #!/bin/bash
    if [ $# -eq 0 ]; then
       echo "No parameter given, using default"
       source default.cfg
    else
       source $1.cfg
    fi