Vous êtes sur la page 1sur 24

Unix commands and Shell scripting

Where there is a shell there is a way.

September 24,2014

Copyright 2012 Tata Consultancy Services Limited


1
Agenda

History of Unix
Architecture of Unix
Shell
Unix commands
vi editor
cron tab
Shell scripting
Daemon

2
History of Unix

In mid 1960s a consortium formed by MIT,


GE and AT & T Bell Labs started work
on Multics, later AT & T dropped out
and the subsequent events were
all milestones in history.

3
History of Unix

UNICS (Unix) was developed


Minix was developed by
at AT & T labs by Dennis
Andrew Tanenbaum.
Ritchie and Ken Thompson.

Unix found a new host. Linus Torvalds developed


Linux.

Unix was rewritten in C,


hence it became highly
portable.

4
Architecture of Unix

The Unix
system
architecture is

3
layered.

5
Shell

Section overview
The shell is a special utility which
-Responsibilities
parses the command line and performs
-types
the following tricks.
Command execution.
Variable substitution.
Filename substitution.
I/O redirection.
Pipeline hookup.
Acts as an interpreted
programming language.

6
Shell

Types of shell

sh
bash
ksh
csh
tcsh

7
Commands

Section overview
Unix commands have the following syntax:
-Regular expressions

-filters
command <-options> <arguments> -decision statements

-loops

Regular expressions
A sequence of characters and wildcards
that form a search pattern.
Regex Matches
. (dot) Any single character
Filters
* Take
(asterisk)
input from input and display the Zero
output or more occurrences
in standard output.
of the preceding character
sed, awk, grep, tr, cut , paste, sort, uniq
[] Any single character
enclosed
8
Commands

Regex Matches
\{n\} Matches n occurrences of
the preceding character.
\(.\) Stores the matched
characters in registers 1-9.

Filters
Commands which take input from standard input
and send the output to standard output.
sed, awk, grep, tr, cut , paste, sort, uniq

9
About friendship

FRIENDSHIP is a relationship of
mutual affection
between 2 or more peoplee.
between 2 or more peoplee.

Friends hip has been studied in

academic fields such such as sociollogy, social psychollogy,


anthroopology, and phillosophy.

A World Happiness Database study found that that


People with cloose friendships are happiiiier.

10
Commands

Decision statements
The exit status of the condition is checked and the
appropriate branch is chosen.

There are 2 statements available :

if
case

11
Commands

if construct

if command
then
set of instructions
elif command (optional)
then
set of instructions
else (optional)
set of instructions
fi

12
Commands

case construct
case variable in
value1) set of instructions;;
value2) set of instructions;;
valuen) set of instructions;;
*) set of instructions;; (optional)
esac

13
Commands

Loops
Loops cause a set of instructions to be
executed a predefined number of times.

The 3 loops available are:

while
for
until

14
Commands

for construct
for variable in value1 value2 .valuen
for ((variable initialization; condition; updation))
for variable
do
set of instructions
done

15
Commands

while construct
while command
do
set of instructions
done

until construct
until command
do
set of instructions
done

16
vi editor

vi short for visual editor has two


modes of operation
line mode
command mode

vi <filename> opens the file in vi editor.

exiting vi
:w saves the file
:q and :q! exit without saving the file
:wq and :x save and exit from file

17
vi editor

cursor movement
k Moves the cursor up one line
j Moves the cursor down one line
h Moves the cursor left one character
l Moves the cursor right one character
w Move cursor to the start of the next word
b Move cursor to the start of the previous word
0 Moves the cursor to the start of the line
$ Moves cursor to the end the line
:0 Moves the cursor to the first line in file
:$ Moves the cursor to the last line in file
:n Moves the cursor to the nth line in file

18
vi editor

inserting text
i Insert text before cursor
I Insert text at the beginning of the line
a append text after the cursor
A append text at the end of the line
o Insert a new line to add text below the cursor
O insert a new line to add text above the cursor

deleting text
x deletes character under the cursor
X deletes the character before the cursor
dw deletes the word under cursor
dd deletes the line where the cursor rests
19
vi editor

cut and paste


yy copies the current line
nyy copies n lines from the current line
p pastes the copied text after the current line
P pastes the copied text before the current line

search and replace


/string searches forward for the presence of text
?string searches backward for the presence of text
:s/search/replace/g searches the pattern and replaces the text
globally

20
cron tab

cron is a time based job scheduler in Unix.


crontab (cron table) is a file which maintains
the schedule.

crontab commands
crontab -l displays the cron tab file
crontab -e opens the cron tab file in editor
crontab -r deletes the cron tab file

21
cron tab

format for scheduling


a typical entry in the crontab file:
30 4 * * * /home/prasanna/run.sh

fields values
minute 0 - 59

hour 0 - 23

day of the month 1 - 31

month 1 - 12

day of the week 0 - 6 (0 stands for Sunday)

22
daemon

daemon are process that run in the background,


without a controlling terminal and parent.

Steps to create a daemon


1. fork () used to create a child process
2. setsid () used to detach from the terminal
3. chdir () to set the directory
4. close () to close all open file descriptors
5. umask () to create mask for the files
6. signal handling
7. log file handling
23
Thank You

IT Services
Business Solutions
Consulting

Vous aimerez peut-être aussi