0% found this document useful (0 votes)
174 views

Cs Output

Uploaded by

Durai Jerome
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
174 views

Cs Output

Uploaded by

Durai Jerome
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Commands in UNIX

Ex. No:1 Basic Commands in UNIX

1. who
[2k10cs@localhost ~]$ who
2k10cs pts/1 2011-01-03 09:36 (172.16.1.183)
2k9cs062 pts/2 2011-01-03 09:51 (172.16.0.123)
2k9cs055 pts/3 2011-01-03 09:43 (172.16.0.116)
2k9cs058 pts/8 2011-01-03 09:54 (172.16.0.119)
2k9cs064 pts/9 2011-01-03 09:51 (172.16.0.125)
2k9cs063 pts/14 2011-01-03 09:44 (172.16.0.124)
2k9cs063 pts/10 2011-01-03 09:56 (172.16.0.124)
2k9cs051 pts/11 2011-01-03 09:54 (172.16.0.112)
[2k10cs@localhost ~]$

2. who am i
[2k10cs@localhost ~]$ who am i -H
NAME LINE TIME COMMENT
2k10cs pts/30 2011- 01-03 10:01 (172.16.1.183)
[2k10cs@localhost ~]$

3. finger
[2k10cs@localhost ~]$ finger
Login Name Tty Idle Login Time Office Office Phone
2k9cs051 pts/11 1 Jan 3 10:02 (172.16.0.112)
2k9cs052 pts/19 Jan 3 10:02 (172.16.0.113)
2k9cs053 pts/29 Jan 3 10:04 (172.16.0.114)
2k9cs054 pts/5 Jan 3 10:02 (172.16.0.115)
2k9cs055 pts/3 26 Jan 3 09:43 (172.16.0.116)
2k9cs055 pts/22 Jan 3 09:54 (172.16.0.116)
2k9cs057 pts/28 Jan 3 10:02 (172.16.0.118)
2k9cs058 pts/8 Jan 3 09:54 (172.16.0.119)
2k10cs pts/30 Jan 3 10:01 (172.16.1.183)
[2k10cs1@localhost ~]$

[2k10cs@localhost ~]$ finger 2k10cs001


Login: 2k10cs Name: (null)
Directory: /home/2k10cs001 Shell: /bin/bash
On since Mon Jan 3 09:36 (IST) on pts/1 from 172.16.1.183
35 minutes 28 seconds idle
On since Mon Jan 3 10:01 (IST) on pts/30 from 172.16.1.183
No mail.
No Plan.
[2k10cs@localhost ~]$
4. cal
[2k10cs@localhost ~]$ cal
January 2011
Su Mo Tu We Th Fr Sa
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
30 31
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$ cal 2011

[2k10cs001@localhost ~]$

[2k10cs@localhost ~]$ cal 10 2011

[2k10cs001@localhost ~]$
5. history
[2k10cs@localhost ~]$ history

[2k10cs001@localhost ~]$

6. man
[2k10cs@localhost ~]$ man

[2k10cs001@localhost ~]$

7. exit
[2k10cs@localhost ~]$ exit
8. date
[2k10cs@localhost ~]$ date
Mon Jan 3 13:43:03 IST 2011
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$
[2k10cs@localhost ~]$ date +%d
03
[2k10cs@localhost ~]$ date +%m
01
[2k10cs@localhost ~]$ date +%b
Jan
[2k10cs@localhost ~]$ date +%y
11
[2k10cs@localhost ~]$ date +%Y
2011
[2k10cs@localhost ~]$ date +%a
Mon
[2k10cs@localhost ~]$ date +%H
14
[2k10cs@localhost ~]$ date +%M
28
[2k10cs@localhost ~]$ date +%S
50
[2k10cs@localhost ~]$ date +%d-%m-%Y
20- 01-2011
[2k10cs@localhost ~]$ date “+%d %h %Y”
20 Jan 2011
[2k10cs@localhost ~]$

9. echo
[2k10cs@localhost ~]$ echo Welcome to li nux
Welcome to linux
[2k10cs@localhost ~]$

10. id
[2k10cs@localhost ~]$ id
uid=659(2k10cs001) gid=759(user) groups=759(user),901(user)
context=system_u:system_r:unconfined_t
[2k10cs@localhost ~]$

11. tty
[2k10cs@localhost ~]$ tty
/dev/pts/1
[2k10cs@localhost ~]$
12. bc
[2k10cs@localhost ~]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
5*3
15
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$ bc -q
5*6
30
[2k10cs@localhost ~]$

13. mail
[2k10cs@localhost ~]$ mail 2k10cs002
Subject hi
Hai
Cc:
[2k10cs@localhost ~]$

14. wall
[2k10cs@localhost ~]$ wall
Hai
[2k10cs@localhost ~]$

15. write
[2k10cs@localhost ~]$ write 2k10cs002
Hai
[2k10cs@localhost ~]$

16. ps
[2k10cs@localhost ~]$ ps
PID TTY TIME CMD
2712 pts/1 00:00:00 bash
5623 pts/1 00:00:00 ps
[2k10cs@localhost ~]$

17. clear
[2k10cs@localhost ~]$ clear
[2k10cs@localhost ~]$
18. uname
[2k10cs@localhost ~]$ uname
Linux
[2k10cs@localhost ~]$

19. tput cols


[2k10cs@localhost ~]$ tput cols
80
[2k10cs@localhost ~]$

20. tput clear


[2k10cs@localhost ~]$ tput clear
[2k10cs@localhost ~]$

21. tput cup

22. tput smso

23. tput rmso


24. semicolan(;)
[2k10cs@localhost ~]$ date;who am i
Fr i Jan 21 13:52:13 IST 2011
2k10cs pts/1 2011-01-21 13:05 (172.16.1.183)
[2k10cs@localhost ~]$

25. &&
[2k10cs@localhost ~]$ dat && echo "hi"
-bash: dat: command not found
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$ date && ech "hi"


Fr i Jan 21 13:48:16 IST 2011
-bash: ech: command not found
[2k10cs@localhost ~]$

26. ||
[2k10cs@localhost ~]$ dat || echo "hi"
-bash: dat: command not found
hi
[2k10cs@localhost ~]$
Ex. No:2 Working with Files
1. cat
For creating new file
[2k10cs@localhost ~]$ cat > countries
India
Pakistan
Srilanka
Australia
India
Am erica
[2k10cs@localhost ~]$

For displaying a file


[2k10cs@localhost ~]$ cat countries
India
Pakistan
Srilanka
Australia
India
Am erica
[2k10cs@localhost ~]$

2. touch
[2k10cs@localhost ~]$ touch abc
[2k10cs@localhost ~]$

3. cp
[2k10cs@localhost ~]$ cp countries country
[2k10cs@localhost ~]$ cat country
India
Pakistan
Srilanka
Australia
India
Am erica
[2k10cs@localhost ~]$

4. mv
[2k10cs@localhost ~]$ mv country countlst
[2k10cs@localhost ~]$ cat countlst
India
Pakistan
Srilanka
Australia
India
Am erica
[2k10cs@localhost ~]$

5. rm
[2k10cs@localhost ~]$ rm count.lst
[2k10cs@localhost ~]$

6. ls
[2k10cs@localhost ~]$ ls
a.c dead.letter fact.sh lock3.sh prim e.sh sumeven.sh
bc.sh digitcount.sh fibonacci.sh lock.sh sa sumnat.sh
big3.sh digitsum.sh file1.c multi.sh sample sumodd.sh
circle.sh even.sh fl.c oddeven.sh se :wq
countlst f1 lock1.sh odd.sh selva
countries fact lock2.sh oxf sema.c
[2k10cs@localhost ~]$

7. wc
[2k10cs@localhost ~]$ wc multi.sh
9 28 173 multi.sh
[2k10cs@localhost ~]$

8. sort
[2k10cs@localhost ~]$ sort countries
Am erica
Australia
India
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

9. fil e
[2k10cs@localhost ~]$ file countries
countries ascii text
[2k10cs@localhost ~]$

I/O Redirection
1. >
[2k10cs@localhost ~]$ ls > myfile
[2k10cs@localhost ~]$
2. <
[2k10cs@localhost ~]$ cat < myfile
abc
a.c
bc.sh
big3.sh
circle.sh
countlst
Fr i Jan 7 11:00:11 IST 2011
[2k10cs@localhost ~]$

3. >>
[2k10cs@localhost ~]$ date >> myfile
[2k10cs0@localhost ~]$ cat myfile
Ex. No:3 Working with Directories

1. pwd
[2k10cs@localhost ~]$ pwd
/home/2k10cs
[2k10cs@localhost ~]$

2. mkdir
[2k10cs@localhost ~]$ mkdir CPLII
[2k10cs@localhost ~]$

3. rmdir
[2k10cs@localhost ~]$ rmdir CPLII
[2k10cs@localhost ~]$

4. cd
[2k10cs@localhost ~]$ cd CPL
[2k10cs@localhost CPL]$
Ex. No:4 UNIX Editor
Starting vi editor

Inserting text
O – Opens a blank line before the current Line

o – Opens a blank line after the current Line


Copying And Pasting text

Deleting text
Ex. No:5 Pipes

1. Pipe
[2k10cs0@localhost ~]$ who|wc -l
2
[2k10cs@localhost ~]$

2. tee
[2k10cs@localhost ~]$ who|tee sample |wc
1 5 54
[2k10cs@localhost ~]$
Ex. No:6 Filters
1. head
[2k10cs@localhost ~]$ head -4 fruits
Grapes
Orange
Apple
Banana
[2k10cs@localhost ~]$

2. tail
[2k10cs@localhost ~]$ tail -4 fruits
Apple
Banana
Papaya
Pineapple
[2k10cs@localhost ~]$

3. nl
[2k10cs@localhost ~]$ nl countries
1 India Asia
2 USA America
3 China Asia
4 Canada America
5 Sudan Africa
6 India Asia
7 India Asia
[2k10cs@localhost ~]$

4. uniq
[2k10cs@localhost ~]$ sort countries -o count.lst
[2k10cs@localhost ~]$ cat count.lst
Canada Am erica
China Asia
India Asia
India Asia
India Asia
Sudan Africa
USA America
[2k10cs@localhost ~]$ uniq count.lst
Canada Am erica
China Asia
India Asia
Sudan Africa
USA America
[2k10cs@localhost ~]$
5. comm
[2k10cs@localhost ~]$ cat countries
Am erica
Asia
India
Pakistan
Srilanka
2k10cs@localhost ~]$ cat count.lst
Am erica
Asia
Canada
China
India
Srilanka
[2k10cs@localhost ~]$ comm countries count.lst
Am erica
Asia
Canada
China
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

6.diff
[2k10cs@localhost ~]$ cat countries
Am erica
Asia
India
Pakistan
Srilanka
2k10cs@localhost ~]$ cat count.lst
Am erica
Asia
Canada
China
India
Srilanka
[2k10cs@localhost ~]$ diff countries count.lst
2a3,4
> Canada
> China
4d5
< Pakistan
[2k10cs@localhost ~]$
7.cmp
[2k10cs@localhost ~]$ cmp countries count.lst
countries count.lst differ: byte 14, line 3
[2k10cs@localhost ~]$
Ex. No:7 Grep and sed commands

1. grep
[2k10cs@localhost ~]$ cat countries
India Asia
USA America
China Asia
Canada Am erica
Sudan Africa
[2k10cs@localhost ~]$
[2k10cs@localhost ~]$ grep Asia countries
India Asia
China Asia
[2k10cs@localhost ~]$

2. sed

append
Append a line after nth line
[2k10cs@localhost ~]$ sed '2a\Australia' cntry
Am erica
Asia
Australia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Append a line after every line matching the pattern


[2k10cs@localhost ~]$ sed '/Asia/a\Australia' cntry
Am erica
Asia
Australia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Append a line at the end of the file


[2k10cs@localhost ~]$ sed '$a\USA' cntry
Am erica
Asia
India
Pakistan
Srilanka
USA
[2k10cs@localhost ~]$
Replace
Replace a nth line of the file
[2k10cs@localhost ~]$ sed '2c\Australia' cntry
Am erica
Australia
Asia
Asia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Replace a line which matches the pattern


[2k10cs@localhost ~]$ sed '/Asia/c\Australia' cntry
Am erica
Australia
Australia
Australia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Replace the last line of the file


[2k10cs@localhost ~]$ sed '$c\Australia' cntry
Am erica
Asia
Asia
Asia
India
Pakistan
Australia
[2k10cs@localhost ~]$

Insert
Insert a line before the nth line of the line.
[2k10cs@localhost ~]$ sed '2i\Australia' cntry
Am erica
Australia
Asia
Asia
Asia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$
Insert a line before every line with the pattern
[2k10cs@localhost ~]$ sed '/Asia/i\Australia' cntry
Am erica
Australia
Asia
Australia
Asia
Australia
Asia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Insert a line before the last line of the file


[2k10cs@localhost ~]$ sed '$i\Australia' cntry
Am erica
Asia
Asia
Asia
India
Pakistan
Australia
Srilanka
[2k10cs@localhost ~]$

Delete

Delete Nth Line


[2k10cs@localhost ~]$ sed 4d cntry
Am erica
Asia
Asia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Delete from ith to jth line from file


[2k10cs@localhost ~]$ sed 2,6d cntry
Am erica
Srilanka
[2k10cs@localhost ~]$
Delete the line which matches the given pattern
[2k10cs@localhost ~]$ sed '/Asia/d' cntry
Am erica
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Delete the last line from input.


[2k10cs@localhost ~]$ sed '$d' cntry
Am erica
Asia
Asia
Asia
India
Pakistan
[2k10cs@localhost ~]$

Substitue
Substitute a word to another word
[2k10cs@localhost ~]$ sed 's/Asia/Australia/' cntry
Am erica
Australia
Australia
Australia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$

Substitute a word to another word at a particular location


[2k10cs@localhost ~]$ sed '2s/Asia/Australia/' cntry
Am erica
Australia
Asia
Asia
India
Pakistan
Srilanka
[2k10cs@localhost ~]$
Ex. No:8 Awk filter
1.awk

With only action


[2k10cs@localhost ~]$ awk '{print NR $0}' cntry
1America Continent
2Asia Continent
3Asia Continent
4Asia Continent
5India Country
6Pakistan Country
7Srilanka Country
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$ awk '{print NR $2}' cntry


1Continent
2Continent
3Continent
4Continent
5Country
6Country
7Country
[2k10cs@localhost ~]$

[2k10cs@localhost ~]$ awk '{print NR " - " $1 " - " $2}' cntry
1 - America - Continent
2 - Asia - Continent
3 - Asia - Continent
4 - Asia - Continent
5 - India - Country
6 - Pakistan - Country
7 - Srilanka - Country
[2k10cs@localhost ~]$

With only pattern


[2k10cs@localhost ~]$ awk '/Asia/' cntry
Asia Continent
Asia Continent
Asia Continent
[2k10cs@localhost ~]$

With both pattern and action


[2k10cs@localhost ~]$ awk '/Asia/{print NR " - " $1 }' cntry
2 - Asia
3 - Asia
4 - Asia
[2k10cs@localhost ~]$
With Field Separator
[2k10cs@localhost ~]$ cat cnt
Layer1=Apple
Layer2=Orange
Layer3=Grapes
Layer4=Mango
[2k10cs@localhost ~]$ awk -F= '/{print $1 }' cnt
Layer1
Layer2
Layer3
Layer4
[2k10cs@localhost ~]$
Ex. No:9 Security Commands

1. chmod
[2k10cs@localhost ~]$ chmod 777 myfile
[2k10cs@localhost ~]$

2. chown
[2k10cs@localhost ~]$ chown staff01 myfile
[2k10cs@localhost ~]$

3. chgrp
[2k10cs@localhost ~]$ chown staff myfile
[2k10cs@localhost ~]$

4. ls -l
[2k10cs@localhost ~]$ ls -l
total 184
-rw-r--r-- 1 2k10cs users 4 Feb 4 11:00 A1
-rw-r--r-- 1 2k10cs users 33 Feb 25 14:50 ab
-rw-r--r-- 1 2k10cs users 46 Feb 11 12:17 actor
-rw-r--r-- 1 2k10csusers 187 Feb 25 10:54 Area.sh
-rw-r--r-- 1 2k10cs users 37 Feb 11 11:37 countires
-rw-r--r-- 1 2k10csusers 0 Feb 11 12:02 countries
-rw-r--r-- 1 2k10csusers 3 Feb 11 12:11 countries1
drwxr-xr-x 2 2k10csusers 4096 Feb 18 10:24 d1
drwxr-xr-x 3 2k10cs users 4096 Feb 18 10:54 ddd
-rw-r--r-- 1 2k10cs users 77 Feb 18 10:24 f1
-rw-r--r-- 1 2k10csusers 130 Feb 25 11:26 factorial
-rw-r--r-- 1 2k10cs users 133 Feb 25 11:13 factorial.sh
-rwxrw- --- 1 2k10cs users 32 Feb 18 11:58 family
-rw-r--r-- 1 2k10cs users 37 Feb 28 14:35 file1
-rw-r--r-- 1 2k10csusers 2 Feb 18 10:10 filename
-rw------- 1 2k10cs users 704 Feb 14 14:38 m box
-rw-r--r-- 1 2k10cs users 115 Mar 2 16:13 modes
-rw-r--r-- 1 2k10csusers 247 Feb 25 11:43 prim e
-rw-r--r-- 1 2k10cs users 141 Feb 25 11:58 prim e.sh
[2k10cs@localhost ~]$

5. passwd
[2k10cs@localhost ~]$ passwd
Changing password for user 2k10cs.
Changing password for 2k10cs
(current) UNIX password:
passwd: Authentication token manipulation error
[2k10cs@localhost ~]$
Shell Programming
Ex. No:10 Area and Circumference of the Circle
Program:
echo Enter the radius of the circle
read r
area=$(echo "3.14*$r*$r"|bc)
circum=$(echo "2*3.14*$r"|bc)
echo The area of the circle is $area
echo The circumference of the circle is $circum

Output:
[2k10cs@localhost ~]$ sh circle.sh
Enter the radius of the circle
8
The area of the circle is 200.96
The circumference of the circle is 50.24
[2k10cs@localhost ~]$
Ex. No:11 Factorial of a number

Program:
echo Enter a number
read n
fact=1
for ((i=1; i<=$n; i++))
do
fact=$(echo "$fact*$i"|bc)
done
echo The factorial of $n is $fact

Output:
[2k10cs@localhost ~]$ sh fact.sh
Enter a number
7
The factorial of 7 is 5040
[2k10cs@localhost ~]$
Ex. No:12 Prime number or not
Program:
echo Enter the Number
read n
count=0
for ((i=1; i<=$n; i++))
do
rem=$(echo "$n%$i"|bc)
if test $rem -eq 0
then
count=$(echo "$count + 1"|bc)
fi
done
if test $count -eq 2
then
echo The given number is prime
else
echo The given number is not prime
fi

Output:
[2k10cs@localhost ~]$ sh prime.sh
Enter the Number
37
The given number is prime
[2k10cs@localhost ~]$
Ex. No:13 Multiplication table
Program:
echo Enter the multiplication table number
read mulno
echo Enter the limit
read lmt
for ((i=1; i<=lmt; i++))
do
val=$(echo "$i*$mulno"|bc)
echo $i \* $mulno = $val
done

Output:
[2k10cs@localhost ~]$ sh multi.sh
Enter the multiplication table number
8
Enter the limit
10
1*8=8
2 * 8 = 16
3 * 8 = 24
4 * 8 = 32
5 * 8 = 40
6 * 8 = 48
7 * 8 = 56
8 * 8 = 64
9 * 8 = 72
10 * 8 = 80
[2k10cs@localhost ~]$
Ex: No:14 Odd number or even number
Program:
echo Enter the number
read n
rem=$(echo "$n%2"|bc)
if test $rem -e q 0
then
echo The given number is even
else
echo The given number is odd
fi

Output:
[2k10cs@localhost ~]$ sh oddeven.sh
Enter the number
743
The given number is odd
[2k10cs@localhost ~]$
Ex: No:15 Odd Series
Program:
echo Enter the range
read ran
st=1
echo The Odd Series is
for((i=1; i<=$ran; i++))
do
echo $st
st=$(echo "$st+2"|bc)
done

Output:
[2k10cs@localhost ~]$ sh odd.sh
Enter the range
6
The Odd Series is
1
3
5
7
9
11
[2k10cs@localhost ~]$
Ex: No:16 Even Series
Program:
echo Enter the range
read ran
st=2
echo The Even Series is
for((i=1; i<=$ran; i++))
do
echo $st
st=$(echo "$st+2"|bc)
done

Output:
[2k10cs@localhost ~]$ sh even.sh
Enter the range
5
The Even Series is
2
4
6
8
10
[2k10cs@localhost ~]$
Ex: No:17 Sum of N odd numbers
Program:
echo Enter the range
read ran
st=1
sum=0
for((i=1; i<=$ran; i++))
do
sum=$(echo "$sum+$st"|bc)
st=$(echo "$st+2"|bc)
done
echo The sum of the $ran odd numbers is $sum

Output:
[2k10cs@localhost ~]$ sh sumodd.sh
Enter the range
5
The sum of the 5 odd num bers is 25
[2k10cs@localhost ~]$
Ex: No:18 Sum of N even numbers
Program:
echo Enter the range
read ran
st=2
sum=0
for((i=1; i<=$ran; i++))
do
sum=$(echo "$sum+$st"|bc)
st=$(echo "$st+2"|bc)
done
echo The sum of the $ran even numbers is $sum

Output:
[2k10cs@localhost ~]$ sh sumeven.sh
Enter the range
5
The sum of the 5 even numbers is 30
[2k10cs@localhost ~]$
Ex: No:19 Sum of N natural numbers
Program:
echo Enter the range
read ran
st=1
sum=0
for((i=1; i<=$ran; i++))
do
sum=$(echo "$sum+$st"|bc)
st=$(echo "$st+1"|bc) done
echo The sum of the $ran natural numbers is $sum

Output:
[2k10cs@localhost ~]$ sh sumnat.sh
Enter the range
5
The sum of the 5 natural numbers is 15
[2k10cs@localhost ~]$
Ex: No:20 Fibonacci series
Program:
echo Enter the range
read ran
f0=-1
f1=1
echo The fibonacci series is
for((i=1; i<=$ran; i++))
do
f2=$(echo "$f0+$f1"|bc)
echo $f2
f0=$f1
f1=$f2
done

Output:
[2k10cs@localhost ~]$ sh fibonacci.sh
Enter the range
7
The fibonacci series is
0
1
1
2
3
5
8
[2k10cs@localhost ~]$
Ex: No:21 Counting number of digits in a number
Program:
echo Enter a number
read n
count=0
while test $n -ne 0
do
count=$(echo "$count+1"|bc)
n=$(echo "$n/10"|bc)
done
echo The Number of digits in a given number is $count

Output:
[2k10cs@localhost ~]$ sh digitcount.sh
Enter a number
12345
The Number of digits in a given number is 5
[2k10cs@localhost ~]$
Ex: No:22 Sum of digits of a number
Program:
echo Enter a number
read n
sum=0
while test $n -ne 0
do
d=$(echo "$n%10"|bc)
sum=$(echo "$sum+$d"|bc)
n=$(echo "$n/10"|bc)
done
echo The Sum of digits in a given number is $sum

Output:
[2k10cs@localhost ~]$ sh digitsum .sh
Enter a number
12345
The Sum of digits in a given number is 15
[2k10cs@localhost ~]$
Ex: No:23 Biggest of three numbers
Program:
echo Enter three numbers
read a b c
if [ $a -ge $b -a $a -ge $c ]
then
echo $a is big
elif [ $b -ge $c ]
then
echo $b is big
else
echo $c is big
fi

Output:
[2k10cs@localhost ~]$ sh big3.sh
Enter three numbers
23 43 12
43 is big
[2k10cs@localhost ~]$
Ex: No:24 Arithmetic operation using case
Program:
echo Menu
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo Enter your choice
read cho
if [ $cho -ge 1 -a $cho -le 4 ]
then
echo "Enter num1 and num2"
read num1 num2
case $cho in
1)
res=$(echo "$num1+$num2"|bc)
echo "The sum of $num1 and $num2 is $res"
;;
2)
res=$(echo "$num1-$num2"|bc)
echo "The difference of $num1 and $num2 is $res"
;;
3)
res=$(echo "$num1*$num2"|bc)
echo "The multiplication of $num1 and $num2 is $res"
;;
4)
res=$(echo "$num1/$num2"|bc)
echo " $num1 / $num2 is $res"
;;
esac
else
echo "Invalid Choice"
fi

Output:
[2k10cs@localhost ~]$ sh arithmetic.sh
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
1
Enter num1 and num2
10 13
The sum of 10 and 13 is 25
[2k10cs@localhost ~]$
Ex: No:25 Merging two files and Sorting using Command
Line Arguments
Program:
cat $1 > $3
cat $2 >> $3
echo "The two file $1 and $2 are merged to $3"
echo " "
echo "The content of $3 is"
cat $3
echo " "
echo "The content of $3 after sorting is"
sort $3 -o $3
cat $3

Output:
[2k10cs001@localhost ~]$ cat file1
Unix commands
[2k10cs001@localhost ~]$ cat file2
Shell Programming
[2k10cs@localhost ~]$ sh merge.sh file1 file2 file3
The two file file1 and file2 are merged to file3

The content of file3 is


Unix commands
Shell Programming

The content of file3 after sorting is


Shell Programming
Unix commands
[2k10cs@localhost ~]$
C Programming in
UNIX
Ex: No:26 Simulation of fork System call
Program:
#include<stdio.h>
main()
{
int fpid;
printf("Before forking ...\n");
fpid = fork();
if (fpid == 0)
{
printf("Child Process fpid=%d\n", fpid);
}
else
{
printf("Parent Process fpid=%d\n", fpid);
}
printf("After forking fpid=%d\n", fpid);
}

Output:
[2k10cs@localhost ~]$ cc fork1.c –o fork1
[2k10cs@localhost ~]$ ./fork1
Before forking ...
Child Process fpid=0
After forking fpid=0
Parent Process fpid=26333
After forking fpid=26333
[2k10cs@localhost ~]$
Ex: No:27 Simulation of signal
Program:
#include <stdio.h>
#include <signal.h>
#define MAX_i 10000
#define DIVISOR 1000
#define MAX_j 20000
void main()
{
int i;
unsigned long j, sum;
signal(SIGINT, SIG_IGN);
printf("CPU-bound loop started. Pressing Ctrl-C has no effect....\n");
for(i=1;i<=MAX_i;i++)
{
sum=0;
for(j=0;j<=MAX_j;j++)
{
sum += j;
}
if(i%DIVISOR == 0)
{
printf("Iteration %d, sum = %ld\n", i, sum);
}
}
printf("Computation is done.\n\n");
signal(SIGINT, SIG_DFL);
printf("CPU-bound loop restarted.Pressing Ctrl-C HAS effect now...\n");
for(i=1;i<=MAX_i;i++)
{
sum=0;
for(j=0;j<=MAX_j;j++)
{
sum += j;
}
if(i%DIVISOR == 0)
{
printf("Iteration %d, sum = %ld\n", i, sum);
}
}
printf("Computation is done.\n");
}
Output:
[2k10cs@localhost cprg]$ cc signal.c –o signal
[2k10cs@localhost cprg]$ ./signal
CPU-bound loop started. Pressing Ctrl-C has no effect....
Iteration 1000, sum = 200010000
Iteration 2000, sum = 200010000
Iteration 3000, sum = 200010000
Iteration 4000, sum = 200010000
Iteration 5000, sum = 200010000
Iteration 6000, sum = 200010000
Iteration 7000, sum = 200010000
Iteration 8000, sum = 200010000
Iteration 9000, sum = 200010000
Iteration 10000, sum = 200010000
Computation is done.

CPU-bound loop restarted.Pressing Ctrl-C HAS effect now...


Iteration 1000, sum = 200010000
Iteration 2000, sum = 200010000
Iteration 3000, sum = 200010000
Iteration 4000, sum = 200010000
Iteration 5000, sum = 200010000
Iteration 6000, sum = 200010000

[2k10cs@localhost cprg]$
Ex: No:28 File handling – Copy the content of one
file to another file
Program:
#include<stdio.h>
#include<fcntl.h>
main()
{
FILE *fp1,*fp2;
char c,srcflname[35],tarflname[35];
printf("\nEnter the source file:");
scanf("%s",srcflname);
printf("\nEnter the target file :");
scanf("%s",tarflname);
fp1=fopen(srcflname,"r");
fp2=fopen(tarflname,"w");
if(fp1==NULL)
{
printf("\nCannot open the file %s",srcflname);
}
else if(fp2==NULL)
{
printf("\nCannot open the file %s", tarflname);
}
else
{
while(!fe of(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
printf("\nThe file is copied");
}
fclose(fp1);
fclose(fp2);
}
Output:
[2k10cs@localhost ~]$ cat fruits
Mango
Apple
Orange
[2k10cs@localhost ~]$ cc filecpy.c –o filecpy
[2k10cs@localhost ~]$ ./filecpy
Enter the source file:fruits

Enter the target file:fr uitscpy

The file is copied[2k10cs@localhost ~]$ cat fruitscpy


Mango
Apple
Orange
[2k10cs@localhost ~]$ cat fruits
Mango
Apple
Orange
[2k10cs@localhost ~]$
Content beyond
syllabus
Ex: No:29 Running UNIX commands from C
Program:
#include<stdio.h>
main()
{
system("clear");
printf("Current date\n");
system("date");
printf("\n");
printf("The number of users who are currently logged in are \n");
system("who|wc -l");
printf("\n");
printf("Your login id is \n");
system("whoami");
}

Output:
[2k10cs@localhost ~]$ cc unxcmd.c - unxcm d
[2k10cs@localhost ~]$ ./unxcmd
Curr ent date
Mon Jan 31 11:27:21 IST 2011

The number of users who are currently logged in are


30

Your login id is
2k10cs
[2k10cs@localhost ~]$
Ex: No:30 Simulation of wait system call
Program:
#include<stdio.h>
#include<unistd.h>
int main()
{
int pid, status;
if(fork())
{
printf("\nI'm the parent, and waiting");
pid=wait(&status);
printf("\nI'm the parent. \n - my child's PID is %d \n - my child's exit status is %d\n",pid,status);
}
else
{
printf("\nI'm the child, and sleeping");
sleep(2);
printf("\nI'm the child, and exiting");
exit(0);
}
}

Output:
[2k10cs@localhost cprg]$ cc wait.c - o wait

[2k10cs@localhostcprg]$ ./wait

I'm the child, and sleeping


I'm the child, and exitingI'm the parent, and waiting
I'm the parent.
- my child's PID is 6158
- my child's exit status is 0
[2k10cs@localhost cprg]$

You might also like