create your own

Unix Shell Scripting (Editor and Shell)

76
rate or flag this page

By mtsi1098


The Editor

From the operating system discussion (click link above) we know that there are many different operating systems but how to communicate with the OS depends on the type of OS you have.

DOS - There are at least two ways to communicate with Disk Operating System. You can have a file/script of DOS commands or a command line. The file/script would allow execution of one or more commands sequentially vs. a command line which allows execution of one command at a time.

Windows - In addition to the file/script and command line approach, Windows also has icons (representing one or more commands) to interact with the OS.

Unix - In addition to the file/script and command line approach, Unix with a GUI front end like X-Windows also has icons (representing one or more commands) to interact with the OS.

A OS script has command(s) or logic that enables the completion of one or more tasks. To create the file or script you would use a full screen editor supplied by the OS. If you are using DOS then the "ed" editor can be utilized to create a script. If you are using Windows then notepad can be utilized to create a script. If you are using Unix then the "vi" editor can be utilized to create a script. The editor you choose really depends on which one you are more comfortable with.

Editor

Learning the vi and Vim Editors Learning the vi and Vim Editors
Price: $19.08
List Price: $34.99
VI Editor Pocket Reference VI Editor Pocket Reference
Price: $4.41
List Price: $9.95

The Shell

Now that you are familiar with the OS and editor, it is time to think about the file/script that I have been discussing. Since I am most comfortable with the Unix and vi combination, I will use these tools as my preference for creating a file/script.

A shell is defined as a program that accepts operating system commands and submits them for execution.

In Unix, the file or script of commands is also known as a shell script and is created using the language of a particular shell. A shell script is executed under a shell. A Unix shell is a command line interpreter which passes commands to the OS for execution.

Unix has basically three different shells to work with - there is the borne shell (sh), c-shell (csh) and korn shell (ksh), With the creation of Linux there is also the borne again shell (bsh). Some shells are more optimal than others and some are easier to work with than others but all shells have their own syntax and structure requirements.

I have worked with many different Unix shells and prefer the ksh to perform tasks. The ksh is a combination of csh and sh languages and, in my, opinion is the most flexible.

The Shell Structure

I cannot discuss the ways to create a ksh because everyone has their own style when it comes to developing in a language. I can, however, discuss the components that I feel should be included in each ksh.

The first line of every script should indicate the shell you are using. The line would look something like this - /usr/bin/ksh. This tells Unix what shell to execute the shell script under. The "/usr/bin" is the location of the shell.

The next few lines should be comments that describe the reason for creating the shell script, including, but limited to, author, date, time and version control information.

I prefer the structured well documented approach when it come to shell scripting that allows flexibility it terms of recovery, rerun, restart. Functions that error trap and log every command are ideal since you can build in a restart point based on the command last executed. Use plenty of variables to eliminate hard coding which enables portability.

Once the shell script has all the logic necessary to complete a task it is ready to be sent to the OS for execution. The execution of a shell script means that syntax is checked as the script executes (it is interpreted line by line, not compiled into an executable). If there are no syntax errors then you determine if the logic acted as you expected and thus the word testing is born.

#! /usr/bin/ksh
###########################################
# Script Name..law_env.sh
# Author...........hubber
# Date..............December 2008
# Summary.......This script was designed...
# Parameters....
# Version History...
# MODIFICATIONS...
# Name Date Description
###########################################

HI() # Function

{

echo "Hello mtsi1098" >> message.log # Log to file

}

v1=`date +%Y%m%d`
v2=`date +%H%M%S`
v3=`date +%H%M`
stat=0

HI; # Function call

stat=$? # Error Trap

exit $stat

Comments

RSS for comments on this Hub

frogdropping profile image

frogdropping  says:
3 months ago

Oh my ... I started to read this then certain words started appearing. Like 'windows', 'dos' and 'unix'. I have absoltuely no ability to retain such info and you wrote it all so well. My mind is naughty. If you could change a few words into 'jam', 'coffee' or even 'donuts' I'd remember this a lot better. I'm assuming then that you know a thing or three about the inner workings of computers?

I know a little bit but I can't seem to access it. My hard drive is broken. I am seriously impressed by your abilities. I'm going to read something else of yours now :)

mtsi1098 profile image

mtsi1098  says:
3 months ago

thanks for the input and hopping by :) I will try the jam, coffee and donuts...

frogdropping profile image

frogdropping  says:
3 months ago

You should try all three in your mouth as well. At the same time. At least once in your lifetime. It's a great experience, honest :)

mtsi1098 profile image

mtsi1098  says:
3 months ago

ok - so I take a bite of a donut (with jam on it) then take a sip of coffee :)

Portamenteff profile image

Portamenteff  says:
3 weeks ago

Although you may know this mts!1098, I thought it should be mentioned that a script is simply a program or module of a program that is not compiled. It is parsed into machine language at runtime. This is convenient for me anyway because debugging can happen much more rapidly. The code can be passed to other scripters to work on it, run it as is without all the "make" and "make install" and linking rigmarole.

mtsi1098 profile image

mtsi1098  says:
3 weeks ago

Portamenteff - I understand the shell and would like to provide this supplement to your explanation. As a command line parser of OS commands, the shell is responsible for executing OS commands and parsing them line by line to indicate if these commands will pass the OS syntax requirements. It would be nice if the entire pasring could take place and have the OS indicate all errors one time and not in sequence until all are corrected...thanks

motricio profile image

motricio  says:
3 weeks ago

Very technical, and very well written.

Most to say that typing Systems terminology to users clearly is one of the most tough challenges we need to deal as Architects.

Indeed, it is great to find hubs about Unix, OS and many other IT stuff.

Thnx for writing, keep it up!

mtsi1098 profile image

mtsi1098  says:
3 weeks ago

motricio - thanks for your input as it is always difficult to translate between technical and user language...

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working