Unix Shell Script Structure

September 15, 2007 by Mark Marucot · Leave a Comment 

The shell script structure consists of header, declaration and body. These three sections are basic structure of shell script. But script can run with header and body section only. Declaration section is useful for reading and passing values within the script.

Read more

Unix Shell Script Creating Functions

June 15, 2007 by Mark Marucot · Leave a Comment 

Functions are named code blocks that can be referenced and executed as a unit. Functions have an option to take variable number of arguments and may return a value. If function returns a value, calling the function is usually part of an expression.

Syntax

CODE:
  1. name() {
  2. statements
  3. }

Example

CODE:
  1. #!/bin/sh
  2. sum() (
  3. echo $(($A + $B))
  4. )
  5. A=2
  6. B=5
  7. sum $A $B

Unix Shell Script Looping

June 15, 2007 by Mark Marucot · Leave a Comment 

Loops are use to perform certain task multiple times. Using loops helps minimize placing code multiple time performing similar functions. Loops in Unix scripting uses for and while loops.

Do While Loop

The Do While loop takes the following form:
Syntax

CODE:
  1. while [ condition ]
  2. do
  3. # statement
  4. done

Example

CODE:
  1. #!/bin/sh
  2. count=$1 # Initialise count to first parameter
  3. while [ $count -gt 0 ] # While count is greater than 5 do
  4. do
  5. echo “Loop $count” # Display Loop
  6. count=$(expr $count -1) # Decrement count by 1
  7. done

For While Loop

The For loop takes the following form:

CODE:
  1. For variable in word
  2. do
  3. # statement
  4. done

Example

CODE:
  1. #!/bin/sh
  2. for count in 1 2 3 4 5
  3. do
  4. echo “Loop $count” # Display Loop
  5. done

Unix Shell Script Case Statement

May 15, 2007 by Mark Marucot · Leave a Comment 

The case statement is similar to if .. else if .. else statement which performs specific statement/s if there are three or more conditions.
Syntax:

CODE:
  1. case word in
  2. value1) statements
  3. value2) statements
  4. *) statements
  5. esac

Example:

CODE:
  1. !#/bin/sh
  2. case $1
  3. in
  4. 1) echo ‘First Choice’;;
  5. 2) echo ‘Second Choice’;;
  6. *) echo ‘Other Choice’;;
  7. esac

Unix Shell Script Operators

May 15, 2007 by Mark Marucot · Leave a Comment 

The table below shows the list of operators used in conditional statements in Unix scripting.

Operator Description Example

Unix Shell Script Conditional Statements

April 15, 2007 by Mark Marucot · Leave a Comment 

Conditional statements are use to perform specific action/s based on a given conditions
if Statement

The if statement performs specific statement/s if the condition is met otherwise the set of statements is skipped.

CODE:
  1. if [ condition ]
  2. then
  3. # if-code
  4. fi

Wrong

CODE:
  1. if [$foo = "bar" ]

Correct

CODE:
  1. if [ "$foo" = "bar" ]

Note: Bracket ([) is actually a program which means it is required to place spaces in between the condition because it reads the next group of words or letters as arguments or program parameters. Syntax should be if [ space <var1> space <operator> space <var2> space ]

if .. else Statement

The if .. else statement performs specific statement/s if the condition is met otherwise the another statement/s is performed.

CODE:
  1. if [ condition ]
  2. then
  3. # if-code
  4. else
  5. # else-code
  6. fi

if .. else if .. else Statement

The if .. else if .. else statement performs specific statement/s if there are three or more conditions.

CODE:
  1. if [ condition1 ]; then
  2. # if-code
  3. elif [ condition2 ]; then
  4. # if-code
  5. else
  6. # else-code
  7. fi

Unix Shell Script Variables

April 15, 2007 by Mark Marucot · Leave a Comment 

Variables are use to store information. The syntax of declaring variables is shown below:

CODE:
  1. VAR=value

Note: There must be no spaces between the variable and the value. VAR=value will work but VAR=value will not. The reason is when the shell sees that the “=”, it assumes that it’s a variable assignment. If there is spaces before and after the “=”, it interpreted VAR as a command with two arguments “=” and “value”.

CODE:
  1. #!/bin/sh
  2. MESSAGE=“Hello Unix”
  3. echo $MESSAGE

Read more

Unix Shell Scripts Introduction

March 15, 2007 by Mark Marucot · Leave a Comment 

A shell script is a script written for the shell or command line interpreter of an operating system like Unix. This script contains list of commands that are run in sequence.

History of Shell Scripting

Steve Bourne create the first Unix shell designed for scripting named Bourne shell (sh) which appeared in the Seventh Edition Bell Labs Research version of Unix. It remains the standard command line interface to Unix. The first Unix shell was Thompson shell written by Ken Thompson in the first version of Unix in 1971.

Read more

Oracle + Linux = Oracle Unbreakable Linux

October 15, 2006 by Mark Marucot · Leave a Comment 

There are many humors last year about Oracle distributing Linux own version to its clients such as Oracle - Ubuntu tie-up and Oracle’s plan in redistributing Red Hat Linux. Now the concrete plan has been manifested and Oracle launched Oracle Unbreakable Linux.

Oracle Unbreakable Linux is Oracle’s brand around its global support offerings for Red Hat Linux and the completely compatible Oracle Enterprise Linux. The Oracle Unbreakable Linux Support Program provides enterprises with industry-leading global support for Linux and was developed in response to customer demand for true enterprise-quality Linux support and the need to significantly reduce IT infrastructure costs. Oracle is committed to delivering high quality, comprehensive and integrated support solutions to help drive the adoption of Linux in the enterprise. In addition to this support offering, Oracle continues to enhance Linux capabilities in the enterprise, most recently with the addition of the Oracle Management Pack for Linux and provides on-going contributions to the community.1

Read more