Archive for the ‘Shell Scripting’ Category

WordPress Auto-Update Script For A Linux Server

Monday, March 16th, 2009

I wrote a little update script for my server to auto update my wordpress installation and figured it might help some other people as well. To use it you simply supply as arguments to the script three things:

1. the directory where your wordpress install lives
2. the name of the backups you’d like to create for that directory
3. the version of the wordpress install you’d like to upgrade to

For example, on my server I use this command to update my wp version:

sudo ./ /path/to/wordpress/installation efnx 2.7.1
[enter password]
[watch output]


Here is the code to my script:


echo "Beginning update of $DIR2UPDATE to version $VERSION..."

if [ -d wordpress_svn ]
    day=`date | cut -d" " -f3`
    tme=`date | cut -d" " -f4`
    hour=`echo ${tme} | cut -d":" -f1`

    fileday=`ls -lh | grep wordpress_svn | cut -c 37-39`
    filehour=`ls -lh | grep wordpress_svn | cut -c 40-41`

    if [ ${fileday} != ${day} ]; then
            echo "SVN directory not up to date, [ file's date ${fileday} != today ${day} ] deleting and updating"
        rm -rf wordpress_svn/*
        cd wordpress_svn
            svn co${VERSION} .
            if [ "$filehour" != "$hour" ]; then
                    echo "SVN directory not up to date, [ file's hour ${filehour} != now ${hour} ] deleting and updating"
                rm -rf wordpress_svn/*
                    cd wordpress_svn
                    svn co${VERSION} .
                    echo "SVN directory is up to date, skipping update"
                cd wordpress_svn

    echo "Creating new svn directory and checking out version $VERSION...";
    mkdir wordpress_svn;
    cd wordpress_svn;
    svn co${VERSION} .;

cd ..

if [ -d wordpress_int ]
    echo "Removing old intermediate container...";
    rm -rf wordpress_int;

echo "Creating new intermediate container..."
mkdir wordpress_int
echo "Moving version $VERSION files into intermediate container..."
cp -rpf wordpress_svn/* wordpress_int

echo "Moving config and custom files from $NAME into intermediate container..."

cp -p ${DIR2UPDATE}/wp-config.php wordpress_int
cp -rpf ${DIR2UPDATE}/wp-content/* wordpress_int/wp-content/
cp -p ${DIR2UPDATE}/.htaccess wordpress_int
echo 'Updating svn for wp-content'
svn update wordpress_int

echo "Backing up $DIR2UPDATE..."
mkdir ${NAME}_backup
#mv -f ${DIR2UPDATE}/* ${NAME}_backup   # if you'd like to mv instead of cp
cp -rpf ${DIR2UPDATE}/* ${NAME}_backup
tar -czvf ${NAME}_backup.tar.gz ${NAME}_backup
rm -rf ${NAME}_backup

echo 'Removing svn data from intermediate container...'
rm -rf `find wordpress_int/ -type d -name .svn`
echo "Moving intermediate container contents to $DIR2UPDATE..."
cp -rpf wordpress_int/* ${DIR2UPDATE}

Or you can just download the script here [rightclick + 'save as']->

Formatting gcc, g++ Output Using PHP In TextMate

Saturday, September 20th, 2008

While at ZendCon08 I saw a lot of people sharing the same love for TextMate as myself. I fricken love TextMate. Some of my friends laughed at me, saying “Bah, TM’s not an IDE, why do you want to write shell scripts to try to make it one?” And I’d be like, “I’m an ActionScript developer, there’s no point in using a sledge hammer to pound in nails.” This makes sense because my friends use Eclipse. I tried Eclipse but it’s so huge and the plugins are huge and the interface is cluttered and theming syntax-coloring is a pain. So when I got home from ZendCon, with my renewed love for PHP, and my continual quest to eventually be a good C programmer, I decided to write a gcc parser script that linked back into TextMate to go directly to my errors. I use a dark syntax theme, a slightly modded Amy, and the parser reflects that. Check out a picture of these gcc errors:

Picture of colored gcc output.

Picture of colored gcc output.

To do this you’ll need PHP installed on your machine. After that, the first step is to create a new command in TextMate’s C Bundle. Call it “Build, Format With PHP,” or anything else. Set it to save all files in the project when run [from the pull down at the top] now enter this into the text area:

export FILE=`basename ${TM_FILEPATH}`
~/Library/Application\ Support/TextMate/Support/bin/

Next select Output as “Show as HTML” so we can see our output. Now create a file called “” in the folder ~/Library/Application Support/TextMate/Support/bin/
Once you’ve created the file, use TM or vim to fill it with this:

make &> ${FILE}.mkout
php -f ~/Library/Application\ Support/TextMate/Support/bin/gcc_format.php ${FILE} ${DIR}

This bash script executes the make on the makefile and directs the standard and error output to the same file. Then the script calls php to parse and display the file. So now create a file named “gcc_format.php” in the same folder as and fill it with the meat of our parser:


    // get stdout and stderr then delete temp file
    $dir = $argv[2];
    $commandFile = $argv[1] . '.mkout';
    $commands = file_get_contents($commandFile);
    // break lines up by line ending
    $lines = explode("\n", $commands);
    // find errors and link them to pages and line number
    $errors = 0;
    for ($i=0; $i < count($lines); $i++)
        $fnpos = strpos($lines[$i], ':');
        $filename = substr($lines[$i], 0, $fnpos);
        $lnpos = strpos($lines[$i], ': error:');
        $linestart = $lnpos - $fnpos - 1;
        if($linestart < 0) continue;
        $line = substr($lines[$i], $fnpos+1, $lnpos - $fnpos - 1);
        $link = "txmt://open/?url=file://$dir/$filename&line=$line&column=1";
        $lines[$i] = substr_replace( $lines[$i], '</a>', $lnpos, 0);
        $lines[$i] = "<a href=\"$link\">" . $lines[$i];
    $output = implode("<br />\n", $lines);
    echo '
        <style type="text/css">
        body    {background:#000000; color:#999999;}
        #comment{width:100%; background: #230021; color:#6060BF;}
        #fail   {width:100%; background:#3E0018; color:#9918B8;}
        #succ   {width:100%; background:#230021; color:#B0FFF0;}
        dslsh   {color:#A96AA9;}
        a:link  {color:#008080;}
        a:hover {color:#80A0FF;}
            <div id="comment">
                <dslsh>//</dslsh> '
. $argv[1] .'\'s build results:
    echo $output . '<br />';
    if($errors > 0)
        echo '<div id="fail">Build failed with '. $errors .' errors.</div>';
        echo '<div id="succ">Build Succeeded!</div>';

This example assumes that you are working in a directory with a makefile, but I’m sure it can be easily modded to work with any build script. The colors reflect my favorite theme, but they should also be easily modded with lines 32-38 of the php script. Happy coding, peoples!

Auto Comment Block In TextMate

Monday, January 21st, 2008

If you haven’t heard of TextMate go check it out it’s sweet. It allows a helluva lot of automation and is damn near the only thing you need for development of any language [minus compilers]. I’m about ready to throw away my Flash CS3 IDE I got for free from my day job and shell out the 39 pounds to buy it [those of you who know me personally know I don't ever buy software].

So, here’s a little command I wrote in the form of a bash script [I think all textmate commands are bash scripts or cocoadialog thangs]. The script takes selected input and pads it with “/* */” if it isn’t already, effectively commenting the text or it strips the comment block. I don’t write shell scripts often, so I like it when I get one that’s really useful.


# Make comment / Undo comment
# by Schell Scivally
# @

opening=`echo "$TM_SELECTED_TEXT" | awk 'BEGIN {nlines=0} /\/\*/ {nlines++} END {print nlines}'`
if [[ "$opening" == 0 ]];
        echo -n "/*$TM_SELECTED_TEXT*/"
        output=`echo "$TM_SELECTED_TEXT" | sed 's/\*\///g' | sed 's/\/\*//g'`
        echo -n "$output"

Follow me on GitHub
Follow me on Google+
Follow me on Twitter