0

This is part of a bash script code I wish to use multi-threading

for DATABASE in $DATABASES ; do
    echo Converting $DATABASE
    # Check if the table is MyISAM (we don't want to convert InnoDB tables over and over again)
    TABLES=$(echo "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA = '$DATABASE' and ENGINE = 'MyISAM'" | $MYSQL_COMMAND)
    for TABLE in $TABLES ; do
        echo Converting MyISAM $TABLE to InnoDB
        echo "ALTER TABLE $TABLE ENGINE = INNODB" | $MYSQL_COMMAND $DATABASE
    done
    if [ "x$TABLES" = "x" ] ; then
        echo No MyISAM tables found in $DATABASE database
    fi
    echo
done

Is it possible for FOR to use multi-threading?

I was thinking about rewriting the script and doing it with xargs -P3 -n1 -I{} and so on, but it looks dirty to me.

Luka
  • 2,117

1 Answers1

0

I did it!

function convert_db() {
    echo Converting $1
    # Check if the table is MyISAM (we don't want to convert InnoDB tables over and over again)
    TABLES=$(echo "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA = '$1' and ENGINE = 'MyISAM'" | $MYSQL_COMMAND)
    for TABLE in $TABLES ; do
        echo Converting MyISAM $TABLE to InnoDB
        echo "ALTER TABLE $TABLE ENGINE = INNODB" | $MYSQL_COMMAND $1
    done
    if [ "x$TABLES" = "x" ] ; then
        echo No MyISAM tables found in $1 database
    fi
    echo
}

for DATABASE in $DATABASES ; do
convert_db $DATABASE &
done
Luka
  • 2,117