• Uncategorized

About mysql : Understanding-the-meaning-of-the-following-method-in-linuxbash

Question Detail

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-

  1. What is echo “$1” doing here?
  2. Why are we using the pipe operator?
  3. And what is the relation between the echo command and the mysql command?

Question Answer

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.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.