Ugly Bash Scripting

I started down this rabbit hole tonight trying to make a bash script do more than I probably should make it do. But I didn’t want to move to something else. This may be some of the ugliest stuff I’ve ever written.

for file in $files; do
	if [ "$file" != "./00000000000001-tSQLVersions.sql" ]; then
		echo "Checking if $file is already loaded..."
		# oh this is ugly, oh so very very ugly
		# the tr on the end trims the newline out of the return from SQL
		check=`$MySQL -u$MyUSER -h$MyHOST -p$MyPASS $MyDB --execute="SELECT COUNT(*) FROM tSQLVersions WHERE message = '$file';" | tr -d '\n'`
		check=`echo -n "$check"`
		# ridiculously ugly hack to hardcode updater to always assume this special file isn't loaded

	if [ "$check" = "COUNT(*)0" ]; then
		echo "Loading $file..."
		$MySQL -u$MyUSER -h$MyHOST -p$MyPASS $MyDB < $file
		$MySQL -u$MyUSER -h$MyHOST -p$MyPASS $MyDB --execute="insert into tSQLVersions (message) values ('$file');"
		echo "File $file has already been loaded!"

The tr -d '\n' at the end of line 6 is my personal favorite. MySQL was dumping a newline in the STDOUT and screwing up my comparison. Fix it right? Nah. Just rip that newline out of there. All better!

I share this for others to know what not to do.

But hey, it works. :-) Who needs any fancy pants migrations.

| 2008 |