Professional Documents
Culture Documents
#!/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.
2.cal.txt Content:
7*8 quit 3.
1.txt Content: aaa bbb ccc ddd eee fff ggg 2.txt content: bbb cc ddd eee fff ggg hhh sdf sdf dsf sdf
Type
Descript ion
Syntax
Example
Result
Gener al Purpo se
Comm ands date print or date set the system date and time Display echo message Text/exp on screen ression An bc arbitrary expressio precision n calculator language bc filename Mailx is used for sending mail/atta chment to particular email id mail x s Subj ect infor matio n email id Tue Oct 27 15:35:08 CDT 2009 echoW elcome bc 7*8 quit Welcome
echo
bc
56
Mailx
bc cal.txt 56 (mentioned on top) mailx s Test Mail send to Subject abc@gmail.com abc@gmail.co m
Handl ing Files Comm ands pwd Short for print working directory the pwd comman d displays the name of the pwd pwd / appldata/d ownload Gives Current Directory
cd
cd pathnam e
cd /appldata/dow nload
cd ~ or cd !
Directory shortcut to change home dir(~) to Home directory Back one step Current directory is previous directory tmp directory is created in the current directory path tmp directory removed from current directory path cat In Xanadu ,did Kubla Khan on
Where Alph , the sacred river, ran Through caverns measureless to man
director y name
rmdi r
Remove a directory
rmdir tmp
rmdir directory name
cat
cat filename
cp
Copies files from one location to another. Rename s a file or moves it from one director y to another director y.
mv
mv oldname newname
wc
Short for word wc count, filename wc displays a count of lines, words, and characte rs in a file.
wc sample.txt
3 17 98 sample.txt5
cmp
Compar es two cmp files and file1 tells you file2 what line number s are different Displays two files diff file1 and file2 prints the lines that are different .
diff
diff 2.txt
1.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
com m
comm 2.txt
1.txt
aaa bbb
c c
file2
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
gzi Gzip redu ces the gzip size of filename the named files using LempelZiv coding (LZ77). Wheneve r possible, each file is replaced by one with the extension .gz, whil e keeping the same ownershi p modes, access and modificati on times
gzip 1.txt
1.txt.gz
zip
archive.zip
1.txt 2.txt sample.txt
archive.zi p Hint: If a.zip already exists .Zip command does not overwrite . If file exists then file will either updated or appended to archieve. zip 1.txt 2.txt Sample.t xt
unzip archive.zip
ls
apdmd51 7:/home/ myscetl>l s -l total 12434 -rw-r--r-1 myscetl dstage 28 Oct 17 06:09 1.txt -rw-r--r-1 myscetl dstage 28 Oct 17 06:09 2.txt drwxr-xrx 3 myscetl dstage 96 Sep 29 23:44 Abhijit Hint: The list preceded by the words total
ls lrt filename
ls lrt 1.txt
12434
Simpl e Filteri ng
hea Displays
head 1.txt
head filename
head -2 1.txt
aaa bbb Hint : head n filename Display only n number of lines ggg (Last line only)
tail
tail -1 1.txt
bbb
tail +2 1.txt ccc Hint: Line start from ddd 3 to last line
eee
fff
cut
Wher Thro
past e
paste 2.txt
1.txt aaa
bbb bbb ccc ddd ddd eee eee fff ggg hhh fff ggg cc
Sorts files, merges files that are already sorted, and checks files to determi ne if they have been sorted uni Reports q or deletes repeate d lines in a file.
sort
sort filename
caverns measurel ess to man Where Alph , the sacred river, ran In Xanadu , did Kubla Khan
uniq uniq.txt
Uniq filename
tr
[Only unique values ] tr "[A-Z]" "[a- in xanadu z]" ,did kubla <sample.txt khan
Hint: where Replace all alph , the capital character with sacred river, ran small character
man
Filteri ng Using Regul ar Expre ssion egre Searche p s a file
for a pattern. Extende d version of grep.Egr ep is basically used for regular expressi on. gre Used for grep i AAA aaa p filename pattern grep matchin option g filename
sed
egrep 1.txt
"a+" aaa
A stream editor
In Xanadu , did Kubla Don Where Alph , the sacred river, ran
ps f
Ps -option
nohu p
Hint: Full listing showing the PPID of each process nohup abc.sh nohup.ou
nohup sciptna me.sh & Hint: Gives process id (ps id) and create nohup.o ut for output of the running status. We can cat nohup.o ut in
t (for output)
echo $name
exit
expr
exit 1 When somethi ng went wrong The expr expr comman number d reads the
expr 3 + 5
if
Expressi on paramet er, evaluate s it, and writes the result to standar d output. To check the conditio n
Number= 10
case
The case st atement is always preferred when there are many items to select from instead of using a large if/elif/else statement
NUM =10 case $NUM in "nine" ;; 10) echo "ten" ;; *) echo "INVALID NUMBER!" ;; esac
ten
while
on done