Tuesday, November 15, 2011

Programming in Unix 101

Let's go over a few programming basics, as we haven't gotten a proper review of them in class just yet. A program contains variables, parameters and arguments that can be acted upon by commands. For example:

Command Parameter Argument
ls -l name

This would return a list of files in long format containing the word name in the title. The argument, in this instance "name", can also be replaced by a variable, which would be represented by a $ followed by the variable name, such as $NAME.

A variable is merely a container for something, such as a string (ie, a word) or an integer (a number). Strings cannot be added, subtracted, etc, but integers can. (Note: an integer is a whole number, without a decimal point.) To create a string variable, simply decide on a name and tell it to store something.

NAME="Jason"

However, to create an integer variable, it needs be made into the type integer. This command should work better:

typeset -i AGE=28

These can be handy, but don't do much sitting out in the open without a script file. Creating a script file is as easy as making a file executable and editing the contents with an editor like vi. The easiest way to create a file to edit with vi is like this:

vi filename

Make sure you're in the directory you want to create the file in, or
specify the directory you want to use when typing the command (vi
folder/filename). Now inside vi, it's time to create a script. (In vi,
press i to use input mode, and escape to enter command mode.) It's a
good idea to start the script with this:

#!/bin/bash

The # indicates a comment, and will not run a command, but this special
line also tells the shell to run this in the bash or Bourne Again Shell,
so that any commands used in the script will work like normal to the
shell used in class. The next few lines should contain some information
about the file for future editing purposes:

#
#Script by Jason Groce
#This is an example script
#Created 11/15/2011
#Last Edited 11/15/2011
#

Now let's get to programming. We need a function for this script to
accomplish. How about finding out how old you are in seconds? For
this, we need to ask the user some information and act upon that
information. Let's get the name and birthdate for the user first:

echo
echo "What is your name? "
read yourname
echo ""
echo "What is your birthdate? (yyyymmdd)"
read bday
echo ""

Now it's time for some crazy math fun (okay, this is a bit out there, sorry):

#Seconds elapsed
typeset -i seconds=$(date +%s)-$(date -d $bday +%s)

Fairly precise, this subtracts the length of time between the creation of Unix and the $bday variable from now. And finally, to display the output of the script:

echo "Hello, $yourname, you are $seconds seconds old!"

There we have it! Save the file (in vi, use shift+Z+Z to save and close or :w filename to save as a new file), and run the script:

[f52560@bearybear ~]$ ./filename

What is your name?
Jason Groce

What is your birthdate? (yyyymmdd)
19830221

Hello, Jason Groce, you are 906751610 seconds old!

The reason for the ./ is because the $PATH variable used to identify the default location of programs does not include the path to the script just created. (Remember figlet?)

Enjoy, script-kiddies!

No comments:

Post a Comment