Upload
ahmed-lakadkutta
View
236
Download
0
Embed Size (px)
Citation preview
8/3/2019 UNIX Learnings
1/20
UNIX Learnings
ntoduction of UNIX Shell
.What is Shell?>Shell is the command interpreter for Unix systems.
.What is shell-scripting?A shell scripting is a text file with set of Unix commands in it. Shell scripts usually begin with a #! and a shellame(Complete pathname of shell).
Example:
#!/bin/ksh (Called as magic line.Must be first line in the shell scripting.)
We can interact with Kernel using different shell commands such as sort,ls,grep etc. .
Fig : UNIX Architecture
8/3/2019 UNIX Learnings
2/20
Basic Unix Commands:Unix commands are categorize into different types as shown in fig.
Fig : Categorizes UNIX Commands
Descriptions of Unix commands
Sample files :1. Sample.txt Content:
In Xanadu ,did Kubla KhanWhere Alph , the sacred river, ranThrough, caverns measureless to man
8/3/2019 UNIX Learnings
3/20
2.cal.txt Content:7*8
quit
3.1.txt Content:aaabbbcccdddeeefffggg2.txt content:bbbccdddeeefffggghhhsdfsdfdsfsdf
5.Uniq.txt content :ramram
shyamshyamabhijitpooja
Type Comman
dNam
e
Description
Syntax Example Result
GeneralPurpose
8/3/2019 UNIX Learnings
4/20
Commands
date print orset thesystem
date andtime
date Tue Oct 2715:35:08CDT 2009
echo Displaymessageon screen
echoText/expression
echoWelcome
Welcome
bc Anarbitraryprecisioncalculatorlanguage
bcexpression
bc7*8quit
56
bcfilename
bc cal.txt(mentioned ontop)
56
Mailx Mailx isused forsendingmail/attachmenttoparticular
email id
mailxsSubjectinformatio
nemailid
mailx s [email protected]
Mail send [email protected]
Handl
ingFiles
Commands
pwd Short forprintworking
directorythe pwdcommanddisplaysthe nameof the
pwd pwd /appldata/download
GivesCurrentDirectory
8/3/2019 UNIX Learnings
5/20
currentworkingdirectory
cd ChangeDirectory
cdpathnam
e
cd~ orcd !
cd ..
cd/appldata/dow
nload
shortcut tohome dir(~)
Back one step
Directorychange to
download
Directorychangeto Homedirectory
Currentdirectory ispreviousdirectory
mkdir
Create adirectory mkdir
director
y name
mkdir tmp tmpdirectory iscreated inthe currentdirectorypath
rmdir Removeadirectory
rmdir
directory
name
rmdir tmp tmpdirectoryremovedfromcurrentdirectorypath
cat The cat
command
concatenat
es and
display
files
cat
filename
catsample.txt(content ontop)
In Xanadu ,did Kubla KhanWhere Alph , the sacred
river, ranThrough caverns
measureless to man
8/3/2019 UNIX Learnings
6/20
cp Copiesfilesfromone
locationtoanother.
cp
oldpathnam
e
newpathna
me
cp/appldata/download/sample.txt/appldata/ON
COP
Filesample.txtcopy to/appldata/ONCOP
mv Renames a fileormoves itfrom
onedirectory toanotherdirectory.
mv
oldname
newname
Move thefilemyfile.txttonewdirectory
mv abc.txtbcd.txt
mv myfile.txtnewdirectory/
Renameabc.txt tobcd.txt
Myfile.txtmove tonewdirectory
wc Shortfor wordcount,wcdisplaysa countof lines,words,and
characters in afile.
wcfilename
wc sample.txt
wc l (Totalnumber of lines)
wc c (Totalnumber of Character)
3 1798sample.txt5
3 = Lines13 = Words
98=Character
8/3/2019 UNIX Learnings
7/20
cmp Compares twofiles andtells you
whatlinenumbers aredifferent
cmpfile1file2
cmp 1.txt 2.txt 1.txt 2.txt differ:char 1, line 1
diff Displaystwo filesandprints
the linesthat aredifferent.
diff file1file2
diff 1.txt2.txt
1d0
< aaa
3c2
< ccc
---
> c c
7a7
> hhh
Hints:
d a line was deleted
c a line was changed
a a line was added
lines with< or >
are the difference
themselves
comm
ForCommon databetweenfiles
commfile1
comm 1.txt2.txt
aaa
bbb
8/3/2019 UNIX Learnings
8/20
file2
c c
ccc
ddd
eee
fff
ggg
hhh
----------------
Hint:
1)comm -1
fil1.txt
file2.txt
->select lines
common to
file1
2) comm -2
fil1.txt
file2.txt
->select lines
common to
file2
3) comm -3
fil1.txt
file2.txt
->select lines
not common to
both files
8/3/2019 UNIX Learnings
9/20
gzip
Gzip reduces thesize ofthenamedfilesusingLempel-Zivcoding(LZ77).
Wheneverpossible,each fileisreplacedby onewith theextension.gz, while keepingthe sameownership modes,accessandmodification times
gzipfilename
gzip 1.txt1.txt.gz
gunzip
Todecompre
ss files.gunzipfilename.gzORgzip dfilename
gunzip 1.txt.gz 1.txt
8/3/2019 UNIX Learnings
10/20
.gzORgzcat
filename.gz
zip Tocompress files
zipzipfilename.zipfile1.html
file2.txt
zip
archive.zip1.txt 2.txtsample.txt
archive.zip
Hint:
If a.zipalready
exists.Zipcommanddoes notoverwrite. If fileexiststhen filewill either
updatedorappendedtoarchieve.zip
unzip
Todecompress files.
unziparchive.
zip
unziparchive.zip
1.txt
2.txt
Sample.txt
BasicFiles
Comm
ands
8/3/2019 UNIX Learnings
11/20
ls ListingFileattributes
ls -(option)filename
(optional)
ls lrtfilename
ls l
ls lrt 1.txt
apdmd517:/home/myscetl>ls -l
total12434
-rw-r--r--1 myscetldstage28 Oct 1706:091.txt
-rw-r--r--1 myscetldstage28 Oct 1706:092.txt
drwxr-xr-x 3myscetldstage96 Sep29 23:44Abhijit
Hint:
The list
precededby thewordstotal
12434
8/3/2019 UNIX Learnings
12/20
-rw-r--r--1 myscetldstage28 Oct 1706:091.txt
Simple
Filtering
8/3/2019 UNIX Learnings
13/20
head
Displaysthe firstfewlines of
a file
headfilename
head 1.txt
head -2 1.txt
aaa
bbb
ccc
ddd
eee
fff
ggg
aaa
bbb
Hint :head nfilename
Displayonly n
numberof linestail Displays
the lastfewlines ofa file.
tailfilename
tail -1 1.txt
Hint:Take only lastline only-------------------
tail +2 1.txtHint:Line start from3 to last line
ggg(Last lineonly)
----------------
bbb
ccc
ddd
eee
8/3/2019 UNIX Learnings
14/20
fff
gggcut Helps
split thelines ofafile(vertically)
cut-delimiter-fieldnumberfilename
cut d , -f1
sampl.txt
Hint: Delimiteris comma (,).
Defaultdelimiter istab.------------------Cut c 1-5
sample.txt
Hint: -c optionis for specificcolumn only (1to 5 column).
InXanadu
WhereAlph
Through
In XaWher
Thro
paste
Joinsthe linesofdifferentfiles
pastefile1file2file3
paste 1.txt2.txt
aaa
bbb
bbb c c
cccddd
dddeee
eee fff
fff ggg
ggghhh
8/3/2019 UNIX Learnings
15/20
sort Sortsfiles,mergesfiles
that arealreadysorted,andchecksfiles todetermine iftheyhavebeensorted
sortfilename
sort sample.txt Throughcavernsmeasureless to
man
WhereAlph , thesacredriver, ran
InXanadu ,did Kubla
Khan
uniq
Reportsordeletesrepeated linesin a file.
Uniqfilename
uniq uniq.txt Ram
shyam
abhijit
pooja
[Onlyuniquevalues ]
tr Translatescharacters.
troptionsexpr1expr2standard
input
tr "[A-Z]" "[a-z]"
8/3/2019 UNIX Learnings
16/20
man
Filtering
UsingRegul
ar
Expression
egrep
Searches a filefor apattern.Extende
dversionofgrep.Egrep isbasicallyused forregularexpressi
on.
egrep optionfilename
egrep "a+"1.txt
aaa
grep
Used forpatternmatching
grep optionfilename
grep i AAAfilename
aaa
sed Astreameditor
sedpattern
{Action;}filename
sed's/Khan/Don/'sample.txt
Hint: ReplaceKhan with Don
InXanadu ,did Kubla
Don
WhereAlph , thesacredriver, ran
8/3/2019 UNIX Learnings
17/20
Throughcavernsmeasureless toman
Process
Comm
ands
ps Showsstatusofprocesses
Ps-option
ps f
Hint:Full listing
showing thePPID of eachprocess
nohup
Runs acommandwithouthangups.
nohupsciptname.sh &
Hint:Givesprocessid (psid) andcreatenohup.out foroutputof therunning
status.We cancatnohup.out in
nohup abc.sh nohup.out
(foroutput)
8/3/2019 UNIX Learnings
18/20
order tosee thestatus
Shell
programmi
ng
commands
read Theread
statement is theshellsinternaltool fortakinginputfrom theuser.
read
variablename
read name echo$name
exitTo existHint:exit 0 Everthing is fine
exit 1 -Whensomethi
ng wentwrong
existnumber(0/1)
exist 0
expr Theexprcommand readsthe
exprnumber
expr 3 + 5 8
8/3/2019 UNIX Learnings
19/20
Expressionparameter,
evaluates it, andwritestheresult tostandardoutput.
1(space)operator
(+/-/*)(space)number2
if Tocheckthecondition
if[condition]thenExprfi
if [ $count =10 ]; thenecho"Number=10"fi;
Number=10
case The case statement isalwayspreferredwhen thereare manyitems toselect frominstead ofusing alargeif/elif/elsestatement
CaseexprvalueInCase1)expr ;Case2:expr;esac
NUM =10
case $NUM in
"nine" ;;
10) echo"ten" ;;
*) echo"INVALIDNUMBER!" ;;
esac
ten
while
While{condition}doexpressi
Alphabet=ab c d eCount=0while[$Countlt -5]
8/3/2019 UNIX Learnings
20/20
ondone
doCount=`expr$Count + 1`
done