Writing Shell Scripts Under Unix

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

Here is a shell script t hat report s t he current upt im e of t he MySQL server. I t runs a SHOW STATUS query t o get t he value of t he Uptime st at us variable t hat cont ains t he server upt im e in seconds: binsh mysql_uptime.sh - report server uptime in seconds mysql -B -N -e SHOW STATUS LIKE Uptime The first line of t he script t hat begins wit h is special. I t indicat es t he pat hnam e of t he program t hat should be invoked t o execut e t he rest of t he script , bin sh in t his case. To use t he script , creat e a file nam ed m ysql_upt im e.sh t hat cont ains t he preceding lines and m ake it execut able wit h chm od + x. The m ysql_upt im e.sh script runs m ysql using - e t o indicat e t he query st ring, - B t o generat e bat ch t ab- delim it ed out put , and - N t o suppress t he colum n header line. The result ing out put looks like t his: .mysql_uptime.sh Uptime 1260142 The com m and shown here begins wit h . , indicat ing t hat t he script is locat ed in your current direct ory. I f you m ove t he script t o a direct ory nam ed in your PATH set t ing, you can invoke it from anywhere, but t hen you should om it t he . from t he com m and. Not e t hat m oving t he script m ake cause csh or t csh not t o know where t he script is locat ed unt il your next login. To rem edy t his wit hout logging in again, use rehash aft er m oving t he script . The following exam ple illust rat es t his process: .mysql_uptime.sh Uptime 1260348 mv mysql_uptime.sh usrlocalbin mysql_uptime.sh mysql_uptime.sh: Command not found. rehash mysql_uptime.sh Uptime 1260397 I f you prefer a report t hat list s t he t im e in days, hours, m inut es, and seconds rat her t han j ust seconds, you can use t he out put from t he m ysql STATUS st at em ent , which provides t he following inform at ion: mysql STATUS; Connection id: 12347 Current database: cookbook Current user: cbuserlocalhost Current pager: stdout Using outfile: Server version: 3.23.47-log Protocol version: 10 Connection: Localhost via UNIX socket Client characterset: latin1 Server characterset: latin1 UNIX socket: tmpmysql.sock Uptime: 14 days 14 hours 2 min 46 sec For upt im e report ing, t he only relevant part of t hat inform at ion is t he line t hat begins wit h Uptime . I t s a sim ple m at t er t o w rit e a script t hat sends a STATUS com m and t o t he server and filt ers t he out put wit h grep t o ext ract t he desired line: binsh mysql_uptime2.sh - report server uptime mysql -e STATUS | grep Uptime The result looks like t his: .mysql_uptime2.sh Uptime: 14 days 14 hours 2 min 46 sec The preceding t wo script s specify t he st at em ent t o be execut ed by m eans of t he - e com m and- line opt ion, but you can use ot her m ysql input sources described earlier in t he chapt er, such as files and pipes. For exam ple, t he following m ysql_upt im e3.sh script is like m ysql_upt im e2.sh but provides input t o m ysql using a pipe: binsh mysql_uptime3.sh - report server uptime echo STATUS | mysql | grep Uptime Som e shells support t he concept of a here-docum ent , which serves essent ially t he sam e purpose as file input t o a com m and, except t hat no explicit filenam e is involved. I n ot her words, t he docum ent is locat ed right here in t he script , not st ored in an ext ernal file. To provide input t o a com m and using a here-docum ent , use t he following synt ax: command MARKER input line 1 input line 2 input line 3 ... MARKER MARKER signals t he beginning of t he input and indicat es t he m arker sym bol t o look for at t he end of t he input . The sym bol t hat you use for MARKER is relat ively arbit rary, but should be som e dist inct ive ident ifier t hat does not occur in t he input given t o t he com m and. Her e-docum ent s are a useful alt ernat ive t o t he - e opt ion when you need t o specify lengt hy query input . I n such cases, when -e becom es awkward t o use, a here-docum ent is m ore convenient and easier t o writ e. Suppose you have a log t able log_tbl t hat cont ains a colum n date_added t o indicat e when each row was added. A query t o report t he num ber of records t hat were added yest erday looks like t his: SELECT COUNT As New log entries: FROM log_tbl WHERE date_added = DATE_SUBCURDATE ,INTERVAL 1 DAY; That query could be specified in a script using - e, but t he com m and line w ould be difficult t o read because t he query is so long. A here- docum ent is a m ore suit able choice in t his case because you can writ e t he query in m ore readable form : binsh new_log_entries.sh - count yesterdays log entries 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