DB_USER = 'db_user'
DB_PASS = 'db_pass'
DB_HOST = 'db_host'
DB = 'db'
function dbConnection() {
echo "$1" | mysql -u${DB_USER} -p${DB_PASS} -h${DB_HOST} ${DB} -N
}
Request you to ignore the actual values of the db variables. I wanted to understand the meaning of the above method. I have the following doubts-
- What is echo "$1" doing here?
- Why are we using the pipe operator?
- And what is the relation between the echo command and the mysql command?
CodePudding user response:
mysql
is able to read queries from stdin and |
connects the stdout of the left-hand side command to the stdin of the right-hand side command. So echo "$1"
will write a query (the first argument passed to dbConnection
) to stdout which will be then read by the mysql
command from its stdin.
CodePudding user response:
Try passing it some valid SQL.
dbConnection "SELECT 1 2;"
The string passed becomes the argument passed in.
echo "$1"
becomes (effectively)
echo "SELECT 1 2;"
which the pipe then connects to mysql`'s stdin - it becomes the input to the program.
It's the script to execute.