Compiling and uploading Arduino C code over serial in Linux

I had a spare hour this evening so I decided to flex my bash skills and write a script that will compile and upload code you've written in C for your Arduino. I happen to have the Arduino UNO, so you may have to look up the values that correspond to your microcontroller by referencing the AVR options and hardware specs from Atmel. I originally got this idea by reading a post by Balau.
#! /bin/bash
# Compile avr-gcc and upload via serial

# ensure we're root user
if [ $UID -ne 0 ]
then
    echo "Must be root" 1>&2
    exit 1
fi

# ensure we have the correct number of arguments - only the first is used
if [ $# -lt 1 ]
then
    echo "** Must submit file for compilation" 1>&2
    exit 1
elif [ $# -gt 1 ]
then
    echo "** Warning: only using first argument as file name"
fi

check_compilation()
{
    # check if there are input arguments use them as error message
    if [ $# -eq 1 ]
    then
        FNAME=$1
        if ! [ -e $FNAME ]
        then
            echo "** File $FNAME does not exist, exiting" 1>&2
            exit 1
        fi
    elif [ $# -gt 1 ]
    then
        # just use one
        echo "** Warning: next time just use 1 input for function \
check_compilation"
        check_compilation $1
    else
        echo "** please provide valid number of arguments" 1>&2
        exit 1
    fi
}

print_border()
{
    printf "\n"
    WIDTH=$(tput cols)
    for ((i=0; i<$(( $WIDTH-2 )); i=i+1))
    do
        printf "*"
    done
    printf "\n\n"
}

BASE_FILE_NAME="${1%.*}"     # kind of a redundant step for clarity
BASE_FILE_NAME_c="$BASE_FILE_NAME.c"
BASE_FILE_NAME_o="$BASE_FILE_NAME.o"
BASE_FILE_NAME_h="$BASE_FILE_NAME.hex"
DF_CPU="16000000UL"          # processor speed (MHz)
MCU="atmega16u2"             # look up option for the processor
SER_PORT="/dev/ttyACM0"      # serial port
BAUD_RATE="115200"

echo "** compiling $BASE_FILE_NAME_c"

if ! [ -e $BASE_FILE_NAME_c ]
then
    echo "** File $BASE_FILE_NAME_c does not exist, exiting" 1>&2
    exit 1
fi
avr-gcc $BASE_FILE_NAME_c -Os -DF_CPU=$DF_CPU -o $BASE_FILE_NAME_o -c -mmcu=$MCU

check_compilation $BASE_FILE_NAME_o
avr-gcc $BASE_FILE_NAME_o -o $BASE_FILE_NAME -mmcu=$MCU

# generate hexcode
check_compilation $BASE_FILE_NAME
avr-objcopy -O ihex -R .eeprom $BASE_FILE_NAME $BASE_FILE_NAME_h

# upload over serial
check_compilation $BASE_FILE_NAME_h
print_border
avrdude -F -V -c arduino -p ${MCU^^} -P $SER_PORT -b $BAUD_RATE -U \
    flash:w:$BASE_FILE_NAME_h
print_border

# cleanup
rm $BASE_FILE_NAME_o $BASE_FILE_NAME_h $BASE_FILE_NAME

echo "** finished compilation and upload"
exit 0
With this I simply compiled the LED blinking code in Balau's post. I'll be digging into these libraries in the coming months when I'm not working (I just got a job at Tops in the meat dept. :P). I'll reproduce the code here for ease of reading (it's a very generic example you can find on a number of different sites).
/* Make an LED blink on the Arduino UNO board (classic example) */

#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>

#define BLINK_DELAY_MS 100

uint8_t
main(void)
{
    DDRB |= _BV(DDB5);

    while (1)
    {
        // turn on
        PORTB |= _BV(PORTB5);
        _delay_ms(BLINK_DELAY_MS);

        // turn off
        PORTB &= ~_BV(PORTB5);
        _delay_ms(BLINK_DELAY_MS);
    }
    
    return 1;
}