Marius van Witzenburg We fight for our survival, we fight!

14Jun/111

How to convert a string to lower case or UPPER case with Bash

Posted by mariusvw

Ever had a situation where you want the output or input in a BASH script to be lowercase or uppercase whatever happens but you couldn't find a way how to do that? You can!

Simply, use these two functions in your script:

1
2
3
4
5
6
7
8
9
10
toLower() {
  echo $1 | tr "[:upper:]" "[:lower:]" 
} 
toUpper() {
  echo $1 | tr "[:lower:]" "[:upper:]" 
}
 
# Example
echo `toUpper hey`
echo `toLower HEY`

I hope this helps you out with writing your precious script ;-)

12Jun/110

How to convert MySQL databases to UTF-8

Posted by mariusvw

Convert databases to UTF8.

// Databases to convert to utf8
$databases = array(
    'database1' => array('localhost', 'username1', 'password'),
    'database2' => array('localhost', 'username2', 'password'),
    'database3' => array('localhost', 'username3', 'password')
);
 
foreach ($databases as $db => $db_info) {
    mysql_connect($db_info[0], $db_info[1], $db_info[2]);
    mysql_select_db($db);
    $result = mysql_query("SHOW TABLES");
 
    echo '<b>Database: ' . $db . '</b><br />';
 
    mysql_query("ALTER DATABASE " . $db . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
    while ($row = mysql_fetch_row($result)) {
        echo '-&gt; ' . $row[0] . '<br />';
        mysql_query("ALTER TABLE " . $row[0] . " CONVERT TO CHARACTER SET utf8;");
    }
}