iPhone 3GS OS4 Upgrade
September 4, 2010 – 5:23 pm | One Comment

iTunes for Windows must be one of the buggiest, most poorly written applications out there. In terms of wasted months of your life it definitely ranks up there with Lotus Notes and Media Player 12. I don’t know how well iTunes works on a Mac (I like computer mice with twenty buttons, so I never was a big Mac fan), but I think I read somewhere that iTunes for Windows is the leading cause of suicides among iPhone owners.

Read the full story »
Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands and Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » MySQL, Scripts

Passing MySQL Commands from Shell Script

Submitted by Igor on June 25, 2009 – 3:30 amNo Comment
Passing MySQL Commands from Shell Script

Running MySQL commands from a shell script is a relatively simple task that has a lot of people baffled. Some say its too complicated and suggest using PHP or Perl, others claim doing so is a security risk (a favorite excuse of the ignorant), and some resort to using a shell script to writing SQL commands to a text file that MySQL would use as input. Below is a much more simple and direct way of generating and running complex SQL queries directly from a shell script without temporary files and without any security issues.

Let’s start with the basic idea:

echo "SELECT * FROM table_name" | mysql -u<db_user> -p<db_passwd> db_name

Using this method, you can pass any shell variables to MySQL. Aha, some will say, you have to put your password in the shell script and that is definitely not secure. You don’t have to: you can have your script prompt you for a password:

#!/bin/bash
echo -n "Enter username: " ; read db_user
echo -n "Enter $db_user password: " ; stty -echo ; read db_passwd ; stty echo ; echo ""
echo "SHOW DATABASES ;" | mysql --skip-column-names -u$db_user -p$db_passwd
echo -n "Enter database name: " ; read db_name
echo "SHOW TABLES ;" | mysql --skip-column-names -u$db_user -p$db_passwd $db_name
echo -n "Enter table name: " ; read table_name
echo "SELECT * FROM $table_name ;" | mysql -t -u$db_user -p$db_passwd $db_name

The script above will prompt your for the username and password (password will not be visible as you type it). It will then show you the list of available database and prompt you to select one. The script will then show you all the tables in that database and ask you to specify the name of the table you want to use. Finally, the script will select everything from that table. Everything is very simple, secure, and straightforward.

Popularity: 7% [?]

Related posts

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.