Shell Scripting 01

I decided that I had better touch base with shell scripting fundamentals. It has been a year since I last worked with it and I don’t want to loose the skills.

Lets start off with something really simple.

A script that creates 5 files if they do not already exist.

#!/bin/bash 
for i in 1 2 3 4 5; do     
    fileName=$i.txt 
    echo "Processing $fileName"   
    if test -e $fileName; then 
        echo "$fileName already exists!" 
        echo "Skipping..." 
        echo " " 
    else 
        echo "Creating $filename" 
        touch $fileName 
        if test -e $fileName; then 
            echo "$fileName created successfully." 
            echo " " 
        fi 
    fi 
done

Stay tuned for another installment, I am thinking that next time we will try traversing a directory say using the find command and back up certain files based on attributes eg file extension, size, owner etc. To do this we will really only need to add a few lines to the script above… or do we? Perhaps we will end up with even less lines of code. Later on we will also start breaking up the code into functions and call them from a master script. Fun times ahead.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s