Shell Scripting

PS : This page keeps on updating !

Shell scrip learning is easy and powerful programming method can help you save time, learn via command-line.If you already know C/Perl/Python programming language or any other programming language, learning the scripting language won’t be much difficult.

What Is Shell Scripting?
It is a series of command written in plain text file.The shell reads this file and carries out the commands as though they have been entered directly on the command line.

Process of writing and executing a script
A shell script is a file that contains ASCII text. To create a shell script, you use a text editor.
We can use terminal editor vi/vim or GUI editor gedit
We’ll be using the bash shell.Bash is available for Mac OS users and on Windows too. Since it’s so universal.

Basic Command Line Editing

You can use the following key combinations to edit and recall commands:

CTRL + L : Clear the screen.
CTRL + W : Delete the word starting at cursor.
CTRL + U : Clear the line i.e. Delete all words from command line.
Tab : Auto-complete files, directory, command names and much more.
CTRL + R : Search through previously used commands
CTRL + C : Cancel currently running commands.
CTRL + T : Swap the last two characters before the cursor.
ESC + T : Swap the last two words before the cursor.
CTRL + H : Delete the letter starting at cursor.

Each shell script consists of 
  • Shell keywords such as if..else, do..while.
  • Shell commands such as pwd, test, echo, continue, type.
  • Linux binary commands such as w, who, free etc..
  • Text processing utilities such as grep, awk, cut.
  • Functions - add frequent actions together via functions. For example, /etc/init.d/functions file contains functions to be used by most or all system shell scripts in the /etc/init.d directory.
  • Control flow statements such as if..then..else or shell loops to perform repeated actions.

Each script has purpose
  • Specific purpose - For example, backup file system and database to NAS server.
  • Act like a command - Each shell script is executed like any other command under Linux.
  • Script code usability - Shell scripts can be extended from existing scripts. Also, you can use functions files to package frequently used tasks.

Example #1


# My first script

echo "Good morning, world."

The first line of the script indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as perl, awk and python also use this mechanism.

The second line is a comment. Everything that appears after a "#" symbol is ignored by bash.As your scripts become bigger and more complicated, comments become vital.

The last line is the echo command. This command simply prints its arguments on the display.

Script Permissions
chmod +x OR chmod 755

Executing script
sh OR ./

Output :
Good morning, world.

Example #2


#! /bin/bash
echo "Hi $USER !"
echo "Running processes List"

The first line of the script indicating what program is used to interpret the script.
The second line tells you, your “username”
The third line just echos the command
The fourth line list the running processes.

Output :
Hello vivek !
Running processes List
1231 pts/0    00:00:00 bash
1247 pts/0    00:00:00 sh
1318 pts/0    00:00:00 ps

Example #3

This script acts as an interactive script.


#! /bin/bash
echo "What is your Name?";
read a;
echo "welcome Mr./Mrs. $a, May i know your Age please.";
read b;
echo "Thanks Mr./Mrs. $a / $b for telling us your name and age.";

Output :
What is your Name?
welcome Mr./Mrs. vivek, May i know your Age please.
Thanks Mr./Mrs. vivek / 22 for telling us your name and age.

Example #4
This script has User Defined Variable (UDV)

variable name=value
'value' is assigned to given 'variable name' and Value must be on right side.
Let’s give a variable name, like date_formatted to this command
variable=$(command –options arguments)


#! /bin/bash
date_formatted=$(date +%d-%m-%y:%H.%M.%S)
echo "Todays Date and Time is: " $date_formatted

Output :
Todays Date and Time is:  12-09-13:12.36.18

Example #5


#! /bin/bash
echo $1 $2

To run
./ one two

Output :
one two       // $1 has the first value which is "one" and $2 has the value "two"


#! /bin/bash
echo $2

To run
./ one two

Output :
two         // Here in script we echoed only the 2nd parameter so the output will be "two"

Hope u understand the Example#5 shown above

Here is the Practical Example for #5

#! /bin/bash
date_formatted=$(date +%d-%m-%y:%H.%M.%S)
mv $1 $2.$date_formatted

To run
./ one.txt two.txt  // Please note u have these one.txt and two.txt in your pwd.


Shell Scripting Shell Scripting Reviewed by vivek sathisan on 15:05 Rating: 5

No comments:

© Copyright to vivek sathisan. Powered by Blogger.