Discussion Using mysql as a Calculator

Ther es no rule against t hat . Just be sure t o supply t he appropriat e argum ent s t o t he com m and.

1.33.3 Discussion

I f you need t o process query result s wit hin a program , youll t ypically use a MySQL program m ing int erface designed specifically for t he language youre using for exam ple, in a Perl script youd use t he DBI int erface . But for sim ple, short , or quick-and- dirt y t asks, it m ay be easier j ust t o invoke m ysql direct ly from wit hin a shell script , possibly post processing t he result s wit h ot her com m ands. For exam ple, an easy way t o writ e a MySQL server st at us t est er is t o use a shell script t hat invokes m ysql, as is dem onst rat ed lat er in t his sect ion. Shell script s are also useful for prot ot yping program s t hat you int end t o convert for use wit h a st andard API lat er. For Unix shell script ing, I recom m end t hat you st ick t o shells in t he Bourne shell fam ily, such as sh, bash, or ksh. The csh and t csh shells are m ore suit ed t o int eract ive use t han t o script ing. This sect ion provides som e exam ples showing how t o w rit e Unix script s for bin sh. I t also com m ent s briefly on DOS script ing. The sidebar Using Execut able Program s describes how t o m ake script s execut able and run t hem . Using Executable Programs When you writ e a program , youll generally need t o m ake it execut able before you can run it . I n Unix, you do t his by set t ing t he execut e file access m odes using t he chm od com m and: chmod +x myprog To run t he program , nam e it on t he com m and line: myprog However, if t he program is in your current direct ory, your shell m ight not find it . The shell searches for program s in t he direct ories nam ed in your PATH environm ent variable, but for securit y reasons, t he search pat h for Unix shells oft en is deliberat ely set not t o include t he current direct ory . . I n t hat case, you need t o include a leading pat h of . t o explicit ly indicat e t he program s locat ion: .myprog Som e of t he program s developed in t his book are int ended only t o dem onst rat e a part icular concept and probably never will be run out side your current direct ory, so exam ples t hat use t hem generally show how t o invoke t hem using t he leading . pat h. For program s t hat are int ended for repeat ed use, it s m ore likely t hat youll inst all t hem in a direct ory nam ed in your PATH set t ing. I n t hat case, no leading pat h will be necessary t o invoke t hem . This also holds for com m on Unix ut ilit ies such as chm od , which are inst alled in st andard syst em direct ories. Under Windows, program s are int erpret ed as execut able based on t heir filenam e ext ensions such as .exe or .bat , so chm od is unnecessary. Also, t he com m and int erpret er includes t he current direct ory in it s search pat h by default , so you should be able t o invoke program s t hat are locat ed t here wit hout specifying any leading pat h. Thus, if youre using Windows and you want t o run an exam ple com m and t hat is shown in t his book using . , you should om it t he . from t he com m and.

1.33.4 Writing Shell Scripts Under Unix