Unix Shell Scripting – Introduction
#1 Identify Your Shell : Every shell is unique and identified with a unique qualifier. If you are writing a code for your korn shell, you should qualify your script to run in that shell. No exception, it’s a mandatory part. This has to be placed at the very beginning of the script. Example – For KSH below line qualify it to run in K shell or second sample is for b shell.
#2 Understand parameters / parameterize – Script is meant to preserve and reuse the lines of code. If your code is dynamic, you tend to pass the parameters during execution rather hard coding it. The parameters can be of any type – can be a filename, table name or a database name etc. So a decent understanding is required on how to pass and read parameters
- Consider script name is testscript.sh (I will be referring this script name multiple times in this post)
- Lets go with reverse understanding strategy; see below p1, p2, p3, p4 are all the parameter
- Calling at the time of execution: testscript.sh p1 p2 p3 p4
- How to read these parameters in the script? - $1, $2, $3, $4 holds the values of p1, p2, p2, p4 respectively.
- $0 – it hold the script name that is being executed (Here $0 = testscript.sh)
A catch – you don’t always have to pass the same number of parameters to your script; it can be built dynamic in terms of number of parameters; Use $# to find number of parameters and code your script to act unique based on the number of parameters you receive
#3 Environment settings, functions, variables – It’s a natural practice to store some project variables and functions that are reusable, for example the output file directory or a log file directory. They can be preserved in a file and can be called from the script rather coding them every time
- To call another file from a script (You need to use dot (.) and File_Name )
- . PROJ_FUNCTIONS_FILE . PROJ_VARIABLE_FILE
#4 Loops and If Else Blocks – I am not going add any huge information about loops as they are quite self explanatory in nature and similar to those traditional loops, They are the core of your script processing. You need to decide what will suit your needs best.
- IF Else
- Do while
- For Loop
#5 Validate Previous Step Execution – It is prevalent that you validate the execution status of your previously executed statement; Most of the times this status is a decision maker to proceed with the next execution. a script is a good script if it checks at each steps and log or exit the script with failure notification.
- $? – Gives the status of previously executed statement
- Possible values – 0 (success), 1 (Failure)
#6 Log capturing – Here comes another mandatory part that one can’t ignore; this could be achieved by simple redirection. Use redirection operators “>, >>” to redirect the output / error (can be get by giving option 2).
- Sample 1 >> file.log
- Sample 2 > file.error
#7 Exit – Never forget to exit your script. As you are already aware that every process is assigned a ID on the course of its execution, it is required to come out of the process when you are done.
- Exit 0 – post successful execution
- Exit 1 – Exit on Error
It was an attempt to describe Linux or Unix shell scripting. Comments are welcome.