Is it possible to use bash to fetch a string from an array provided in the following syntax within a config file?

|        | Version | PackageManager | Webserver | Database |
| Centos | 7       | yum            | httpd     | mariadb  |
| Ubuntu | 14.04   | apt-get        | apache    | mysql    |

From within a bash script (bootstrap.sh) I want to be able to write a single line to install a package such as "./install.sh -webserver" to install either httpd or apache depending on the Server OS.

- From within bootstrap.sh I simply call ./install.sh -webserver" that holds the logic to check which is the current Server OS and if the Server OS is supported
- check what is to be installed (-webserver)
- looks into transform table to choose the right package manager and package name for the related OS
- and returns the command required (yum install httpd) within bootstrap.sh to install httpd

I know this looks weird but you help is much apprecaited.

  • And does your file really contain all that useless formatting? Are the | and -- actually there? Why?
    – terdon
    Mar 26 '16 at 10:26
  • I am using the table as provided as part of my DRY approach. My Documentation is going to be build from comments / specific syntax from within the code/config file. However, you might be right, I could also convert tab stops for the documentation for "readabillity" and keep the config table for transform more clean.
    – frank
    Mar 26 '16 at 10:56
  • I really recommend you do so. Having the extra characters and whitespace will make everything you need to do downstream more complicated.
    – terdon
    Mar 26 '16 at 11:02

If I understand you correctly, you need a command that will take an OS as input and return the correct installation command by reading the table you show. If your table truly is in the format you show, including the pretty-for-humans but useless-for-machines | and -, you can do this to get the webserver:

awk -vOS="Centos" '$2==OS{print $6,"install",$8}' table 

And this for the database:

awk -vOS="Centos" '$2==OS{print $6,"install",$10}' table 

The actual output would be:

$ awk -vOS="Centos" '$2==OS{print $6,"install",$8}' table 
yum install httpd

$ awk -vOS="Centos" '$2==OS{print $6,"install",$10}' table 
yum install mariadb

$ awk -vOS="Ubuntu" '$2==OS{print $6,"install",$8}' table 
apt-get install apache

$ awk -vOS="Ubuntu" '$2==OS{print $6,"install",$10}' table 
apt-get install mysql

If your table is in a simpler format, like:

         Version  PackageManager  Webserver  Database 
 Centos  7        yum             httpd      mariadb  
 Ubuntu  14.04    apt-get         apache     mysql    

The above would become:

awk -vOS="OS_NAME" '$1==OS{print $3,"install",$4}'
awk -vOS="OS_NAME" '$1==OS{print $3,"install",$5}'

So, in your script, assuming you have the OS saved as $os and the option passed (webserver or database) as $option, you can do:

case $option in
         commmand=$(awk -vOS="$os" '$2==OS{print $6,"install",$8}' table ) ;;
          command=$(awk -vOS="$os" '$2==OS{print $6,"install",$10}' table) ;;
  • Thank you very much! This goes in the right direction, but actually the only paramater I want to provide is the option for the package to be installed (--webserver). The script / code I am looking for therefore should check what OS the script is running on, look into the "table" if the OS is supported and "build" the command required for that OS by looking for the right package manager and the name of yum package. Would that be possible too?
    – frank
    Mar 26 '16 at 10:53
  • @frank that is a very different proposition from what you are asking. Your question says you already have the logic to test the OS. Ping me in /dev/chat and we can discuss how you can clarify your requirements.
    – terdon
    Mar 26 '16 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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