From owner-freebsd-questions@FreeBSD.ORG Tue Oct 27 19:37:36 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0F094106566B for ; Tue, 27 Oct 2009 19:37:36 +0000 (UTC) (envelope-from carmel_ny@hotmail.com) Received: from blu0-omc4-s10.blu0.hotmail.com (blu0-omc4-s10.blu0.hotmail.com [65.55.111.149]) by mx1.freebsd.org (Postfix) with ESMTP id B613E8FC20 for ; Tue, 27 Oct 2009 19:37:35 +0000 (UTC) Received: from BLU0-SMTP74 ([65.55.111.135]) by blu0-omc4-s10.blu0.hotmail.com with Microsoft SMTPSVC(6.0.3790.3959); Tue, 27 Oct 2009 12:37:35 -0700 X-Originating-IP: [67.189.183.172] X-Originating-Email: [carmel_ny@hotmail.com] Message-ID: Received: from scorpio.seibercom.net ([67.189.183.172]) by BLU0-SMTP74.blu0.hotmail.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Tue, 27 Oct 2009 12:37:34 -0700 Received: from scorpio.seibercom.net (scorpio.seibercom.net [192.168.1.103]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) (Authenticated sender: carmel_ny@scorpio.seibercom.net) by scorpio.seibercom.net (Postfix) with ESMTPSA id B330522848 for ; Tue, 27 Oct 2009 15:37:33 -0400 (EDT) Date: Tue, 27 Oct 2009 15:37:33 -0400 From: carmel_ny To: freebsd-questions@freebsd.org In-Reply-To: <4AE71D33.9000102@infracaninophile.co.uk> References: <4AE71D33.9000102@infracaninophile.co.uk> Organization: seibercom.net X-Mailer: Claws Mail 3.7.2 (GTK+ 2.16.6; i386-portbld-freebsd7.2) Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEX+/v7++v6YOTrq8PCcuIX989UvOSj++v0BNCbpAAAAB3RJTUUHsQwfFzs7RBhzUQAAAhJJREFUOI1dU8GOqzAMNKIoV1bvwD1i0ysqrHplIdBrVSX7ATSbd03VVvn9tQNtQy0hjAdn7LED4AAcPtWm9RV+MPSfxhBLx9ajd6X/ngB6/mTwnRSZua7i7Ca+0ctZKo4Qmz+JY13X6I3nFZBxIYW1PbgfQ5RP8g0XlltEWGf3cV03joYpRnFbvYDKbXjZlXyyhEZA4lI+cN3NaVXE4VKjSwTExO10eTEkkJVqIAD5z0nUBQJluQDRSQjcrBiHAJxZlAH5CUMBMC7OcJ4LMQNnxhZ1HYPscMc6J4UlWRMNwzOpCcAHKSICd1EDn83abdREIbXsHkD1OinP1aCUCOEVRaa1lMcvywUWdYgk13JQUpYNKmvXQ8Kw5ML9YI5h8SakctBc7E/IYuLhYd/zZIk+1gM1vNweQBvHE0j+oYah3sMqAytQYlZk6+ANaaawJdu3OFzYGMZ3iGpa3qMlq9ZH0VZTgrCtw/ngdYkEIIpSbP1bWQAdFdX9vocBdkH2qVjVmuMu3gI5rjs814EUdrCZgWlPaxZZ3RiLFUtr+ud0PXwp2dnQSNXgePt6AZpBj6UMJ7VQkzN4utVeaSW1Dhn/kblGrKeMvNGnzwX4zuEDarYz1KdPtR60Gul0Gued+515SJXhCsl+Tx/3kY/UDvicPll9mfu50t3tvQ/thZpJYgeuwdSKNJ6tCD98MCgoxLDaPxbwqqwPWaWiAAAAAElFTkSuQmCC X-Face: "\j?x](l|]4p?-1Bf@!wN<&p=$.}^k-HgL}cJKbQZ3r#Ar]\%U(#6}'?<3s7%(%(gxJxxcR Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 27 Oct 2009 19:37:35.0012 (UTC) FILETIME=[EED5BE40:01CA573C] Subject: Re: Using bash with MySQL X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: freebsd-questions@freebsd.org List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Oct 2009 19:37:36 -0000 On Tue, 27 Oct 2009 16:17:55 +0000 Matthew Seaman replied: >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. >> >> Assume a data base: >> >> database: MyDataBase >> table: MyTable >> field: defaults >> >> Now, I have populated the 'defaults' fields with the declare >> statements that I will use in the script. They are entered similar to >> this: >> >> declare -a MSRBL_LIST >> >> Now, I issue this from my bash script: >> >> SQL_USER=user # MySQL user >> SQL_PASSWORD=secret # MySQL password >> DB=MyDataBase # MySQL data base name >> HOST=127.0.0.1 # Server to connect to >> NO_COLUMN_NAME="--skip-column-names" >> COM_LINE="-u${SQL_USER} -p${SQL_PASSWORD} -h ${HOST} >> ${NO_COLUMN_NAME}" table=MyTable >> >> >> DECLARE_STATEMENTS=($(mysql ${COM_LINE} -i -e"use ${DB}; SELECT >> defaults FROM "${table}" WHERE 1;")) >> >> for (( i=0;i<${#DECLARE_STATEMENTS[*]};i++)); do >> echo ${DECLARE_STATEMENTS[i]} >> done >> >> This output is produced: >> >> declare >> -a >> MSRBL_LIST >> >> 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. >> >> 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=0;i<${#DECLARE_STATEMENTS[*]};i++)); do > echo ${DECLARE_STATEMENTS[i]} >done > >In Posix shell, the intended functionality would be more usually coded >like this: > >IFS=$( 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. >(You don't have to use echo to do that; you can just put a literal >newline between single quotes, but it's hard to tell all the different >forms of whitespace 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 shell programming and positively svelte when compared to >bash and it's on every FreeBSD machine ever installed, so why bother >with anything else? Matthew, unfortunately, that is not the problem. However, you post pointed me in the right direction. Notice this line: (should all be on one line) DECLARE_STATEMENTS=($(mysql ${COM_LINE} -i -e"use ${DB}; SELECT defaults FROM "${table}" WHERE 1;")) I am saving the output of the MySQL search in an array. Unfortunately, the array is assuming that each space in the returned search is a new element. I have not found out a way to prevent this. If you have any suggestions, I would appreciate them. I have tried putting: IFS=$( echo ) before the 'DECLARE_STATEMENTS' call; however, that produces this error: ./scamp-sql: line 128: syntax error near unexpected token `)' ./scamp-sql: line 128: `DECLARE_STATEMENTS=$(mysql ${COM_LINE} -i -e"use ${DB}; SELECT defaults FROM ${table} WHERE '1';"))' I know the principal is correct because I tried this code snippet: IFS=$( echo ) a="1 2 3" b=($a) echo ${b[0]} 1 2 3 CONCAT would not benefit me either since the 'space' would still exist in the returned search query. I might have to devise some hack to combine the three elements into one. Fortunately, there are three parts to every element. Unfortunately, there are a lot of them. -- Jerry gesbbb@yahoo.com |::::======= |::::======= |=========== |=========== | Sweater, n.: A garment worn by a child when its mother feels chilly.