alditis
alditis

Reputation: 4817

Function in bash not working

My bash: "testing" with a function "query"

#!/bin/bash
MY_USER="root"
MY_PASSWORD="my_password"
MY_BD="my_db"
MY_HOME="/home/express"

function query {
    mysql -e $1 -u $MY_USER --password=$MY_PASSWORD $MY_BD
}

case $1 in
"period")
    mysql -e "SELECT cron_period()" -u $MY_USER --password=$MY_PASSWORD $MY_BD;;
"hold")
    query "SELECT cron_hold()";;
*)
    echo "Others";;
esac

Test:

testing period // Working with complete command
testing hold   // Not working with function query, show commands info of MySQL

The problem is when using the function query, but do not know how to correct it.

Any suggestions please?

Upvotes: 3

Views: 2796

Answers (3)

user2678992
user2678992

Reputation: 11

Try that:

function query {
    mysql -e ${1} -u $MY_USER --password=$MY_PASSWORD $MY_BD
}

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

Try like this ie, put $1 in double qoutes:

function query {
    mysql -e "$1" -u $MY_USER --password=$MY_PASSWORD $MY_BD
}

Upvotes: 3

Ben
Ben

Reputation: 693

Try putting double quotes around the $1 in your function.

Upvotes: 4

Related Questions