shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] MySQL bash script (Para quem interessar)


From: Marcus Gmail
Subject: Re: [shell-script] MySQL bash script (Para quem interessar)
Date: Mon, 20 Apr 2009 08:10:37 -0300
User-agent: Thunderbird 2.0.0.21 (Windows/20090302)

Bom Dia!

Obrigado Pedro, a mim interessou sim.

Abs! Fica com Deus!

Pedro Robson Leao escreveu:


::::::::::::::
include/mysql.sh
::::::::::::::
#!/bin/bash

# MySQL shell library
# Pedro Robson Leao - 2009/04/19
# address@hidden <mailto:pedro.leao%40gmail.com>

_DB_ERROR="";
_DB_ERROR_CODE=0;

_DB_RS_NEXT=1;
_DB_RS_COUNT=0;

function mysql_connect() {
local user=${1:-"root"};
local passwd=${2};
local db=${3:-"mysql"};
local host=${4:-"localhost"};

_DB_USER="${user}";
_DB_PWD="${passwd}";
_DB_HOST="${host}";
_DB="${db}";
}

function mysql_error() {
#ERROR 1046 (3D000) at line 1: No database selected
local error=$( echo ${1} | tr '£' ' ' );
if echo "${error}" | egrep -q "^ERROR [0-9]+ \([A-Za-z0-9]+\)" ; then
_DB_ERROR_CODE=$( echo ${error} | cut -d " " -f 2 );
_DB_ERROR=$( echo ${error} | cut -d : -f 2- );
else
false;
fi
}

function mysql_haserror() {
[ ${_DB_ERROR_CODE} -ne 0 ] && true || false;
}

function mysql_geterror() {
echo "${_DB_ERROR}";
}

function mysql_geterrno() {
echo "${_DB_ERROR_CODE}";
}

function mysql_execute() {
local line;
local ct=0;

while read line ; do
if [ ${ct} -eq 0 ] ; then
if mysql_error "${line}" ; then
#IFS=$OLDFS;
return;
else
_DB_COLUNS_NAME=( ${line} );
fi
else
_DB_RS[${ct}]="${line}";
fi
ct=$((ct+1));
done < <( mysql -u"${_DB_USER}" -p"${_DB_PWD}" -h"${_DB_HOST}" "${_DB}" -e "${1}" 2>&1 | tr ' ' '£' ); #done < <( mysql -u"${_DB_USER}" -p"${_DB_PWD}" -h"${_DB_HOST}" "${_DB}" -e "${1}" 2>&1 | sed s/'\t'/\"'\t'\"/g | sed s/$/\"/g | sed s/^/\"/g );
_DB_RS[${ct}]=;
_DB_RS_COUNT=${ct};
}

function mysql_count() {
echo "${_DB_RS_COUNT}";
}

function mysql_getfirst() {
_DB_RS_NEXT=1;
echo $( mysql_getnext );
}

function mysql_getnext() {
_DB_RS_NEXT=$((_DB_RS_NEXT+1));
}

function mysql_hasnext() {
[ ${_DB_RS_COUNT} -le $((_DB_RS_NEXT+1)) ] && return 1 || return 0;
}

function mysql_line() {
echo ${_DB_RS_NEXT};
}

function mysql_getfield() {
local size=${#_DB_COLUNS_NAME[@]};
local VCT=( ${_DB_RS[${_DB_RS_NEXT}]} );

if echo ${1} | egrep -q "^[0-9]+$" ; then
# pesquisa por numero do campo

[ ${1} -le ${size} ] && {
echo ${VCT[${1}]} | tr '£' ' ';
}
else
# pesquisa por nome do campo

local i;

for ((i=0;i<size;i++)) ; do
if [ "${1}" == "${_DB_COLUNS_NAME[$i]}" ] ; then
break;
fi
done

echo ${VCT[$i]} | tr '£' ' ';
fi
}
::::::::::::::
mysql_sample.sh
::::::::::::::
#!/bin/bash

source include/mysql.sh

#sample

mysql_connect root Germany information_schema;

mysql_execute "SELECT * FROM TABLES";

mysql_haserror && {
echo "$( mysql_geterrno ): $( mysql_geterror )";
exit;
}

mysql_getfirst;

echo "$(mysql_line) : $( mysql_getfield 0 )|$( mysql_getfield 1 )|$( mysql_getfield 2)|$( mysql_getfield 3)|$( mysql_getfield 4)|$( mysql_getfield 5)";

while mysql_hasnext ; do
mysql_getnext;
echo "$(mysql_line) : $( mysql_getfield TABLE_SCHEMA )|$( mysql_getfield ENGINE )|$( mysql_getfield TABLE_NAME )";
done





reply via email to

[Prev in Thread] Current Thread [Next in Thread]