Archivio

Posts Tagged ‘shell’

bash scripting: change delim using for

gennaio 7, 2013 Commenti disabilitati

Usually when I write shell script I prefer to use the statement “for”.
But there’s a problem: by default the ‘for’ get every single word.

That’s because if not specified¬†the ‘for’ use as delimiter the space character (” “).
So, if I want to work with several word in a single iteration i need to change the delimiter.

That’s easy!
To change the delimiter you just need to change the variable IFS.

Let’s see some code:


LOGFILE=/var/log/mylogfile.log
IFS=";"                              # change delimiter in ";" (magic)
EXCEPTIONS="timeout;java.somerror;a lot of errors"    # my exception 
for word in $EXCEPTIONS;do
    echo -n "Check Exception: \"$word\" ..."         # escape "
    N=$(grep -ic $word $LOGFILE)                     # check log
    if [ $N -gt 0 ];then                             # if found
        echo -e "\nWARNING: Found $N exception:\"$word\"" # print 
    else
        echo "None"                                  # else no
    fi
done

output:


> Check Exception: "timeout"...None
> Check Exception: "java.somerror"...None
> Check Exception: "a lot of errors"...
WARNING: Found 3 exception "a lot of errors"

That’s an easy way to use the ‘for’ statement even if usually cant and so you can write good code.
enjoy ūüôā

Categorie:linux Tag:, , , , , , ,

mdb2mysql

febbraio 4, 2010 Commenti disabilitati

Ecco come convertire un file MS Access (mdb)  dentro un database mysql

1. Installare il pacchetto mdbtool e se non lo si ha già anche mysql

sudo aptitude install mdbtool mysql -y

2. Crearsi un utente mysql con username identico al profilo e disabilitare la password, questo server per effettuare le operazioni pi√Ļ velocemente senza digitare la password ogni volta. Ma non √® il massimo se lo vediamo dal punto di vista della sicurezza…

echo "CREATE USER '$USERNAME'@localhost IDENTIFYED BY '';" | mysql -u root
echo "GRANT ALL PRIVILEGES ON *.* TO '$USERNAME'@'localhost' | mysql -u root

3. Eseguire lo script mdb2mysql per convertire il file in una struttura SQL:

#!bin/bash
# Author: s@mba - GPLv3

usage(){
 if [ "$@" != "" ] ;then
 echo "$@"
 else
 echo "$0 [ option ] [ file.mdb ]
 Usage
 -help        Show this help
 -debug        Print in debug mode
 -view        Print only what to do
 -db        Set the new DB to create in mysql
 "
 fi
 exit 1
}
which mdb-schema 2>&1 /dev/null || usage "Before install mdbtool!"
which mysql 2>&1 /dev/null || usage "Before install mysql!"
[ $# -eq 0 ] && usage

while $[ $# -ne 1 ];do
 case $1 in
 -d|-db)    DBNAME=$1
 ;;
 -view)    VIEW=1
 ;;
 -debug)    DEBUG=1
 ;;
 *)    usage
 esac
 shift
done

[ $# -eq 0 ] && usage "Specify file.mdb !"

MDBFILE=$1    

[ $VIEW ] && echo "1. Get the schema and create in DB '$DBNAME'"
[ $DEBUG ] && mdb-schema $MDBFILE mysql 

# This works only if run mysql with your username and profile is NOPASSOWRD
mdb-schema es1.mdb mysql   | grep -vE "DROP|^-"  | mysql -f  $DBNAME

[ $VIEW ] && echo "2. Export data from $MDBFILE to $DBNAME"
for i in $(mdb-tables $MDBFILE);do
 mdb-export -I -R ';\n' $MDBFILE $i
done > $FILE_EXPORT
[ $DEBUG ] && cat $FILE_EXPORT

cat $FILE_EXPORT | mysql -f $DBNAME

[ $? -eq 0] && echo "Export Successfully!" || echo "Error: some problem in export operation"
exit 0

provare per credere…

Categorie:linux Tag:, , , , , , ,