Writing Shell Scripts Under Windows

mysql cookbook MYSQL_INPUT SELECT COUNT As New log entries: FROM log_tbl WHERE date_added = DATE_SUBCURDATE ,INTERVAL 1 DAY; MYSQL_INPUT When you use - e or here-docum ent s, you can refer t o shell variables wit hin t he query input — alt hough t he following exam ple dem onst rat es t hat it m ight be best t o avoid t he pract ice. Suppose you have a sim ple script count _rows.sh for count ing t he rows of any t able in t he cookbook dat abase: binsh count_rows.sh - count rows in cookbook database table require one argument on the command line if [ -ne 1 ]; then echo Usage: count_rows.sh tbl_name; exit 1; fi use argument 1 in the query string mysql cookbook MYSQL_INPUT SELECT COUNT AS Rows in table: FROM 1; MYSQL_INPUT The script uses t he shell variable, which holds t he com m and- line argum ent count , and 1 , which holds t he first argum ent aft er t he script nam e. count _rows.sh m akes sure t hat exact ly one argum ent was provided, t hen uses it as a t able nam e in a row-count ing query. To run t he script , invoke it wit h a t able nam e argum ent : .count_rows.sh limbs Rows in table: 12 Variable subst it ut ion can be helpful for const ruct ing queries, but you should use t his capabilit y wit h caut ion. A m alicious user could invoke t he script as follows: .count_rows.sh limbs;DROP TABLE limbs I n t hat case, t he result ing query input t o m ysql becom es: SELECT COUNT AS Rows in table: FROM limbs;DROP TABLE limbs; This input count s t he t able rows, t hen dest roys t he t able For t his reason, it m ay be prudent t o lim it use of variable subst it ut ion t o your own privat e script s. Alt ernat ively, rewrit e t he script using an API t hat allows special charact ers such as ; t o be dealt w it h and rendered harm less see Recipe 2.8 .

1.33.5 Writing Shell Scripts Under Windows

Under Windows, you can run m ysql from wit hin a bat ch file a file wit h a .bat ext ension . Here is a Windows bat ch file, m ysql_upt im e.bat , t hat is sim ilar t o t he m ysql_upt im e.sh Unix shell script shown earlier: ECHO OFF REM mysql_uptime.bat - report server uptime in seconds mysql -B -N -e SHOW STATUS LIKE Uptime Bat ch files m ay be invoked wit hout t he .bat ext ension: C:\ mysql_uptime Uptime 9609 DOS script ing has som e serious lim it at ions, however. For exam ple, here- docum ent s are not support ed, and com m and argum ent quot ing capabilit ies are m ore lim it ed. One way around t hese problem s is t o inst all a m ore reasonable working environm ent ; see t he sidebar Finding t he DOS Prom pt Rest rict ive? Finding the DOS Prompt Restrictive? I f youre a Unix user who is com fort able wit h t he shells and ut ilit ies t hat are part of t he Unix com m and- line int erface, you probably t ake for grant ed som e of t he com m ands used in t his chapt er, such as grep, sed, t r, and t ail. These t ools are so com m only available on Unix syst em s t hat it can be a rude and painful shock t o realize t hat t hey are nowhere t o be found if at som e point you find it necessary t o work at t he DOS prom pt under Windows. One way t o m ake t he DOS com m and- line environm ent m ore palat able is t o inst all Cygnus t ools for Windows Cygwin or Unix for Windows UWI N . These packages include som e of t he m ore popular Unix shells as well as m any of t he ut ilit ies t hat Unix users have com e t o expect . Program m ing t ools such as com pilers are available w it h each package as well. The package dist ribut ions m ay be obt ained at t he following locat ions: ht t p: www.cygwin.com ht t p: www.research.at t .com sw t ools uwin These dist ribut ions can change t he way you use t his book under Windows, because t hey elim inat e som e of t he except ions where I qualify com m ands as available under Unix but not Windows. By inst alling Cygwin or UWI N, m any of t hose dist inct ions becom e irrelevant .

Chapter 2. Writing MySQL-Based Programs

Sect ion 2.1. I nt roduct ion Sect ion 2.2. Connect ing t o t he MySQL Server, Select ing a Dat abase, and Disconnect ing Sect ion 2.3. Checking for Errors Sect ion 2.4. Writ ing Library Files Sect ion 2.5. I ssuing Queries and Ret rieving Result s Sect ion 2.6. Moving Around Wit hin a Result Set Sect ion 2.7. Using Prepared St at em ent s and Placeholders in Queries Sect ion 2.8. I ncluding Special Charact ers and NULL Values in Queries Sect ion 2.9. Handling NULL Values in Result Set s Sect ion 2.10. Writ ing an Obj ect -Orient ed MySQL I nt erface for PHP Sect ion 2.11. Ways of Obt aining Connect ion Param et ers Sect ion 2.12. Conclusion and Words of Advice