Shell scripts are a simple way of completing (repetitive) tasks on the command line, much like many programming languages will be used. You will see ~ something — ignore the ‘~’ this is to signify that it is a command entered onto the command line itself.

Writing the code

~ vi

# My first script
MESSAGE="Hello World!"

Here we have just entered a really simple example of using variables and returning the value.

Setting the permissions

We need to set permission to allow the script to be executed, this is needed to be done on all unix environments for something to be run

~ chmod +x

Running the script

~ sh

Hello World!

Now you should have the response “Hello World!” from running this script.

Going more advanced

Lets explore some of the basic options we have to play with in the shell by example.

Loop test

~ vi

# My first loop
FILETYPES="xml html css js"
        echo 'Looking for' $TYPE 'files'
        find . -name '*'$TYPE

Now set the correct permissions and run the script

~ chmod +x
~ sh

Looking for xml files
Looking for html files
Looking for css files
Looking for js files

If conditional test

~ vi

# If the file does not exist, make it
if [ ! -f $FILE ] ; then
    mkdir -p $FILE
ls -las $FILE

Now set the correct permissions and run the script

~ chmod +x
~ sh

Hopefully this is enough to help you on your way to making more use of the powers of Unix…

Further reading

Bash Manual
Useful bash one liner scripts