My very first bash shell script
So at work, I do some "glue" work. I'm learning all sorts of things at once. At any time during the day, I'm confident with what I'm doing or a humiliated greenhorn. Often times, where I fall short in my lack of knowledge, I shore up with a different area where I'm less dumber. It definitely makes for a rainbow of emotions.
Today I wrote my first bash shell script and it felt really good. The code is below, but here's what it does:
- Queries our dotCMS database to find where we have outstanding records (that's my Java greenhorns creating those records)
- If records are found:
- SQL dumps the 2 tables to my local Ubuntu desktop
- Restores the data into my local data base
- Sends mail making use of the "mailx" software to myself and our sysadmin, notifying him to access some PHP generated SQL to run against our production dotCMS database
The PHP was generated a while ago which parses pipe-delimited data in to an ad-hoc SQL query. This script runs hourly as a cron job.
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/bin/sh to="test@test.com" dbh="database host path" dbu="database user" dpw="database password" dbn="database name" ldbh="local database host" ldbu="local database user" lpw="local database password" ldb="local database name" mysql -N -h $ldbh -u $ldbu -p$lpw $ldb << EOT > /tmp/t$$ select blah blah from blah blah where blah blah = blah blah blah EOT VAR=`cat /tmp/t$$` if [ "$VAR" != "" ]; then echo "yes, we have unreported records\r\n$VAR" mysqldump --single-transaction -u $dbu -p$dpw -h $dbh $dbn table1 table2 > /tmp/tmp.sql mysql -u $ldbu $lbu -p$lpw $ldb < /tmp/tmp.sql echo "Sysadmin, we have unreported records:\n$VAR\n\nCan you please run the following SQL against the prod dotCMS db?\nhttp://xxxx/xxxx.php" | mailx -s "Unreported Records" $to fi |
My Pitch for Ubuntu Linux 10.04
For those of you out there interested in an alternative to Mac or Windows to run your computer (even on Apple computers), may I recommend Ubuntu Linux? Contrary to popular belief, installation is no longer a science project, but a 2-3 click breeze to install alongside or in place of Windows. I've been using it at work and home for a couple of years now. I've set up on a few friends' computers with fantastic results, each report they'd never go back to Windows (*Mac users are a slightly different story). There's a new version (10.04 - Lucid Lynx) coming out on April 29. Give it a try!
Why would you do this?
- It's free
- It's fast
- It's rock solid, it's based off of Unix which came out in 1969
- All software is free and very easy to install
- It's aesthetically pleasing
- No Viruses
- No Malware
- No Spyware
- No hassle (or expense) to upkeep virus software like Norton or McAfee
- Anything you can do on Mac or Windows you can do on Linux
- You're supporting open source software development by using it
Ubuntu Linux 9.10 Karmic Koala
Ubuntu Linux is coming out with a new version in a couple of weeks. Are you sick of Windows? Are you sick of, as Apple says, compromising? Are you sick of paying to compromise? Check out Ubuntu Linux. I've been using it as my primary operating system for a couple of years now and it's great.
- It's free.
- All of the software you want for it is free (office software, graphics software, email, music player, etc.)
- It's really easy to install, so is all of the software you get for it.
- You don't have to get rid of Windows, you can run Windows and Ubuntu on the same computer (but not at the same time). Very easy to use.
- No viruses, spyware, adware. Malicious websites don't affect your system.
- Unadulterated speed, this includes boot up and shutdown.
- It just works.