Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Following

mysql -u root -ppassword -D database -s -N -e "SELECT id FROM myTable"

with special password and database is working fine.

I want to split the code into two parts:

  1. Executional part:

    mysqlE=mysql -u root -ppassword -D database -s -N -e

and

  1. MySQL command part:

    query="SELECT id FROM myTable"

to execute it with something similar like:

mysqlE query

How can I do this?

share|improve this question
1  
passing a password on the command line is a bad idea as it can easily be viewed from /proc or by running ps. See unix.stackexchange.com/a/205184/7696 for a better alternative. Remember to chmod 600 the config file. – cas Oct 21 '15 at 0:02
    
Thank you very much for the hint! – Peter Leger Oct 21 '15 at 0:52
up vote 1 down vote accepted

You almost have it already for a shell script:

#!/bin/bash

mysqlE="mysql -u root -ppassword -D database -s -N -e"
query="SELECT id FROM myTable"
$mysqlE "$query"

Another way is to put the mysql command in a function (put it in ~/.bashrc for instance)

function mysqlE()
{
    mysql -u root -ppassword -D database -s -N -e "$@"
}

from a new shell or source ~/.bashrc use

mysqlE "$query"
share|improve this answer
    
It's working fine. Thank you very much! – Peter Leger Oct 20 '15 at 21:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.