0% found this document useful (0 votes)
3 views

121-Linux Shell Scripting

This document provides an introduction to Linux OS and shell scripting, highlighting its characteristics, uses, and the role of the kernel. It explains the concept of shell scripts, their benefits, and how to create and execute them using the bash shell and vi text editor. Additionally, it covers the basics of using variables and commands within shell scripts, along with practical examples and exercises.

Uploaded by

alborzjfrnk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

121-Linux Shell Scripting

This document provides an introduction to Linux OS and shell scripting, highlighting its characteristics, uses, and the role of the kernel. It explains the concept of shell scripts, their benefits, and how to create and execute them using the bash shell and vi text editor. Additionally, it covers the basics of using variables and commands within shell scripts, along with practical examples and exercises.

Uploaded by

alborzjfrnk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Shell Scripting

Session 01

Vahab Shalchian (ITIL v3 , LPIC-1 , LPIC-2 , LPIC-3)


Introduction to
Linux OS
Linux is one of the most famous pieces of FOSS software. Linux, also
sometimes called GNU/Linux, is a computer operating system, like
Microsoft Windows or Apple Mac OS X. Unlike these other operating
systems, Linux is free.

You can use Linux as Server OS or as stand alone OS on your PC. (But
it is best suited for Server.) As a server OS it provides different
services/network resources to client. Server OS must be:
 Stable
 Robust
 Secure
 High Performance
Introduction to
Linux OS
Linux offers all of the above characteristics plus its Open Source and
Free OS. So Linux can be used as:

On stand alone workstation/PC for word processing, graphics,


software development, internet, e-mail, chatting, small personal
database management system etc.
Introduction to
Linux OS
In network environment as:
• File and Print or Application Server to hare the data, Connect the
expensive device like printer and share it, e-mail within the
LAN/intranet etc are some of the application.
Introduction to
Linux OS
• Linux sever cab be connected to Internet, So that PC's on intranet
can share the internet/e-mail etc. You can put your web sever
that run your web site or transmit the information on the
internet.
Introduction to
Linux OS
So you can use Linux for:

• Personal Work
• Web Server
• Software Development Workstation
• Workgroup Server
• In Data Center for various server activities such as FTP, Telnet,
SSH, Web, Mail, Proxy, Proxy Cache Appliance etc
Kernel
What Kernel Is?

Kernel is hart of Linux OS. It manages resource of Linux OS.


Resources means facilities available in Linux. For e.g. Facility to store
data, print data on printer, memory, file management etc. .
Kernel decides who will use this resource, for how long and when. It
runs your programs (or set up to execute binary files).

It performs following tasks :


 I/O management
 Process management
 Device management
 File management
 Memory management
Kernel

What Kernel Is?

The kernel acts as an


intermediary between the
computer hardware and
various programs
/application/ shell.
Shell

What is Linux Shell ?


Shell is a user program or it's environment provided for user
interaction. Shell is an command language interpreter that executes
commands read from the standard input device (keyboard) or from
a file. Shell is not part of system kernel, but uses the system kernel
to execute programs, create files etc.
Several shell available with Linux including:
Shell Name Remark
BASH ( Bourne-Again SHell ) Most common shell in Linux. It's Freeware shell.
CSH (C SHell) The C shell's syntax and usage are very similar to
the C programming language.
KSH (Korn SHell) A programming shell compatible with the Bourne shell but supporting
advanced programming features like associative arrays and floating-point
arithmetic
TCSH TCSH is an enhanced but completely compatible version of the Berkeley
UNIX C shell (CSH).
Shell

To find all available shells in your system type following command:


$ cat /etc/shells
SHELL environment variable shows your default shell after login :
$ echo $SHELL
To find your current shell first find its process id :
$ echo $$
13285
Then you can look this PID up in ps command output :
$ ps
PID TTY TIME CMD
13102 pts/0 00:00:00 bash
13285 pts/0 00:00:00 ksh ------> This is your current shell
13286 pts/0 00:00:00 ps
Shell

With ps command we can see list of processes from other users as


follows :

Unix-style :
$ ps -ef

BSD-style
$ ps -aux
Shell

How to use Shell


You start to use your shell as soon as you log into your system.
To use shell you have to simply type commands.

For example :
$ date
$ pwd
$ ls
$ uname –a
$ who
$w
Shell Script

What is Shell Script ?


Normally shells are interactive. It means shell accept command
from you (via keyboard) and execute them. But if you use command
one by one (sequence of 'n' number of commands) , the you can
store this sequence of command to text file and tell the shell to
execute this text file instead of entering the commands. This is
known as shell script.
Shell script defined as:
"Shell Script is series of command written in plain text file. Shell
script is just like batch file is MS-DOS but have more power than the
MS-DOS batch file."
Shell Script

Why to Write Shell Script ?

 Shell script can take input from user, file and output them on
screen.
 Useful to create our own commands.
 Save lots of time.
 To automate some task of day today life.
 System Administration part can be also automated.
Getting Started

In this course we are going to use following items :

• A Redhat-based distributions like RHEL and CentOS


• “bash” shell
• vi text editor
vi text editor

vi is the most common text editor on Linux and Unix operating


systems. It is the editor of choice for many developers and power
users.
An improved version of vi named “vim” is available in some
distributions which adds a great deal of functionality and
extensibility that are missing from the original vi.
vi text editor

The vi editor has three modes, command mode, insert mode and
command line mode.
Command mode: letters or sequence of letters interactively
command vi. Commands are case sensitive. The ESC key can end a
command.
Insert mode: Text is inserted. The ESC key ends insert mode and
returns you to command mode. One can enter insert mode with the
"i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open
new line after current line) or "O" (Open line above current line)
commands.
Command line mode: One enters this mode by typing ":" which
puts the command line entry at the foot of the screen.
vi text editor

Some Important “command mode” commands


Keystrokes Action
i Insert at cursor
a Append after cursor
u Undo last change
o Open a new line
dd Delete line
3dd Delete 3 lines
x Delete character at cursor
R Overwrite characters from cursor onward
/search_word Searches for the search_word
n Find next occurrence of
N Find previous occurrence of search_word
Editing text files with
vim
Some Important “command line mode” commands
Command Action
:set nu / :set nonu Display/Don’t display line numbers
:w Save the file to its original location without quitting
:w [filename] Save the file to a new location without quitting
:q Quits the editor without saving the file
:q! Quits the editor without saving the file when there was a
change
:x Saves the file and exists
:s/string1/string2/g Replaces string1 with string2 in the current line
:M,Ns/string1/string2/g Replaces string1 with string2 from line M to line N
:%s/string1/string2/g Replaces string1 with string2 all over the file
Getting Started

How to write shell script


Following steps are required to write shell script:
1. Use any editor like vi to write shell script.
2. After writing shell script set execute permission for your script as
follows
$ chmod [permission] your-script-name
Examples:
$ chmod +x your-script-name
$ chmod 755 your-script-name
Getting Started

3. Execute your script


syntax:
$ bash your-script-name
$ sh your-script-name
./your-script-name

Example:
$ vi first
#My First Script
echo “Hello World”

Now execute it as follows :


$ ./first
Shell Scripting Basics

Using Multiple Commands

If you want to run two commands together, you can enter them
on the same prompt line, separated with a semicolon:

$ date ; who
Thu Nov 1 11:58:14 IRST 2012
root pts/0 2012-11-01 11:44 (5.39.11.10)
v.shalchian pts/1 2012-11-01 11:51 (151.241.125.69)
Shell Scripting Basics

Specifying the shell in script file

When creating a shell script file, you must specify the shell you
are using in the first line of the file. The format for this is:
#!/bin/bash

A comment line in a shell script isn’t processed by the shell.


However, the first line of a shell script file is a special
case, and the pound sign followed by the exclamation point tells
the shell what shell to run the script under
Shell Scripting Basics

Using Variables

A shell script allows you to set and use your own variables
within the script. Setting variables allows you to temporarily store
data and use it throughout the script, making the shell script more
like a real computer program.

Variables are case sensitive, so the variable Var1 is different from


the variable var1
Shell Scripting Basics

Values are assigned to user variables using an equal sign. No


spaces can appear between the variable, the equal sign, and the
value

 Assigning value
Here are a few examples of assigning values to user variables:
var1=10
var2=-57
var3=testing
var4="still more testing“

The shell script automatically determines the data type used for
the variable value.
Shell Scripting Basics

 Using variable value

You can use the value of variable by variable’s name preceded by


a dollar sign

echo $var1
var2=$var1
Shell Scripting Basics

The backtick

The backtick allows you to assign the output of a shell command


to a variable.
You must surround the entire command line command with
backtick characters:

TODAY=`date`
echo $TODAY
or
echo `date`
Shell Script

Exercise :
Write a shell script to
- Print a welcome message to the currently logged-in user
- Print the current date with proper message
- Print the number of currently logged-in users with proper
message

#!/bin/bash
echo “Welcome `whoami`”
echo “Today is `date`”
echo “Currently there are `who | wc -l` users logged-in to system”

You might also like