Learn unix shell scripting: The Basics

We have already written about Unix Shell scripting ,What is its uses, what are various type of shells and what are positional parameters

What is Shell and Shell Scripts

In this post, I will  write about unix shell scripting basics and creating first shell scripts for the beginners and how to learn unix shell scripting

learn unix shell scripting

First of all unix shell scripting basics

Scripts must contain complete documentation
Each script must contain internal comments that document
The name of the script
The purpose for the script
The name of the author(s)
The date the script was written
The date(s) the script was modified.
The name of each script must reflect its use
Each script must have a suffix that describes the script, for example “.sh” for Bourne shell scripts, “.ksh” for Korn shell or “.cgi” for Common Gateway Interface scripts.
Lets create the first shell script

#!/bin/ksh

# This script displays the date, time, username and
# current directory.
echo “Current date and time is:”
date
echo
echo “Your username is: `whoami` \\n”
echo “Your current directory is: \\c”
pwd

echo “Your system name is :`hostname`\\n”

 

 

Explanation of each line in the scripts is given below

  1. #!/bin/ksh as the first line of the script .It is used to tell the script to execute with ksh shell

2. The second two lines beginning with a hash (#) are comments and are not interpreted by the shell.

3.Use comments to document your shell script.

4. The backquotes (`) around the command whoami illustrate the use of command substitution.The command whoami will be executed and output will be printed at that position. Whoami tells the user information

5. The \\n is an option of the echo command that tells the shell to add an extra carriage return at the end of the line.

6.The \\c tells the shell to stay on the same line.

7.  hostname command gives the hostname of the system

Related articles

if condition in unix shell script

Writing loop statement in Unix shell scripting with examples

Functions in Shell Script with some practical examples

Useful Advanced shell scripting examples