0

I am a noob when it comes to the Shell

This block should work, but it doesn't

     #!/bin/bash 
     # LOCAL CONFIGURATION SETTINGS - DB_CONFIG is a "fake" associative array
     #----------------------------------------------------------------------------------
     DB_CONFIG=(
        "DB_NAME=>"
        "DB_USER=>root"
        "DB_PASSWORD=>root"
        "DB_HOST=>127.0.0.1"
        "DB_CHARSET=>utf8"
        "DB_COLLATE=>"
     );

     for prop in ${DB_CONFIG[@]} ; do
     key=${prop%%=>*}
     value=${prop##*=>}
     echo $key;
     if[["$key" == "DB_HOST"]] then 
           db_host="$value"

     if[["$key" == "DB_PASSWORD"]] then  
           db_password="$value"

     done;


     # now set the mysql_conn string with the vars above.
     MYSQL_CON="/Applications/MAMP/Library/bin/mysql --host=$db_host -uroot -$db_password"
     exit;
     REPO='http://svn.wp-plugins.org/'

The problem is how to check the condition if $key == 'DB_HOST' I just can't figure out the syntax of the expression. I have look into how to do it, but on my mac ( running bash 3.2 ) if complains of syntax errors.

3 Answers 3

1

you need to have leave spaces for [ and ] and a ; before then

if [ "$key" == "DB_HOST" ]; then 
      db_host="$value"
fi
Sign up to request clarification or add additional context in comments.

Comments

0

This should work for you:

DB_CONFIG=(
  "DB_NAME=>"
  "DB_USER=>root"
  "DB_PASSWORD=>root"
  "DB_HOST=>127.0.0.1"
  "DB_CHARSET=>utf8"
  "DB_COLLATE=>"
);  

for prop in ${DB_CONFIG[@]}; do
  key=${prop%%=>*}
  value=${prop##*=>}

  if [[ "$key" == "DB_HOST" ]]; then 
       db_host="$value"
  elif [[ "$key" == "DB_PASSWORD" ]]; then  
       db_password="$value"
  fi  
done

echo "$db_host" "$db_password"

You were missing the space after [[ and before ]]. You were also missing your fi s, which correspond to close brace in C or Java, and are required by bash. Additionally, if-statements require a ; after a conditional test.

Comments

0

This is an awesome quick reference guide for bash scripting

http://linuxconfig.org/Bash_scripting_Tutorial

1 Comment

Thanks for the link. I will need to refer to it over the next couple of days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.