Linux Shell Programming

A shell script is a quick and easy  method of prototyping and automating a complex application.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 Phylosophy of breaking task that are complex to small subtask that are

simple and can be chained together.These is considerd 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 usefull languange should have arrays,pointers and generic mechanism used in building data


A shell script basically can be reffered 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.

How Linux Works, 2nd Edition: What Every Superuser Should Know

Example 1

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


# 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. 


# 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/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

Your email address will not be published.

Previous Post

java programming Basic

Next Post

DevOps engineer’s New Certification

Related Posts