Is it possible to use a shell script to run this following command?
mysql -u user use mydb delete from mydata where row='xx'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
This is a job for a "here document":
mysql -u user << END_CMDS use mydb delete from mydata where row='xx' END_CMDS
The "here document" is a way of putting the SQL in-line inside the script itself. I'm not sure how the user ID's MySQL password plays into this. You'll have to experiment if that user has a password.
mysql invocation can be indented in your shell script, but do not indent the token that ends your "here document", which is END_CMDS in my example above.
You may be interested to examine some of the scripts on my mythtv-tools web page. They are reasonably good examples of running mysql commands from a shell script, and of building up moderately complicated mysql commands (e.g. with multiple WHERE clauses) based on getopt option parsing.
They all use the same basic technique of constructing an SQL string and piping it into
None of them use the
--password options, as any user on the system can trawl through
/proc (or just run
ps) and see the login and password in the command-line options. Instead they rely on the existence of a
~/.my.cnf file containing login credentials.