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?
2
Answers
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. Soecho "$1"
will write a query (the first argument passed todbConnection
) to stdout which will be then read by themysql
command from its stdin.Try passing it some valid SQL.
The string passed becomes the argument passed in.
becomes (effectively)
which the pipe then connects to mysql`’s stdin – it becomes the input to the program.
It’s the script to execute.