LINUX SHELL PROGRAMMING(Shell Script)

Shell scripting

A shell script is a quick and easy  method of prototyping and automating a complex application.With Linux shell programming the structure of application is always tested and tinkered with major pitfalls before the final coding in C,C++,Perl,Python or Java.

Shell scripting follows the Philosophy of breaking task that are complex to small sub-task that are simple and can be chained together.These is considered a better approach to problem solving as compared to using the high-level,high-powered all-in-one languages which is seen by people as an all thing.A useful language should have arrays,pointers and generic mechanism used in building data structures.

A shell script basically can be referred as a list of system commands that have been stored in a file,so as to save the programmer the effort of re-typing these sequence of commands every time they have to be invoked.

Example 1

cleanup: A script to clean up log files in /var/log 

#!/bin/bash 

# Proper header for a Bash script. 

# Cleanup, version 2

 # Run as root, of course. 

# Insert code here to print error message and exit if not root. 

LOG_DIR=/var/log 

# Variables are better than hard-coded values.

 cd $LOG_DIR cat /dev/null > messages cat /dev/null > wtmp echo “Logs cleaned up.” exit 

# method of “exiting” from a script.

 #  “exit” (no parameter) returns the exit status 

 #+ of the preceding command.

The  ( #!) at the head of a script instructs your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is  a two-byte magic number, a special marker that designates a file type/an executable shell script.
The immediate line of commands following (#!) refers to the path name;these is the program that helps interpret the commands in the script either being a shell language,a programming language or a utility.The interpreter will execute the commands in these script beginning from the top and leaving

out the comments.
Example 2
#!/bin/sh
 #!/bin/bash
 #!/usr/bin/perl
 #!/usr/bin/tcl
 #!/bin/sed -f 
#!/bin/awk -f 

The line of code s listed above calls different command interpreters.when we use #!/bin/sh,default.

Bourne Shell is called and these is the most commercial that makes the script portable to other non-Linux machines.When writing scripts the path given should always be correct otherwise we are going to experience an error message like “Command not Found” .
When a script consist of generic system commands we can omit   #!,meaning no use of internal shell directives.

Example 2 requires  #! because the Variable uses  a shell specific construct.  #! invokes
the default shell interpreter of which on local machines it is default to /bin/bash on a Linux Machine.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.