[Klug-general] Bash / Curl Tuition

Alan at comm-tech alan at communitytechnology.org.uk
Sun Apr 17 22:06:26 UTC 2011


I have reverted to the original subject - hope this is OK for everyone - 
respond in the apology thread if not ;)

Stephen wrote:
> 1.2  My website is wordpress, so i guess, like you say i will have to 
> work out what commands to query WP db.
>
This shell function returns the value of field $FLDNAME from the first 
record in table $TBLNAME in database mysqldbname.

func_queryMysql() {
         MUSER="myuser"
         MPASS="mypass"
         MHOST="localhost"
         DB="mysqldbname"
         FLDNAME="$1"
         TBLNAME="$2"
         MYSQL="$(which mysql)"
         QUERYSTR="SELECT $TBLNAME.$FLDNAME FROM $TBLNAME"
         GETVAL="$($MYSQL -u $MUSER -p$MPASS $DB -se "$QUERYSTR")"
         echo "$GETVAL"
}

Use the function like this :

         result=$(func_queryMysql field table)
         echo $result

Bash is simply the wrong tool to be interacting with a mysql database 
however, scripts will break as soon as you swear at them with a $+=-!`"' 
or any other of a host of "special" characters. It will also scramble 
your brains. Use php for working mysql. Bash is good for the file and 
system donkey work though, so you need to call your bash scripts from 
php using this:

                $command = "$SCRIPTSDIR/bashscript.sh";
                $shellOutput = shell_exec($command.' > /dev/null; echo $?');

Probably someone else has a better syntax that's just my newbie hack...?

A good principle with scripting is to use the use the right tool for the 
job, for instance sending an email in bash is unreliable due to bad 
headers and spam bins, and you never know if it really went anywhere. 
Whereas using mutt is just like this:

/usr/bin/mutt -H $HEADERFILENAME $ATTACHMENTNAME < $MESSAGE

Good luck with the escaping\ escape\ characters, see you in the asylum ;)

Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/kent/attachments/20110417/39e92de8/attachment.htm>


More information about the Kent mailing list