OS Practical File
Arvind C. Roll no. - 14002 Section - CS1
2
Unix Commands : 1. $ clear clear
- clea clearr scre screen en
2. $ who - details details of users logged in. in the format: format: username username terminal terminal id date time. For eg, $ who Kumar
console
May
9
09:31 (:0)
Vipul
pts/4
May
9
09:31 (:0.0)
Raghav
pts/5
May
9
09:31 (saturn.harvest.com)
3. $ who who am i - your your login login detai details. ls. For eg, $ who am i Kumar
pts/10
4. $ cal
Aug
1
07:56 (pc123.heavens.com)
- calenda calendarr of the curren currentt month. month. For eg,
$ cal February 2008 S
M
Tu
W
Th
Fr
S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
5. $ cal cal 200 2008 8 - cale calend ndar ar of 200 2008. 8. 6. $ cal 2 200 2008 8 - calend calendar ar of Februa February ry 200 2008. 8. 7. $ pwd
- displays displays path of current current working working directory. directory. For eg,
$ pwd /home/student
8. $ ls eg,
- displays displays list of all files files and directorie directoriess of current current working working directory directory.. For
$ls 08_packets.html TOC.sh Calendar Cptodos.shdept.lst
3
9. $ ls ls –l –l - displays displays same list as $ ls ls with detailed detailed inform information ation.. For eg, Total 72 -rw-r—r-1 chap01
Kumar
metal 19514 May 10 13:54
-rw-r—r-1 chap02
Kumar
metal 4174
May 10 15:04
drwxr-xr-x
Kumar
metal 512
May 09 10:31
2
helpdir
10. $ cd [directory [directory name] - change directory directory.. For eg, $pwd /home/kumar $ cd progs $pwd /home/kumar/progs
11. $ cd .. – takes you back to to previous previous directo directory ry $pwd /home/kumar/progs $ cd .. $pwd /home/kumar
12. $ cat
- create and view contents of a file. For eg,
$ cat fl.txt This is a text file.
13. $ cp oldfilename oldfilename newfilename - copies contents from old file to new file 14. $ rm filename filename - removing removing a file 15. $ mv file1 file2 16. $ date
- move file from one location to another.
- specifies specifies system system date. For eg,
Thur Feb 14 17:31:51 IST 2008 17. $ mkdir dirname dirname – make directory directory 18. $ rmdir dirname dirname - remove directory directory 19. $ wc[-l/-w/-c] filename - displays number of line words and character in a file. file. For eg, $ wc fl.txt 1
5
20
fl.txt
4
20. $ sort abc - it is used to arrange the files alphabetically in ascending or descending order. 21. $ grep
- searching a pattern in in a file. For eg, $ grep “is” fl.txt This is a text file.
22. $ shutdown shutdown now - shutdown. 23. $ echo echo “message “message”” - to print a message message.. For eg, $ echo Unix Unix
24. $ printf “message” - to print message, it doesn’t take the next line command. For eg, $ printf Unix Unix
25. $ man ls - displays complete information about ls, page by page. 26. $ man ls | - displays complete information information about ls, line by line 27. $ tty tty - displays displays terminal terminal name. For eg, $ tty /dev/pts/10
28. $ ls –a
– shows hidden files. files.
29. $ ls –x
- shows output in column column format. format.
30. $ ls –F
- all executable executable files. files.
31. $ ls –d
- directory directory..
32. $ ls –afx
- hidden and executable files are displayed in column format.
33. $ echo $shell $shell
- shell state. state.
34. $ set - system variables and know environment. For eg, $ set HOME=/home/henry IFS=’ ‘ LOGNAME=henry MAIL=/var/mail/henry MAILCHECK=60 PATH=/bin:/usr/bin:.:/usr/ccs/bin
5
PS1=’$ ‘ PS2=’> ‘ SHELL=/usr/bin/bash TERM=xterm
35. $ chmod - change mode. 36. $ compress filename - file is compressed as filename.Z. 37. $ uncompress uncompressed ed abc.Z - to uncompress uncompress abc. abc. 38. $ gzip gzip - to compress compress HTML. 39. $ gunzip gunzip - to uncompressth uncompressthee file. file. 40. $ zip fin fin *.html *.html - creates creates an an archive. archive.
6
VI Editor : $ vi 1.
i - to type anything (insert mode)
2.
escape key – command mode
3.
: - last line mode
4.
: sh - exits to UNIX shell
5.
: x – saves files an and qu quits editing mode
6.
: w - saves aves files les and and remai emains ns in edi editing ting mode mode
7.
: wq - sa same as :x
8.
: q!
9.
: q - qui quits ts edit editin ing g mod modee whe when n no no cha chang nges es are are mad madee to to the the file file
10.
Ctrl Ctrl Z – sus suspe pends nds curr curren entt ses sessi sion on and exit exitss to to UNI UNIX X she shell ll
11. 11.
I – tak takes es you you to the the begi beginn nnin ing g of the the lin linee to to ins inser ertt
12 .
a/A – append mode
13. 13.
o/O o/O - inser nsertt blan blank k line ine (Open Open mode) ode)
14 .
r/R – replace character
15.
s/S - deleting word ords/ lines.
16 .
: 5w - 5 lines saved
17 .
20i* - prints * 20 times
18 .
h - to remove character
19 .
l - space
20 .
5l – move 5 spaces ahead
21 .
k - mo m ove cursor up
22 .
j – move down
23 .
$ - move to end of line
24 .
e – move to end of word
25. 25.
w - move movess for forwar ward to begi beginn nnin ing g of wor word
26 .
10G - go to line 10
27 .
Ctrl+F - page forward
28. 28.
Ctr Ctrl+B l+B - scr scroll olls full ull page page back backwa warrd
29.
Ctrl+D – half alf page age forw orward
30.
Ctrl+u - hal half page backwa kwards
- quit quitss edit editin ing g mode mode,, but but afte afterr abou abound ndin ing g chan change gess
7
31 .
Ctrl+L – re redraws screen
32 .
d - d el elete a char
33 .
y – yanking
34 .
c – change char
35 .
! - to act on text
36 .
dd - to delete a line
37. 37.
x – del delet etes es a cha charr on on whi which ch the cur cursor sor is is
38. 38.
X – dele delete tess a char char just just befo before re the the cur curso sor r
39 .
3J – joins 2 lines with 1st
40 .
D$ or D or dd – delete line
41 .
yy – copy complete line
42 .
5yy – copy 5 lines
43.
y$ - copi copies es text text from from curre current nt pos posit itio ion n of curs cursor or to to end end of line line
44.
y1G y1G – copi copies es text text from from cur curre rent nt pos posit itio ion n to beg begin inni ning ng of of the the file file
45 .
p - paste
46.
:1, :1, $s/m $s/mes essa sage/ ge/ms msg/ g/g g = sear search ch and and repl replac acee mess message age with with msg msg in in 1st line
47. 47.
fch fch = move move in forwa orward rd dir directi ection on
48 .
/pat = forward di direction
49 .
Fch = backward di direction
50.
?pa ?pat = back ackward direction
51.
U = undoi doing in in th the cu current li line
52 .
/n = forward
53 .
/N = backward
54.
Ctrl Ctrl+p +pat at = sea searc rch h for for patte pattern rn at begi beginn nnin ing g of of lin line. e.
55 .
$pat = end of line
56.
: 3, 10s/ 10s/me mess ssag age/ e/ms msg/ g/g g = rep repla lace ce mes messa sage ge by by msg msg from from line line 1 to 10
57. 57.
:1,$ :1,$s/ s/me mess ssag age/ e/ms msg g = repla eplace cess 1st occurrence in every line.
58.
\< = used used to matc match h a patte pattern rn at the the beg begin inni ning ng of a wor word d
59. 59.
\> = to to mat match ch a pat patte tern rn at the the end end of a wor word d
60 .
\msg\> = end of a line
61. 61.
: r abc abc = read readss fil filee abc abcee bel below ow curr curren entt fil filee
62.
: r !dat !datee = rea reads ds outpu outputt of date date comm command and belo below w curr curren entt lin linee
63.
: e abc = stop stopss edit editing ing curren currentt file file and will will star startt edit editing ing the file file abc abc 8
64.
: e! abc = same same as as e abc abc but but afte afterr abound abounding ing changes changes made made to the curren currentt file file
65. 65.
: e! e! = load loadss las lastt sav saved ed addi additi tion on of the the fil filee
66.
: n = edits edits next next file file when when vi is is invo invoke ked d with with mult multip iple le file filess
67. 67.
: rew rew = rew rewin inds ds file file list list to star startt edi editi ting ng 1st file
68. 68.
Ctr Ctrl+q l+q = ret return urn to ori origina ginall fil file
69.
: e# e# = return to to or original file
70. 70.
mq = use used d to to mar mark k a loca locati tion on with with char charac acte terr q. q.
71.
“2p = to re recover ver 2 de deletions.
72.
: ab who who worl world d health health organi organizat zation ion = for abbrevi abbreviati ating ng world world heal health th orga organiz nizati ation on as who. who.
73. 73.
: set set aw = auto autosa save ve when when jump jumpin ing g fil files es..
9
Shell Programming: Program-1 Objective: If basic salary of the employee is less than Rs.1500 then H.R.A is equal to 10% of it`s basic salary and D.A is 90% of basic salary. If basic salary either equal to or above Rs.1500 , H.R.A will be 500 5 00 and D.A will be 98% of the basic salary . If the employ salary input through the keyboard write a program in unix shell programming to find the gross salary. Code: $vi abc Echo “Enter basic salary :” read bs if [ $bs –lt 1500 ] then hra=`echo $bs \* 0.1|bc` da=`echo $bs \* 0.9|bc` else hra=500 da=`echo $bs \* 0.98|bc` fi gs=`echo $bc+$hra+$da|bc` echo “Gross salary is Rs.”$gs Output: $ sh abc Enter basic salary : 1400 Gross salary is Rs.2800
Program-2 Objective: The marks obtain a student in five different subjects are input through the keyboard. The student gets, 1. first division division if % above or equal to 60% 2. second division if % between 50%-59% 3. third division if % between 40%-49% 40 %-49% 4. fail if % less than 40% Write a program to calculate the division obtain o btain by the student using logical operator. Code: $vi abc 10
echo “Enter the marks of five subject :” read m1 read m2 read m3 read m4 read m5 per =`echo ($m1 + $m2 + $m3 + $m4 + $m5)/5|bc` if [ $per –ge 60 ] then echo “First division.” fi if [ $per –ge 50 –a $per –le 60 ] then echo “Second division.” fi if [ $per –ge 40 –a $per –le 50 ] then echo “Third division.” fi if [ $per –le 40 ] then echo “Fail.” fi Output: $ sh abc Enter the marks of five subject : 100 90 100 99 100 First division.
Program-3 Objective: Write a programme in shell programming to find whether the input year is leap year or not. Code: $vi abc echo “Enter the year :” read yr con1=`echo $yr \% 4|bc` con2=`echo $yr \% 100|bc` 11
con3=`echo $yr \% 400|bc` if [ $con1 –eq 0 –a $c –ne 0 –o $b –eq 0] then echo “Leap year.” else echo “Not a leap year” fi Output: $ sh abc Enter the year : 2000 Leap year
Program-4 Objective: Write a programme in shell programming to find the factorial of a given no. Code: $vi abc echo “Enter a no. :” read n i=1 fact=1 while [ $i –le $n ] do fact= `echo $fact \* $i|bc` i=`echo $i + 1|bc` done echo $fact Output: $ sh abc Enter a no. : 5 120
Program-5 Objective: Write a programme to find the sum of five digit number. Code: $vi abc echo “Enter five digit no. :” 12
c=0 read no while [ $no gt 0 ] do b=`expr $no % 10` no=`expr $no / 10` c=`expr $b + $c` done echo “Sum of digits is ”$c Output: $ sh abc Enter five digit no. : 12345 Sum of digits is 15
Program-6 Objective: Write a programme to enter an integer through the keyboard and find whether the given no. is even or odd. Code: $vi abc echo “Enter a given no. :” read choice x=`echo $choice % 2|bc` if [ $x –eq 1 ] then echo “No. is odd.” else echo “No. is even.” fi Output: $ sh abc Enter a given no. : 12 No is even.
Program-7 Objective: Write a program to find the value of one no. raised to the power of anothor no. 13
Code: $vi abc echo “Enter a given no. :” read a echo “Enter a no. :” read b ans=1 while [ $b –gt 0 ] do ans=`echo $ans \* $a|bc` b=`echo $b -1|bc` done echo The value of $a rasied to the power $b is $ans Output: $ sh abc Enter a given no. : 10 Enter a no. : 2 The value of 10 rasied to the power 2 is 100
Program-8 Objective: Write a programme to display the smallest no. between three given no. Code: $vi abc echo “Enter three no. a,b and c :” read a read b read c if [ $a –gt $b –a $a –gt $c ] then echo “a is greater than b and c.” fi if [ $b –gt $a –a $b –gt $c ] then echo “b is greater than a and c.” fi if [ $c –gt $a –a $c –gt $b ] then echo “c is greater than a and b.” 14
fi Output: $ sh abc Enter three no. a,b and c : 10 20 30 c is greater than a and b.
Program-9 Objective: Write a programme to find the simple interest. Code: $vi abc echo “Enter the principle amount :” read p echo “Enter the rate of interest :” read r echo “Enter the time :” read t si=`echo ($p \* $r \* $t )/100|bc` echo “The simple interest is” $si Output: $ sh abc Enter the principle amount : 1000 Enter the rate of interest : 4 Enter the time : 1 The simple interest is 40
15