Date: Tue, 27 Oct 2009 16:17:55 +0000 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: freebsd-questions@freebsd.org Subject: Re: Using bash with MySQL Message-ID: <4AE71D33.9000102@infracaninophile.co.uk> In-Reply-To: <BLU0-SMTP742AE1BC94A0883AC0162393B90@phx.gbl> References: <BLU0-SMTP742AE1BC94A0883AC0162393B90@phx.gbl>
next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigE47CC8DE242624C28FD4867E Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable carmel_ny wrote: > I am in the process of writting a script that will use MySQL as a back > end. For the most part, I have gotten things to work correctly. I am > having one problem though. >=20 > Assume a data base: >=20 > database: MyDataBase > table: MyTable > field: defaults >=20 > Now, I have populated the 'defaults' fields with the declare > statements that I will use in the script. They are entered similar to > this: >=20 > declare -a MSRBL_LIST >=20 > Now, I issue this from my bash script: >=20 > SQL_USER=3Duser # MySQL user > SQL_PASSWORD=3Dsecret # MySQL password > DB=3DMyDataBase # MySQL data base name > HOST=3D127.0.0.1 # Server to connect to > NO_COLUMN_NAME=3D"--skip-column-names" > COM_LINE=3D"-u${SQL_USER} -p${SQL_PASSWORD} -h ${HOST} ${NO_COLUMN_NAME= }" > table=3DMyTable >=20 >=20 > DECLARE_STATEMENTS=3D($(mysql ${COM_LINE} -i -e"use ${DB}; SELECT defau= lts FROM "${table}" WHERE 1;")) >=20 > for (( i=3D0;i<${#DECLARE_STATEMENTS[*]};i++)); do > echo ${DECLARE_STATEMENTS[i]} > done >=20 > This output is produced: >=20 > declare > -a > MSRBL_LIST >=20 > Obviously, I want the output on one line for each field. I have tried > enclosing the variables with both single and double quote marks; > however, that does not work. Fields that do not contain spaces are > displayed correctly. >=20 > Obviously, I am doing something really stupid here. I hope someone can > assist me. I probably should ask this on the MySQL forum; however, I > was hoping that someone here might be able to supply a remedy. This loop is where it all goes horribly wrong: for (( i=3D0;i<${#DECLARE_STATEMENTS[*]};i++)); do echo ${DECLARE_STATEMENTS[i]} done In Posix shell, the intended functionality would be more usually coded li= ke this: IFS=3D$( echo ) for ds in $DECLARE_STATEMENTS ; do echo $ds done where $DECLARE_STATEMENTS is split on any characters present in $IFS -- the input field separators, here set to be just a newline character. (Yo= u don't have to use echo to do that; you can just put a literal newline bet= ween single quotes, but it's hard to tell all the different forms of whitespac= e apart if you're reading code snippets in an e-mail...) I suspect similar IFS trickery would work with bash, but I'm not familiar= with the array syntax stuff it uses. /bin/sh is perfectly capable for sh= ell programming and positively svelte when compared to bash and it's on every= FreeBSD machine ever installed, so why bother with anything else? Cheers, Matthew --=20 Dr Matthew J Seaman MA, D.Phil. 7 Priory Courtyard Flat 3 PGP: http://www.infracaninophile.co.uk/pgpkey Ramsgate Kent, CT11 9PW --------------enigE47CC8DE242624C28FD4867E Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.13 (FreeBSD) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEAREIAAYFAkrnHToACgkQ8Mjk52CukIykJgCeMUe/iyhiZ1MCmvqp5bu2Z64c NgwAn3o3L5rO7iZeR7mOyBB3VfyHhsqQ =KvSM -----END PGP SIGNATURE----- --------------enigE47CC8DE242624C28FD4867E--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4AE71D33.9000102>