day 1 - 115 cg

42
8/22/2019 day 1 - 115 CG http://slidepdf.com/reader/full/day-1-115-cg 1/42 Unix Day 1 ----- 1. Introduction to unix 2. unix file system 3. Basic commands 4. The Vi editor 5. File Permissions 6. file links UNIX UNIplexed computing system SCO-UNIX - ken Thompson SYS III, IV, V | ---------------------------------------------------------------- ------------------- | | | | | vendor Sun IBM HP Apple Intel H/w SPARC Mainframes Hp-series POWERPCs x86 O/s SunOs AIX HP-UX Macintosh ? <---------------------unix--------------------------------> Linux Linux Linux Linux Linux cc vc++ ls.c | ls.o ls.obj | | ls.out ls.exe unix/gnu-linux

Upload: samionline

Post on 08-Aug-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 1/42

Unix

Day 1-----1. Introduction to unix2. unix file system3. Basic commands4. The Vi editor5. File Permissions6. file links

UNIX

UNIplexed computing system

SCO-UNIX- ken ThompsonSYS III, IV, V

|----------------------------------------------------------------

-------------------| | | ||

vendor Sun IBM HP AppleIntel

H/w SPARC Mainframes Hp-series POWERPCsx86

O/s SunOs AIX HP-UX Macintosh?

<---------------------unix-------------------------------->Linux Linux Linux Linux

Linux

cc vc++

ls.c|

ls.o ls.obj| |ls.out ls.exe

unix/gnu-linux

Page 2: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 2/42

redhat mandrake suse caldera ubunto| (novell)

--------------------- || | gui control panelpaid free| |RHEL fedora project

|fedora core 1fedora core 9.

Oracle database

-------------Solaris ( OS )h/w (sparc)||+-------------- WEBlogic - app server ( web pages )

-----------------------Solaris ( OS )h/w (sparc)|

-----------------------------------| | | | | |

WIN ->putty.exe

Connect to a unix server

1. telnetis a network component of any os

2. rsh

remote shell

3. sshsecured shellu/p is encrypted

packed into putty.exe

unix server

Page 3: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 3/42

| sshd (background service)| | telnetd

||windows client (telnet,ssh)

unix os is in 2 parts

h/w|kernel / c program / system calls| || +--------------> /boot/vmlinuz....| thousands of binaries (.out)| + relocatable files (.o)| archieved into single file| with compressionshell|user / appln

Power user----------

rootamitsmith ( dba rights )king ( n/w guy )-------------------root ( spy root )

( equivalent to Administrator in windows os)

#

End users----------

raj, vishal, etc...

$

who - online users only

pts / 1

pseudo terminal slave

select empno,ename,sal,comm, sal+comm, from emp

who am i

Page 4: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 4/42

mycommand.c|cc -c mycommand.c ( compilation )|mycommand.o ( ie obj file )|cc -o mycommand.out mycommand.o ( linking )

cal

cal -3

cal -m

cal -s

cal -j

cal 1 1978

cal 1 1cal 1 9999

1980 - 2099

clear

pwd - print working directory

1 / ( root directory of unix file system )|

----------------------------------------------------| |

2 home root <==

|----------------------------------------| | |

3 neena <== rajesh mayur

/home/rajesh <== linux, aix, hpux| | |1 2 3

/ <== sun os standard|export|home

|rajesh

/export/home/rajesh

/|users|

Page 5: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 5/42

airoli|

-------------------| |tower1 tower2| |

----------------- --------------| | | |rajesh raj raja amit

Virtualization---------------

Linux win98 Solaris (Guest OSs)| | |+---------------+

|VMware ( .exe files )|win XP ( Host os )x86

XP win2003 Solaris (Guest OSs)| | |+---------------+

|VMware ( .out files )|Linux ( Host os )x86

Page 6: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 6/42

International Directory Structure for UNIX File System

1 /|

---------------------------------------------------------------------------------------------------------

| | | | | | | | || | | |

2 bin usr sbin home etc dev proc opt varlib mnt boot tmp

| | |+-------+ +-------+ passwd| | | |

3 bin sbin rajesh amit|passwd

 

1. /bin - all the binaries ( executable files )2. /usr/bin - unix system resource binaries

3. /sbin - systems' binaries ( for root user )4. /usr/sbin - systems' binaries ( for root user )

c:\windows\system32>

5. /home - caters the home directory of all users(/home/rajesh)

6. /etc - editable text to configure

c:\windows\system\*.ini files

7. /dev - device driver names

/dev/pts/10 user ashwini's console name/dev/pts/20

8. /proc - running unix processes

9. /opt - optional software by third party

C:\program files\>

10. /var - variable data

mails, buffers, spool, logs, dumps

/var/log/messages - unix messages

/var/log/httpd/ - apache tomcat

/var/log/bea/ - bea - weblogic servers log

Page 7: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 7/42

/var/mail/sachin - mails of sachin

11. /lib - libraries ( dynamic link libraries )DLLs

12. /mnt - mount points for removable media

13. /boot - unix kernel image

14. /tmp - dumping ground for all users

libpam.so.0

pluggable authentication module

abc.c (50)|

----------------------------------| |abc.o abc.o| |

abc.so ( shared object )

(abc.dll -> windows os )

master.c (main() )|master.o|master.out

1 you <--------|--------> unix 2|

java, oracle, sybase, adobe3

unix server ( /dev/console )

Page 8: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 8/42

| || for terminal console

----------------|

+---------------+| | | <--- hard disk ( /dev/sda )+--|---------|--+sda1 sda2

In Unix everything is a file

harddisk printer network card

/----------------------------------------------

| | |dev home proc| | || amit ( rw ------------| | |

+--------------------------------+ 232 2345| | | (ymsgr) (browser)sda lp eth0

rw rw

rundll32 user32.dll, LockWorkStation

PATH=/cg/mybin:/bin:/usr/bin:/home/rajesh

Page 9: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 9/42

file - used to determine the files type regardless ofits name or extension

Press q to exit from ==> ls | less

which - is used to find out the binary's location

ls ls ls ls

Page 10: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 10/42

Features of unix os

1. portable

Solaris RationalJava DB2Star office

windows

c:\......Desktop> tomcat.zip|c:\> ftp 10.58.28.82ftp> username : vishal / x

ftp> bin ( to swich to binary mode )ftp> put *.zip

||||||

|unix server ( vikhroli )

( root # service vsftpd start )

=========================================================

Day 2----------

More on file system

Page 11: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 11/42

basic commandsvi editor

file permissionsfile linksvarious filters

/dev/sda/dev/sda1/dev/sda2/dev/lp/dev/console

mount /dev/usb /mnt

/|

-------------------------------|mnt|

My computer|--------------------------------

| | |c: d: e:

Basic commands=================

Absolute path

[/]

|--------------------| |[home] [etc]| |

====> [amit] passwd|abc.txt

cat /etc/passwd

Relative path

cat abc.txt

Directory commands=====================

make directory - mkdirremove directory - rmdir ( will remove empty directory only)change directory - cd dirnamecome 1 level up - cd ..

Page 12: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 12/42

come 2 level up - cd ../..come 3 level up - cd ../../.. toggle directory - cd -goto home directory- cd ~ OR cd /home/amit

File commands======================create a file - cat > filename

your data here^d ( to save )

view content of file - cat filenamedelete a file - rm filenamerename a file - mv oldname newnamecopy a file - cp sourcefile targetfile/dirctory

cp abc.txt office/inbox/123.txt

move a file - mv abc.txt office/outbox/

/|

------------------------------

| |root home| |

sachin|

-----------------------|

office|

+-----------------------+| |mail bank| |

+---------------+ +---------------+| | | |inbox outbox icici hdfc| | | |smith.doc invite.txt application ford.txt

henry || |+-----------------------+

|

common inode

ln ford.txt /root/office/mail/outbox/henry

ln ford.txt ../../mail/outbox/henry

mkdir -p world/asia/india/mumbai/vikhroli

rm

Page 13: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 13/42

rm -i

rmrm

alias rm='rm -i'

alias del='rm -i'

alias cls=clearalias c=clear

/|root|world <==|

-----------------------------------------------------| | | | |asia australia africa usa europe| | | | |

---------- ---+ ---- -------------- ---| | | | | | |india srilanka sydney kenia brazil mexico london| | | | | | |mum.txt col.prg kan.c zim.q dom.lst prince.csv uk

1. copy mum.txt to london2. rename zim.q to zebra.q3. copy complete australia directory inside india

(hint : use cp -r ie recursive )4. remove complete usa directory in one go

( hint : use rm -r ie recursive )

5. rename india to bharat

rm -r directory( if rm's alias is rm -i )then it will delete interactively

ie rm -ri directory

BUT

rm -rf directory

( force delete, no interactive )

------------------------------------------------

The vi editor=============

Page 14: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 14/42

- to edit files

- programmer's editor

- king of all editors

vi -> VIsual

vim -> Vsual IMproved

$ vi

$ vi invite.txt

Esc----------------> command mode ( default )| || [i a o ]--------[ : / ? ]-------------[v]--------------| | | |+------ Insert mode Ex mode visual mode

Last line mode

Command mode-----------i - to start insert mode at cursoro - open a blank line below cursora - append next to the cursor

: - to start ex modeG - to goto the last linev - to start the visual mode

w - goto the next word forward10w - goto the 10th word forward

b - goto the prev word backward10b - goto the 10th word backward

e - goto the next word forward and set the cursoron the last letter of successive words

^E - scroll down

^Y - scroll up

^U - page up

^F - page down

Ex mode-----------:w - to save the file:w abc.txt - to saveAs abc.txt

Page 15: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 15/42

:q - to quit vi editor ( if file is upto date):q! - to quit vi withous saving changes:wq - save and quit vi editor:set number - enable the line number (:set nu ):set nonumber - disable the line numbers ( :set nonu )

:400 - goes to line number 400:$ - to goto the last line

:split - split the file in two parts

^ww - is used to toggle across screens

:qa - quit all windows/screens

:new - new blank file is started:set nowrap - do not wrap lines

/|

-----------------------------|home

|---------------------| |sachin vishal

|datafile

cd ~

cp /home/vishal/datafile .

Delete a line ddDelete a word dwDelete 3 words 3dw

Copy a line yyPaste a line p

dd yy ( yank/copy)cut copy

Page 16: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 16/42

| |+---------------+

|pastep

:23,27y - copy this range regardless of cursor position:23,27d - delete lines and store it in buffer

Search a pattern---------------------

/gupta/the

Press n to goto next occurance in forward directionPress N to goto prev occurance in backward direction

:set ignorecase

:set noignorecase

/man/\<man - patterns starting with 'man'/man\> - patterns ending with 'man'

/\<gupta\> - exclusive word search, ie whole words

/the

/\<the\>

their

there

breatheless

/the - search from top

?the - search from bottom

Press u to undo last action

10u - undo last 10 actions

Search and Replace a pattern==============================

s - substitute

:s/a/A

Page 17: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 17/42

- replace first occurance on current line

:s/a/A/g- replace all occurances on current line- g means global

:%s/a/A- replace first occurance on all lines

:%s/a/A/g- replace all occurances on all lines

:5,10s/a/A/g- replace all occurances on lines 5 to 10

------------------------------------------

Unix security model (basic)

File permissions=================

3 types of users

1. user/owner

2. group (logical entity)

3. other

type ggg username size m-date m-time filename| ||| | | | | |-rw------- 1 vishal account 25 2009-09-08 15:34 abc.txt

||| ||| | |uuu ooo linkCount groupname

group: account=============================================================owner : vishal

-rw- r-- --- 1 vishal account 25 2009-09-08 15:34 abc.txt

owner : ashwini-rw- r-- r-- 1 ashwini account 6920 2009-09-08 13:32 Book1.txt=============================================================

owner: sachin-rw- r-- r-- 1 sachin dba 6920 2009-09-08 13:32 init.ora

owner: dravid

Page 18: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 18/42

-rw- r-- r-- 1 dravid dba 6920 2009-09-08 13:32 init.lst

type

d - directory- - text/binary fileb - block device filec - character device file

s - socket

3 types of permissions

-----------------------------------------------------------File Directory

-----------------------------------------------------------read cat, vi, ls

-----------------------------------------------------------write modify file, mkdir,rmdir, rm,

vi, cat > cp,mv,cat>,vi,-----------------------------------------------------------execute *command,shell *cd directory

script-----------------------------------------------------------

:wq! - force save

chmod userType+-permissionType entity

chmod u-x bank

chmod gu+wr abc.txt

chmod wr+gu abc.txt (illegal)

chmod g-w,u+r,o-rw pqr.txt

Page 19: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 19/42

ls -l /bin ( will show contents of /bin )

ls -ld /bin ( will show permission of /bin)

File permissions|

-----------------| |Characters Octal

u g o 4 readr w x 2 write+ - 1 execute=

= is used to overlap

chmod ugo=rchmod a=r

4 read2 write1 execute---------7 r,w,x

ugochmod 777 datafile

chmod 754 datafile

Default permission

-------------------

Permissions acquired by the file after creation

umask - user mask ( user specific value )login specific

Arithmetic calculation

file directory---------------------------------------------------base 6 6 6 7 7 7

umask 1 1 1 1 1 1---------------------------------------------------default 5 5 5 6 6 6

Assignment:------------

set the umask to get the minimum default permissionas execute on all user levels for FILE

Page 20: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 20/42

Bitwise calculation

4 - 1002 - 010--------6 - 110 <--- base of file

1 - 001

file directory---------------------------------------------------base 110 110 110

&~umask 010 010 010--------------------------------------------------default 010 010 010

-w- -w- -w-

Can never get execute permission by default on "unix OS"

------------------------------------

file links-----------

||

---------------------------------| |soft link hard linksymbolic linksymlink

'windows short cut is analogous tounix soft link'

- it holds the path of a target file/directory

- it is just a shortcut to a complex path

- if soft link is deleted, nothing will happento the target file

- but if the target file is deleted, thenthe soft link will be broken

ln -s office/mail/inbox ib

ln -s office/bank/icici/application appln

pwd -P

ln -sf ( force creation )

Page 21: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 21/42

ln -s /mnt/cdrom/resume.doc data.txt

========================================set -o

-o means on

+o means off

set -o noclobberprevent the overwrite ofcat > existingFile

set -o ignoreeof

disable ^d to logout

- use the logout/exit command manuallyto logout of the system

-------------------------------------------------Day 3

Hard links

- unique concept of unix/linux os- share a file across multiple directories

- changes made in one will reflect at other locationsor vice versa

- hard link cannot be created across patititon

- hard link cannot be created to a directory

- it has no concern to the pathrather it refers to the internal inode block number

- file can be removed when the link countis set to 1

-rw-r--r-- 1 root root 30798 2009-09-09 10:06 peter.txt

917927 <-- inode block

1 block is required to store meta data

30798/1024 = 31 blocks are required to store the data

= 32 blocks are required to store the file

Page 22: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 22/42

40gb -> 38.5 gb

data blocksinode blocksfree blocks

i - node contains following information

917927 <--- peter.txt===============permissions : -rw-r--r--link count : 1owner : rootgroup : rootsize : 30798mdate : 2009-09-09mtime : 10:0613 pointers to data blocks

0 1 2 3 4 5 6 7 8 9 10 11 12

int inbox=100;int *ib = &inbox;

int rajesh=190;int &raju = rajesh;int &raja = raju;

===============================================Various filters-----------------

wc - word count

count lines, words and characters

wc datafile

wc -l datafilewc -w datafilewc -c datafile

ls | wc -l

ls *.sh | wc -l

ls *.txt | wc -l

Redirection operators-----------------------

Page 23: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 23/42

> output cat > abc.txt

>> append cat >> abc.txt

< input cat < abc.txt OR cat abc.txt

| conversion cmd1 | cmd2cmd1 | cmd2 | cmd3

head -

to view first 10 lines of a file

- used to see the oldest log from a log file- code declaimers from source code- title / header info from documents

tailto view last 10 lines of a file

- used to see the latest log from a log file- footer info from documents

tail /var/log/anylogfile

tail -f /var/log/anylogfile

-f means followup the file

head -9 first.java | tail -5

cut

default field delimiter is TAB

- cut the file vertically

cut -f 1 data.lst

cut -f 2,3 data.lst

cut -c 1-4 emp.lst

cut -d "|" -f 2 emp.lst

cut -d "|" -f 2 emp.lst | cut -d " " -f 2

cut -d "|" -f 4 emp.lst | sort | uniq | wc -l

Page 24: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 24/42

who | cut -d " " -f1 | sort | uniq | wc -l

tr

- translate characters from input file

tr a A < emp.lsttr ab AB < emp.lst

tr abcdefg ABC < emp.lst

a with Ab with Bc with Cand replace d, e, f and g also with C

tr a-z A-Z < emp.lst

tr a '^vTAB' < emp.lst

replace a with a TAB key

q , x, quit, bye, exit,

^d -^c - terminate

^z - send the process in the background

ls -l | tr -s ' ' | cut -d " " -f1,8

tr -s is used to squeeze multiple spaces tothe single space.

who | tr -s ' ' | cut -d " " -f1,5

to get the name and ip of user 'miriam'who | tr -s ' ' | cut -d " " -f1,5 | grep miriam | cut -d " " -f2

To gain the salary of "r.k. shukla"

cut -d "|" -f 2,6 emp.lst |grep "r.k. shukla" | cut -d "|" -f 2

Page 25: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 25/42

grep

global regular expression print

grep pattern filelist

grep gupta emp.lst

grep error /var/log/messages

grep "\<man" emp.lst --colorgrep "man\>" emp.lst --colorgrep "\<gupta\>" emp.lst --color

grep -w "gupta" emp.lst --colorwhole word

grep -c "gupta" emp.lst --color

- count lines that matched the pattern

grep -o "gupta" emp.lst --color

display matching patterns only

grep -o "gupta" emp.lst --color | wc -l

grep -i "gupta" emp.lst --colorignore case

grep sharma * --color

search pattern in all files in current directory

grep -r sharma * --color

recursive search across subdirectories aswell

grep -l sharma *

show file list that matched the pattern

grep ^2 emp.lst --colorshow lines that starts with 2

grep 500$ emp.lst --colorshow lines that ends with 500

Page 26: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 26/42

alias ld='ls -l | grep ^d'list only directories

alias lr='ls -l | grep ^-'list only files

alias sl='ls -l | grep ^l'

list only sym links

sa x enasa k s ena

12 3 4 567sa [xk]s* ena

* -> preceeding character should be matched zero or more times

ag a r walag g a r wal

ag r a wal

grep ag[agr][ra]r*wal emp.lst --color

grep -e shukla -e sharma emp.lst --color-e means expression

-----------------------------------------------------------

sort

- to sort the data

cut -d "|" -f 1 emp.lst

sort -t "|" -k 1 emp.lst

sort -t "|" -k 1n emp.lst

sort the 1st column as numeric

cut -d "|" -f2 emp.lst | cut -d " " -f2 | sort -rshow emp surname in reverse order

cut -d "|" -f4,6 emp.lst | sort -t "|" -k 1 -k2n

sort -t "|" -k 4,4 -k emp.lst

Page 27: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 27/42

4,4 syntax is used in multi column sort

4,4 means

4,4

first character of 4th column to thelast character of 4th column

5

12 45 7891016 / 06 / 1949

sort -t "|" -k 5.7,5.10n -k 5.4,5.5n -k 5.1,5.2n emp.lst

------------------------------------------------------------

Unix processes

process monitoring|---------------------------------| |static view dynamic view| |ps top

process status

top - linuxtop - hp-ux

prstat - sunostop - aix

ps - processes of the current terminal

ps u - all terminals for that user

ps a - all terminals for all users

ps au - all users processes

ps au | grep abhijit

ps x - system processes

service vsftpd statusservice vsftpd start

Page 28: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 28/42

ps aux - all user + system processes

1 (/sbin/init)|2449 (/usr/sbin/sshd) - server process||||4217 (sshd: root@pts/18) - client process|4219 (bash)

echo $$

ps l | grep $$

ps xl | grep PPID

pstree -u -p | less

kill SIGNAL PID

signal -15 (TERMINATE)

-2 (INTERRUPT) same as ^C

-9 (KILL)

kill 28373 ( kill -15 28373 (-15 is default) )

 

------------------------------------------------The Shell

Page 29: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 29/42

----------

Kernel <--> Shell <--> user

D facto standard - bourne shell

ls Steve Bourne - /bin/sh

ls David Korn - /bin/ksh

ls Bill Joy - /bin/csh

ls Bourne Again Shell - /bin/bash

Environment variable

$ - holds the current process idSHELL - holds the shell name

HOME

LOGNAMEMAILPATHLANGCLASSPATH ( in unix, this must be in capital letters)

To declare a variable

scaler type

A=10

A=mumbai

User variables v/s Environment variables(local) (global)

set env--- ---

A=10 HOME,SHELL,MAILCLASSPATH=/...

1 (/sbin/init)|2449 (/usr/sbin/sshd) - server process||||4217 (sshd: root@pts/18) - client process|4219 (bash) <-- login shell SHLVL

Page 30: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 30/42

||15685 <-- child shell SHLVL

export VAR ( to globalize the variable)

export -n VAR ( to localize the variable )

A=10

B=2

let C=$A+$Blet C=$A-$Blet C=$A/$Blet C=$A*$B

expr $A + $B

expr $A - $B

expr $A / $B

expr $A \* $B

" value of A is $A " - variable string

'' - string constant / string literal

`` - backticks, grave accent sign

slaush - slash

First script to say hello world

---------------------------------------

Page 31: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 31/42

Day 4

--------------------------------------------------Why Shell Scripting ?more on scriptingcommand line argumentssedawkfunctionsuser profilefile compressionQ and Afeedbackexam

C++/Java programming language

.c/.java| |.exe .class

1. Automation2. Automation3. Automation

Following are the reasons to avoid shell scripting

1. rich variety of data typeschar,int, float, double

2. rich variety of data modifiersshort, long, signed, unsigned

3. rich variety of operators

4. rich variety of built in library functions

5. efficient file handling

6. GUI interface

7. close source code, ie .exe, .com. .class

8. heavy math operation

9. data structures

stack, queue, linked list, binary trees...

10. mission critical operations

Page 32: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 32/42

Scripting is a GLUE code between UNIX OSand the RESOURCES

USER

--------------------------------Application server ie WEB LOGIC---------------------------------Script -> startWLS.sh----------------------------------unix os ( unices)

USER--------------------------------The grep command for pattern search---------------------------------Script -> patternSearch.sh----------------------------------

unix os ( unices)

:.!date --> to insert the output of date command in vi

Customizing vi editor---------------------

vi -> .exrc

~/.vimrc ( vi editor's profile )

vi --> .vimrc --> emp.lst

echo $?

- returns the exit status of recentlyexecuted command

read pattern :

read filename : emp.lst

Page 33: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 33/42

grep emp.lst

Relational operators for numbers

unix C------------

-eq =

-ne !=

-gt >

-ge >=

-lt <

-le <=

String comparisons---------------------

str1 = str2

str1 != str2

-z to check empty string

-f to check file's presence

-n to check non empty string

Programmming constructs

1. sequence

2. Selectionifcase

3. Iterationforwhile

Page 34: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 34/42

until

 

program to input 2 filenames from userand show if both are hardlinked or not

Command line arguments

- passing parameter to the script

$ bash patternSearch.sh

$ bash patternSearch.sh sharma emp.lst

^L - to clear the foreground mesgs

sed - stream editor

view recordssearchappendinsertmodifydelete

sed 'actions' datafile

- if no actions are giventhen the default is print action

sed '' emp.lst

by default print all lines

sed '3p' emp.lstby default print all lines

Page 35: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 35/42

and also print 3rd line

sed -n '3p' emp.lst

suppress default printingand print 3rd line only

cat -n emp.lst | sed -n -e '3,6p'-e '8,10p'-e '$p'

sed -n '/gupta/p' emp.lst

sed -n -e '/gupta/p'-e '/sharma/p' emp.lst

sed -i '$a555|vishal|trainer|training|12/12/12|1000'

emp.lst

-i means inplace editing of file

p - printi - insert abovea - append below

d - deletes - modify ( substitute )

sed 's/sales/MKT/' emp.lst

sed '5,10s/sales/MKT/' emp.lst

sed '5,10s/sales/MKT/g' emp.lst

1114 sed -i '$a 555|vishal|trainer|training|12/12/12|1000' emp.lst1120 sed -i '10a 555|vishal|trainer|training|12/12/12|1000' emp.lst1123 sed '10i 555|vishal|trainer|training|12/12/12|1000' emp.lst1126 sed '10i 555|vishal|trainer|training|12/12/12|1000' emp.lst1128* sed '/director/d' emp.lst1130 sed '/gupta/p' emp.lst1132 sed -n '/gupta/p' emp.lst1133 sed -n '/\<gupta\>/p' emp.lst

1134 sed -n '/sales/p' emp.lst1136 sed -n '/marketing/p' emp.lst1137 sed 's/marketing/MKT/' emp.lst1139 sed 's/sales/MKT/' emp.lst1141 sed 's/sales/PURCHASE/' emp.lst1143 sed 's/sales/PURCHASE/g' emp.lst1145 sed 's/\<sales\>/PURCHASE/g' emp.lst

---------------------------------------------

Page 36: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 36/42

awk - Aho, Weinberger and Kernighen

- report building

- conditional filters

- grep and cut features are built in

ls -l *.sh | awk -F" " '{print $1,$8}'

#!/bin/bash- contract that that script is writtenfor the bash shell

#!/bin/ksh - written for the korn shell

Page 37: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 37/42

BASH

LOGIN--> .bashrc ---> .bash_profile

shell profile --> assigned --> ~/.bashrcuser profile --> log in--> ~/.bash_profile

KORN

.profile

.kshrc

CSHELL.login

Page 38: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 38/42

.cshrc

.bashrc .bash_propfile

| |aliases umaskfunctions exported variables

set -o settings

* called only once afterlogin

* called every timewhen the new shellis invoked

666022----644

Archieving-----------

tar - Tape ARchieve

creation tar -cvf myprog.tar *.sh *.txt *.lst

viewtar -tvf myprog.tar ( table of contents )

extractiontar -xvf myprog.tar

update the tar

tar -uvf myprog.tar jacky

Compression---------------

zip

gzip

Page 39: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 39/42

bzip2

du -sh /etc ( will run in foreground )

summaryhuman readable form ie kb, mb, gb

du -sh /etc & ( run in background )

grep gupta emp.lst 2> myerr.log 1> myout.log

2> means error log

1> success output log of grep command

cd /etc

du -sh selinux/35mb

tar -cvf selinux.tar selinux/

cp selinux.tar selinux.tar.1cp selinux.tar selinux.tar.2cp selinux.tar selinux.tar.3

zip selinux.tar.1.Z selinux.tar.1

gzip selinux.tar.2 ---> selinux.tar.2.gzbzip2 selinux.tar.3 ---> selinux.tar.2.bz2

/sbinatdcrond

unix schedular

|-----------------------------| |once cyclic

authenticated

/usr/binx --> at crontab

Page 40: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 40/42

authorized/etcat.allow cron.allowat.deny cron.deny

scheduled log/var

/|

-----------------------------| | | |bin sbin etc var| | | |at atd .allow logcrontab crond .deny

who | cut -d " " -f 1 | sort |uniq > at.allow

who | cut -d " " -f 1 | sort |uniq > cron.allow

$ at 18:17> echo "..."> bash shell.sh> wall ....> ^d

$ atq$ atrm

crontab -e editcrontab -l listcrontab -r remove

20 18 10 09 * echo "hello from crontab" > /dev/pts/9

20,30 18,20 10 09 * echo "hello from crontab" > /dev/pts/9

m h d month dow task

Page 41: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 41/42

30 9 1-7 3,6,9,12 6 awk....

18:2018:30

20:2020:30

[email protected]

[email protected]

9870 232 163

10.74.156.24255.255.255.12810.74.156.1

Page 42: day 1 - 115 CG

8/22/2019 day 1 - 115 CG

http://slidepdf.com/reader/full/day-1-115-cg 42/42