Você está na página 1de 53

BHARATI VIDYAPEETH‟S INSTITUTE

OF
COMPUTER APPLICATIONS & MANAGEMENT
(A-4, PACHIM VIHAR, NEW DELHI-110063)

“Linux & X – WindowsProgramming”


(MCA-252)
Practical File

Submitted By: Submitted To:


Shubhangi Tandon Mr. VaibhavSinghal
MCA IV Sem Asst. Pofessor,BVICAM
01811604409

1
01811604409
INDEX
Sr. Description Date Pg. No Remark
No
1. Shell Commands (1-10) 5
2. Shell Commands (11-20) 5-7
3. Shell Commands (21-30) 7-9
4. Shell Commands (31-40) 9-11
5. Shell Commands (41-55) 11-16

1. Write a shell program to add, subtract, multiply and divide of two no. 18

2. Write a shell program to calculate simple interest. 19


3. Write a shell program to calculate the x^y using bc. 20
4. Write a shell program to generate a Fibonacci series. 21
5. Write a shell program to calculate the % of marks. 22
6. Write a shell program to copy one file to another file. 23
7. Write a shell program to print odd numbers between 1 to 100. 24
8. Script to find a file or directory which has been last modified. 24
9. Write a shell program to generate a sequence 1, 4, 9, 16, upto 10 times. 25
10. Write a shell program to generate a sequence 1 5 10 17 upto 10 times. 25
11. Write a shell script which renames all .txt file as .text files in the current 26
directory.
12. Write a shell script to print the table from 2 to 50 but print in horizontal manner. 26
13. Write a shell script to take a process id from user and either kill it or change its 28
priority with nice command show the proper message for priority.
14. Create a interactive program to display top 10 command using switch case. 29
15. Assuming date as 15-02-2011 write a program to display the date in the following 31
manner :
February 15,two zero one one ( Tuseday) ( hint: use +%b,d,y,a etc).
16. Write a shell program to Program to check that the given number is palindrome or 32
not.
17. Write a shell program to Program to check that the given number is Armstrong 33
number or not.
18. Write a shell program to wish “ Good Morning”, “Good Afternoon”,”Good Evening 34
” using system date.
19. A company insures its drivers in the following cases : 35
if the driver is married
if the driver is unmarried m male & above 30 years of age.
if the driver is unmarried, female & above 25 years of age.
In all other cases, the driver is not insured , if the marital status, sex,and age of the
driver are the inputs, write a shell program to determine whether the driver is to
be insured or not. without using logical operator.
20. Write a shell Program to add series 1/1!+2/2!+3/3!+4/4!+5/5!….n/n!. 35
21. Write a shell program to calculate the sum of the following series upto 50 terms 36
sum= -13+33-53+73-93 +113-…………
22. Shell Program to print the given series pattern (no of lines=5) 37
1
1238
2
01811604409
123
1234
12345
23. Shell Program to print the given series pattern. 38
1
121
12321
1234321
123454321
24. Write a shell program to make a list of student in a file record contains roll no, 39
age, name, city. Roll no should start with 1 and should be automatically
incremented by 1 for each new record.
25. Check if file name provided as a command line argument represents the regular 40
file, directory, character special file, block special file, symbolic link, pipe and
socket.
26. Accept a string on command line. If it represents a file then show contents, if it is a 41
directory show directory listing else display error message.
27. Write a shell script which takes a name as parameter and returns the PID(s) of 41
processes with that name.
28. Write a shell program which takes a file name as parameter and check whether 42
the current user has write access on it or not.
29. Write a shell program to enter employee name, address and salary of employees 42
in a file emp.dat with each field separated by a colon. Also check that employee
name should not be null and salary should not be less than equal to zero.

3
01811604409
Shell
Commands List

4
01811604409
1. Create a directory of yourname in student folder and given permission to them.
[student@localhost ~]$ mkdir newdir
[student@localhost ~]$ chmod 777 newdir

2. Copy a dir from ./bin to ./student /yournamedir.


It can't be created.

3. Create a file having the calendar of 2011 name as cal.txt.


cal 2011 > cal.txt

4. Find the present Directory.


$pwd
/home/student

5. Find the inode number of a file.


$ls -i b
91 b

6. Move the content of a file to another.


$mv b c

7. Add the content of one file to another ( append).


$cat > a
1
$cat > b
abc
$cat a >> b
$cat b
abc
1

8. Count the no of the files in a folder.


[student@localhost ~]$ ls | wc -l
18

9. Write a few commands available in /bin and /sbin directory.


/bin => cat , ls , cp, date
/sbin => fsck , fdisk, halt, reboot

10. Find the command to check disk usages.


$du –total -h
11G total

11. Find the command to add the user.


#useradd Shubhangi -p abcdef
#userdel

5
01811604409
12. Rename the test directory to testing.
[student@localhost ~]$ mkdir test
[student@localhost ~]$ mv test testing
[student@localhost ~]$ ls
1* 2 4 a.out* Documents/ file1 file3 Music/ Templates/ tmp/
1.c 3 a/ Desktop/ Downloads/ file2 file4 Pictures/ testing/ Videos/
[student@localhost ~]$

13. Copy multiple file to a folder name as copyfolder from any location.
[student@localhost ~]$ mkdir ab ; cp 1 2 3 4 /home/student/ab

14. Jump to the your own directory.


[student@localhost ~]$ cd ..
[student@localhost home]$ cd ..
[student@localhost /]$ cd $pwd
[student@localhost ~]$

15. Calculate the 2 ^100 through a command and save in a file.


[student@localhost ~]$ bc |cat >file1
2^100
^Z
[2]+ Stopped bc | cat > file1
[student@localhost ~]$ cat file1
1267650600228229401496703205376

16. Find a command to know your terminal.


[student@localhost ~]$ tty
/dev/pts/1

17. How you can display a message through a command.


[student@localhost ~]$ wall hello
hellocast message from student@localhost (pts/1) (Wed Feb 2 10:17:34 2011):

18. How many users are currently login on your system.


[student@localhost ~]$ who
student :0 2011-02-02 09:25
student pts/0 2011-02-02 09:25
student pts/1 2011-02-02 09:26
student pts/2 2011-02-02 10:08

19. First create test1 and test2 directory and then Remove both a single command.
[student@localhost ~]$ mkdir test1
[student@localhost ~]$ mkdir test2
[student@localhost ~]$ ls
1 a.out* Documents/ f1 f3 file1 Music/ Pictures/ Templates/ test2/ two
2 Desktop/ Downloads/ f2 f4 hello.c one pinka/ test1/ tmp/ Videos/
[student@localhost ~]$ rmdir test1 test2
6
01811604409
[student@localhost ~]$ ls
1 a.out* Documents/ f1 f3 file1 Music/ Pictures/ Templates/ two
2 Desktop/ Downloads/ f2 f4 hello.c one pinka/ tmp/ Videos/

20. Display the list of the file in a tree structure.


[student@localhost ~]$ tree
.
|-- 1.c
|-- 4
|-- Desktop
| |-- Home2.desktop
| |-- Welcome.desktop
| |-- register.desktop
| |-- trash.desktop
| `-- upgrade.desktop
|-- Documents
| `-- snapshot_sambha
| |-- snapshot1.png
| |-- snapshot2.png
| |-- snapshot3.png
| |-- snapshot4.png
| |-- snapshot5.png
| |-- snapshot6.png
| `-- snapshot7.png
|-- Downloads

21. Remove multiple files through a single command.


[student@localhost ~]$ rm -f 1 2

22. Compare two files through ( hint: cmp).


[student@localhost ~]$ cat > 1
hello
[student@localhost ~]$ cat > 2
hEllo
[student@localhost ~]$ cmp 1 2
1 2 differ: byte 2, line 1

23. Create a file having content, hello and make the copy of it.
[student@localhost ~]$ cat > 3
hello
[student@localhost ~]$ cp 3 4
[student@localhost ~]$ ls
1 3 Desktop/ Downloads/ Pictures/ tmp/
2 4 Documents/ Music/ Templates/ Videos/

7
01811604409
24. Check what is common in both the file ( hint: comm) .
[student@localhost ~]$ comm 1 2
hello
hEllo
[student@localhost ~]$ comm 3 4
hello

25. Command to Check the owner id .


[student@localhost ~]$ id
uid=10001(student) gid=10001(student) groups=10001(student)

26. Command to check the process running on the machine.


[student@localhost ~]$ ps
PID TTY TIME CMD
14267 pts/1 00:00:00 bash
19823 pts/1 00:00:00 kwrite
19826 pts/1 00:00:00 ps

27. Command to check who is logged onto this terminal.


[student@localhost ~]$ who
student :0 2011-02-01 12:38
student pts/0 2011-02-01 12:39
student pts/1 2011-02-01 12:48
[student@localhost ~]$ who am i
student pts/1 2011-02-01 12:48
[student@localhost ~]$ whoami
student

28. Command to check one line summary of system status. ( hint: uptime)
[student@localhost ~]$ uptime
13:01:54 up 26 min, 3 users, load average: 0.03, 0.06, 0.06

29. Check the command info. and write some important detail on notebook.

8
01811604409
30. Command to remove the history of the system.
[student@localhost ~]$ cat > .bash_history

31. Command to check the files from a dir using wildcard characters.
[student@localhost ~]$ cat > .bash_history

commands to check files from a dir using wildcard characters


[student@localhost ~]$ ls f*
file1
[student@localhost ~]$ ls file?
file1
[student@localhost ~]$ ls fil?1
file1
[student@localhost ~]$ ls fi??1
file1
[student@localhost ~]$ dir
1 3 Desktop Downloads Music Templates Videos
2 4 Documents file1 Pictures tmp
[student@localhost ~]$ dir D*
Desktop:
Home2.desktop register.desktop upgrade.desktop
linux2.odt trash.desktop Welcome.desktop
[student@localhost ~]$ dir D?sk*
Home2.desktop register.desktop upgrade.desktop
linux2.odt trash.desktop Welcome.desktop
[student@localhost ~]$ dir Desk*
Home2.desktop register.desktop upgrade.desktop
linux2.odt trash.desktop Welcome.desktop
[student@localhost ~]$ dir Deskto?
Home2.desktop register.desktop upgrade.desktop
linux2.odt trash.desktop Welcome.desktop

32. Create file1,file2……..file 10 with same data and display them using [1…10] ( hint : ls).
[student@localhost ~]$ cat > file1
hello
[student@localhost ~]$ cat > file2
hello
[student@localhost ~]$ cat > file3
hello
[student@localhost ~]$ cat > file4
hello
[student@localhost ~]$ cat file[1-4]
hello
hello
hello
hello

9
01811604409
33. Check all option of wc command on a file.
WC: Short for word count, wc displays a count of lines, words, and characters in a file.
Syntax:
wc [-c | -m | -C ] [-l] [-w] [ file ... ]

-c Count bytes.
-m Count characters.
-C Same as -m.
-l Count lines.
-w Count words delimited by white space characters or new line characters.
Delimiting characters are Extended Unix Code (EUC) characters from any code
set defined by iswspace()
File Name of file to word count.

Examples :
wc myfile.txt - Displays information about the file myfile.txt. Below is an example of the
OUTPUT.
5 13 57 myfile.txt

5 = Lines
13 = Words
57 = Characters
34. Can we remove a directory which having some file without removing files, remove it.
[student@localhost ~]$ mkdir a
[student@localhost ~]$ cp 1 2 3 4 a
[student@localhost ~]$ rm -rf a

35. Command to summarize free space on disk filesystems (hint : df).


[student@localhost ~]$ df --total
Filesystem Size Used Avail Use% Mounted on
/dev/sda6 12G 3.1G 8.2G 28% /
/dev/sda8 251G 337M 251G 1% /home
/dev/sda1 98G 13G 85G 13% /media/windows
total 361G 17G 344G 5%

36. How a run a c program of hello using a command. ( hint : cc ./a.out).


[student@localhost ~]$ cat > 1.c
#include <stdio.h>
int main ()
{
printf ("hello");
10
01811604409
}
[student@localhost ~]$ cc 1.c
[student@localhost ~]$ ./a.out
hello[student@localhost ~]$

37. Find the process id and kill them ( hint: kill) check option through man.
[student@localhost ~]$ ps
PID TTY TIME CMD
14267 pts/1 00:00:00 bash
19823 pts/1 00:00:00 kwrite
19826 pts/1 00:00:00 ps
[student@localhost ~]$ kill -9 19823
[student@localhost ~]$ ps
PID TTY TIME CMD
14267 pts/1 00:00:00 bash
20934 pts/1 00:00:00 ps
[1]+ Killed kwrite

38. Changing the priority of a job through nice command.


[student@localhost ~]$ nice -n 18 bc
[student@localhost ~]$ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 10001 14267 14265 0 80 0 - 1148 wait pts/1 00:00:00 bash
0 S 10001 27221 14267 0 91 11 - 16950 poll_s pts/1 00:00:00 kwrite
0 T 10001 31965 14267 0 98 18 - 667 signal pts/1 00:00:00 bc
0 R 10001 32649 14267 0 80 0 - 628 - pts/1 00:00:00 ps

39. Command to create a folder name is today's date.


[student@localhost ~]$ mkdir `date +%F`
[student@localhost ~]$ ls
08/ 11:46:13/ 2011/ 8/ AM/ Documents/ Feb/ file3 hello.txt Templates/
Tue/
11:44:25/ 16:45:29/ 2011-02-08/ a/ a.out* Downloads/ file1 file4 Music/ testing/
UTC/
11:45:53/ 1.c 4 ab/ Desktop/ EST/ file2 filename Pictures/ tmp/ Videos/

40. Create a file (using touch) and put some data then append it with all word count options.
[student@localhost ~]$ touch file3 ; ls -al > file3 ; echo "lines" >> file3 ; wc -l file3 >> file3;
echo "words" >> file3; wc -w file3 >> file3 ; echo "chars" >> file3 ; wc -m file3 >> file3

[student@localhost ~]$ cat file3


drwxr-xr-x 2 student student 4096 2011-01-11 10:59 Videos/
-rw------- 1 student student 164 2011-02-08 11:39 .Xauthority
-rw------- 1 student student 10132 2011-02-08 12:05 .xsession-errors
lines
70 file3
words
11
01811604409
550 file3
chars
4241 file3

41. Create a tar file and make the maximum zip of that file and save the size difference in a
file.

[student@localhost ~]$ ls -l Feb1.tar | tr -s " " | cut -d " " -f5 > one ; ls -l Feb.tar.gz | tr -s " " |
cut -d " " -f5 > two ; echo -n "The difference is : " ; expr `cat one` - `cat two`
The difference is : 10043

42. Print the today date with a proper message and save it into a file too (using tee).
[student@localhost ~]$ echo "today is `date`" | tee file
today is Tue Feb 8 12:26:01 EST 2011
[student@localhost ~]$ cat file
today is Tue Feb 8 12:26:01 EST 2011

43. Aliases a command (using aliases).


[student@localhost ~]$ alias list='ls'
[student@localhost ~]$ list
total 160
drwxr-xr-x 2 student student 4096 2011-02-08 11:46 08
drwxr-xr-x 2 student student 4096 2011-02-08 11:44 11:44:25
drwxr-xr-x 2 student student 4096 2011-02-08 11:45 11:45:53
drwxr-xr-x 2 student student 4096 2011-02-08 11:46 11:46:13
drwxr-xr-x 2 student student 4096 2011-02-08 11:45 16:45:29
-rw-r--r-- 1 student student 54 2011-02-02 09:30 1.c
drwxr-xr-x 2 student student 4096 2011-02-08 11:46 2011
drwxr-xr-x 2 student student 4096 2011-02-08 11:59 2011-02-08

44. Put 20 lines in a file using vi editor and show the use of head and tail command.
[student@localhost ~]$ vi myfile
[student@localhost ~]$ cat myfile
hello
this is sample file
using vi editor
one
two
three
four
five
six
seven
eight
nine
ten
[student@localhost ~]$ head -5 myfile
12
01811604409
hello
this is sample file
using vi editor
one
two
[student@localhost ~]$ tail -5 myfile
seven
eight
nine
ten

45. Show all process with their nice priority.


[student@localhost ~]$ ps -el
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 1 0 0 80 0 - 449 poll_s ? 00:00:00 init
1 S 0 2 0 0 75 -5 - 0 kthrea ? 00:00:00 kthreadd
1 S 0 3 2 0 -40 - - 0 migrat ? 00:00:00 migration/0
1 S 0 4 2 0 75 -5 - 0 ksofti ? 00:00:00 ksoftirqd/0
1 S 0 5 2 0 -40 - - 0 migrat ? 00:00:00 migration/1
1 S 0 6 2 0 75 -5 - 0 ksofti ? 00:00:00 ksoftirqd/1
1 S 0 7 2 0 75 -5 - 0 worker ? 00:00:00 events/0
1 S 0 8 2 0 75 -5 - 0 worker ? 00:00:00 events/1
1 S 0 9 2 0 75 -5 - 0 worker ? 00:00:00 khelper
1 S 0 10 2 0 75 -5 - 0 async_ ? 00:00:00 async/mgr

46. Run a simple process and Kill it.


[student@localhost ~]$ kwrite &
[1] 10883
[student@localhost ~]$ ps
PID TTY TIME CMD
3573 pts/1 00:00:00 bash
10883 pts/1 00:00:00 kwrite
11129 pts/1 00:00:00 ps
[student@localhost ~]$ kill -9 10883
bash: kill: (10883) - No such process
[1]+ Killed kwrite

47. Write the steps to add a user and give some important option using man useradd.
Answer :-

User can be added through Konsole(CUI) as well as using userdrake(GUI)

Using command line:


1. first switch to super user (su), it will prompt for root password
2. write useradd user name with -p option for password and user will be added

Using userdrake:
13
01811604409
1. Click add user button
2. give the required fields details
3. and click ok, user will be added

48. Write the steps to modify a user and give some important option using man usermodify.
Answer :-

User can be modified using userdrake as follows :-


1. using edit button, we can edit the properties of user like: password, group etc...
2. edit will open a popup dialog and will ask for properties to change
3. we can add new group also, by first adding a group, and then choose edit option and select
group
4. User can be present in multiple groups also
5. but in that case, permission of user will be in others.

49. Write the steps to delete a user and give some important option using man userdelete.
Answer :-

userdel - Deleting a user


Options:
-r (remove home directory) Files in the user's home directory will be removed along with the
home directory itself and the user's mail spool. Files located in other file systems will have to be
searched for and deleted manually.

Example: To remove the user 'roger' and his home directory


userdel -r roger
50. Write the steps to mount a drive.
Answer :-

1. first check how many drives are mounted using df or using mount
$df
$mount
2. switch to root/super user
$su
password:
3. prompt will change to #, use ls to list /dev which can be mounted

//mounting c:\
#mount /dev/sda1 /media/windows

//mounting usb
#mount /dev/sdb1 /home/USB

//mounting floppy
#mount /dev/fd0 /home/floppy

51. Write the steps to unmount a drive.


14
01811604409
Answer :-

1. first check how many drives are mounted using df or using mount
$df
$mount
2. switch to root/super user
$su
password:
3. prompt will change to #, now unmount the drive using unmount or umount command with the
path being provided

unmounting c:\
#umount /media/windows

unmounting usb
#umount /home/USB

52. Write all searching procedure through ls – l using all wide card characters(ls
myfile*[Enter].
[student@localhost ~]$ ls -l file*
-rw-r--r-- 1 student student 38 2011-02-08 12:26 file
-rw-r--r-- 1 student student 6 2011-02-01 13:24 file1
-rw-r--r-- 1 student student 39 2011-02-08 12:08 file2
-rw-r--r-- 1 student student 4252 2011-02-08 12:17 file3
-rw-r--r-- 1 student student 6 2011-02-01 13:25 file4
-rw-r--r-- 1 student student 49 2011-02-08 12:30 file5
-rw-r--r-- 1 student student 84 2011-02-08 11:50 filename
[student@localhost ~]$ ls -l file?
-rw-r--r-- 1 student student 6 2011-02-01 13:24 file1
-rw-r--r-- 1 student student 39 2011-02-08 12:08 file2
-rw-r--r-- 1 student student 4252 2011-02-08 12:17 file3
-rw-r--r-- 1 student student 6 2011-02-01 13:25 file4
-rw-r--r-- 1 student student 49 2011-02-08 12:30 file5
[student@localhost ~]$ touch f1c.a1zt
[student@localhost ~]$ ls [a-z][1-9]?.?[1-9]?*
f1c.a1zt

53. Use sort command to sort the content of a file.


[student@localhost ~]$ cat > file5
one
two
three
four
five
six
seven
eight
15
01811604409
nine
ten
[student@localhost ~]$ sort file5
eight
five
four
nine
one
seven
six
ten
three
two

54. Create a text file having some algebraic expression to be solved and then calculate that file
using bc and save that output into result.txt.
[student@localhost ~]$ cat >exp
1+2*3+10/2
[student@localhost ~]$ cat exp | bc > result.txt
[student@localhost ~]$ cat result.txt
12
Or

[student@localhost ~]$ bc > result.txt | cat > exp


1+2*3+10/2
[student@localhost ~]$ cat result.txt
12

55. Use echo command to display message There are ___ files in the current directory and
show the difference of “ ”, and „ „ in echo command.
[student@localhost ~]$ echo "There are `ls -l| grep -c '^-'` files in this directory "
there are 33 files in this directory
[student@localhost ~]$ echo 'There are `ls -l| grep -c '^-'` files in this directory '
there are `ls -l| grep -c ^-` files in this directory

16
01811604409
Shell Programs
List

17
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 03411604409
Program : Write a shell program to add, subtract, multiply and divide of
two no.
**********************************************************************************/

echo "Enter first number : "


read n1
echo "Enter second number : "
read n2
echo "Sum : "
echo `expr $n1 + $n2`
echo "Difference : "
echo `expr $n1 - $n2`
echo "Product : "
echo `expr $n1 \* $n2`
echo "Division : "
echo `expr $n1 / $n2`

OUTPUT :

[student@localhost ~]$ sh program1.sh


Enter first number :
20
Enter second number :
10
Sum :
30
Difference :
10
Product :
200
Division :
2

18
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 03411604409
Program : Write a shell program to calculate simple interest.
**********************************************************************************/

echo "Enter principal : "


read p
echo "Enter rate of interest : "
read r
echo "Enter time period : "
read t
echo "Simple interest : "
echo `expr $p \* $r \* $t / 100`

OUTPUT :

[student@localhost ~]$ sh program2.sh


Enter principal :
1000
Enter rate of interest :
10
Enter time period :
5
Simple interest :
500

19
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to calculate the x^y using bc.
**********************************************************************************/

echo "Enter x : "


read x
echo "Enter y : "
read y
echo "x^y : "
echo $x ^ $y | bc

OUTPUT :

[student@localhost Shubhangi]$ sh program3.sh


Enter x :
2
Enter y :
6
x^y :
64

20
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a Fibonacci series.
**********************************************************************************/

echo "Enter number of terms of fibonacci series : "


read n
a=0
b=1
i=3
echo $a
echo $b

while [ $i -le $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done

OUTPUT :

[student@localhost Shubhangi]$ sh program4.sh


Enter number of terms of fibonacci series :
10
0
1
1
2
3
5
8
13
21
34
[student@localhost Shubhangi]$

21
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to calculate the % of marks.
**********************************************************************************/

echo "Enter marks in first subject : "


read n1
echo "Enter marks in second subject : "
read n2
echo "Percentage : "
echo `expr \( $n1 + $n2 \) / 2`

OUTPUT :

[student@localhost Shubhangi]$ sh program5.sh


Enter marks in first subject :
20
Enter marks in second subject :
30
Percentage :
25
[student@localhost Shubhangi]$

22
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to copy one file to another file.
**********************************************************************************/

echo "Enter marks in first subject : "


read n1
echo "Enter marks in second subject : "
read n2
echo "Percentage : "
echo `expr \( $n1 + $n2 \) / 2`

OUTPUT :

[student@localhost ~]$ sh program6.sh


Enter data in file pd
1
2
3
4
5
6
7
8
9
10
Copying data to a new file pd1.txt
Contents of file pd1 are :
1
2
3
4
5
6
7
8
9
10
[student@localhost ~]$

23
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to print odd numbers between 1 to 100.
**********************************************************************************/

echo "Printing the ODD numbers between 1 to 100..."


for (( i=1;i<100;i++ ))
do
a=`expr $i % 2 `
if [ $a -ne 0 ]
then
echo -n " " $i
fi
done

OUTPUT :

[student@localhost Shubhangi]$ sh ques7.sh


Printing the ODD numbers between 1 to 100...
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43
45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85
87 89 91 93 95 97 99
[student@localhost Shubhangi]$

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Script to find a file or directory which has been last modified.
**********************************************************************************/

echo "File or directory that has been last modified : "


ls -t | head -1

OUTPUT :

[student@localhost ~]$ sh program8.sh


File or directory that has been last modified :
program8.sh
[student@localhost ~]$

24
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a sequence 1, 4, 9, 16,
upto 10 times.
**********************************************************************************/

echo "The required sequence is as follows :"


for (( i=1 ; i <=10; i++))
do
echo `expr $i \* $i`
done

OUTPUT :

[student@localhost Shubhangi]$ sh prog9.sh


The required sequence is as follows :
1
4
9
16
25
36
49
64
81
100
[student@localhost Shubhangi]$

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a sequence 1 5 10 17
upto 10 times.
**********************************************************************************/

echo "The required sequence is as follows :"


echo 1
for ((i=2 ; i<=10 ; i++))
do
echo `expr $i \* $i + 1`
done

OUTPUT :

[student@localhost Shubhangi]$ sh prog10.sh


The required sequence is as follows :
1
5
10
17
26
37
50
65
82
101

25
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell script which renames all .txt file as .text files
in the current directory.
**********************************************************************************/

for i in `ls | grep ".txt$"`


do
b="`ls $i | cut -d "." -f1`"
a=`echo "$b.text"`
mv $i $a
done

OUTPUT :

[student@localhost ~]$ kwrite filetry2.sh


QThreadStorage: Thread 0x830b538 exited after QThreadStorage 2147483639 destroyed
[student@localhost ~]$ sh filetry2.sh
[student@localhost ~]$ ls *.text
aa.text a.text b.text hello.text last_modified.text

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell script to print the table from 2 to 50 but print in
horizontal manner.
**********************************************************************************/

echo "Table from 2 to 50 are as follows : "


for (( i=2;i<=50;i++ ))
do
for (( j=1;j<=10;j++ ))
do
echo -n " " `expr $i \* $j`
done
echo " "
done

26
01811604409
OUTPUT :

[student@localhost Shubhangi]$ sh program12.sh


Table from 2 to 50 are as follows :
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
11 22 33 44 55 66 77 88 99 110
12 24 36 48 60 72 84 96 108 120
13 26 39 52 65 78 91 104 117 130
14 28 42 56 70 84 98 112 126 140
15 30 45 60 75 90 105 120 135 150
16 32 48 64 80 96 112 128 144 160
17 34 51 68 85 102 119 136 153 170
18 36 54 72 90 108 126 144 162 180
19 38 57 76 95 114 133 152 171 190
20 40 60 80 100 120 140 160 180 200
21 42 63 84 105 126 147 168 189 210
22 44 66 88 110 132 154 176 198 220
23 46 69 92 115 138 161 184 207 230
24 48 72 96 120 144 168 192 216 240
25 50 75 100 125 150 175 200 225 250
26 52 78 104 130 156 182 208 234 260
27 54 81 108 135 162 189 216 243 270
28 56 84 112 140 168 196 224 252 280
29 58 87 116 145 174 203 232 261 290
30 60 90 120 150 180 210 240 270 300
31 62 93 124 155 186 217 248 279 310
32 64 96 128 160 192 224 256 288 320
33 66 99 132 165 198 231 264 297 330
34 68 102 136 170 204 238 272 306 340
35 70 105 140 175 210 245 280 315 350
36 72 108 144 180 216 252 288 324 360
37 74 111 148 185 222 259 296 333 370
38 76 114 152 190 228 266 304 342 380
39 78 117 156 195 234 273 312 351 390
40 80 120 160 200 240 280 320 360 400
41 82 123 164 205 246 287 328 369 410
42 84 126 168 210 252 294 336 378 420
43 86 129 172 215 258 301 344 387 430
44 88 132 176 220 264 308 352 396 440
45 90 135 180 225 270 315 360 405 450
46 92 138 184 230 276 322 368 414 460
47 94 141 188 235 282 329 376 423 470
48 96 144 192 240 288 336 384 432 480
49 98 147 196 245 294 343 392 441 490
50 100 150 200 250 300 350 400 450 500
[student@localhost Shubhangi]$

27
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell script to take a process id from user and either
kill it or change its priority with nice command show the proper message for
priority.
**********************************************************************************/

echo "Displaying the all processes:"


ps -a
echo "Enter the process id of your choice from the above list :"
read id
echo "1) Kill the process"
echo "2) Change the priority"

echo "Enter your choice:"


read ch
case $ch in
1) kill -9 $id ;;
2) echo "Enter the priority to be set :"
read prior
echo "Enter the command whose priority is to be changed:"
read com

#changing the priority of the command


nice -n $prior $com &;;
*) echo "Wrong choice entered...";;
esac

OUTPUT :

[student@localhost ~]$ sh prog13.sh


Displaying the all processes:
PID TTY TIME CMD
8253 pts/1 00:00:00 kwrite
12131 pts/1 00:00:00 sh
12132 pts/1 00:00:00 ps
Enter the process id of your choice from the above list :
12131
1) Kill the process
2) Change the priority
Enter your choice:
2
Enter the priority to be set :
11
Enter the command whose command is to be changed:
kwrite
[student@localhost ~]$ QThreadStorage: Thread 0xa01c538 exited after QThreadStorage
2147483639 destroyed
exit
There are stopped jobs.

28
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Create a interactive program to display top 10 command using
switch case.
**********************************************************************************/

echo "Displaying top 20 commands using switch case :"

echo "Choice are as follows : "

echo "1. pwd"


echo "2. date"
echo "3. ls"
echo "4. cal"
echo "5. ps"
echo "6. dir"
echo "7. du"
echo "8. df"
echo "9. id"
echo "10. tree"
echo "11.history 5"
echo "12.who"
echo "13. who am i"
echo "14. tty"
echo "15. uptime"
echo "16. wc a.txt"
echo "17. diff a.txt b.txt"
echo "18. comm a.txt b.txt"
echo "19. cp a.txt new_a.txt"
echo "20. rm a.txt"

echo "Enter your choice (1 to 20)"


read ch

case $ch in
1)pwd;;
2)date;;
3)ls;;
4)cal;;
5)ps;;
6)dir;;
7)du;;
8)df;;
9)id;;
10)tree;;
11)history 5;;
12)who;;
13)who am i;;
14)tty;;
15)uptime;;
16)wc a.txt;;
17)diff a.txt b.txt;;
18)comm a.txt b.txt;;
19)cp a.txt new_a.txt;;
20)rm a.txt;;
*)echo "Wrong choice";;
esac

OUTPUT :

[student@localhost Shubhangi]$ sh program14.sh


29
01811604409
Displaying top 20 commands using switch case :
Choice are as follows :
1. pwd
2. date
3. ls
4. cal
5. ps
6. dir
7. du
8. df
9. id
10. tree
11.history 5
12.who
13. who am i
14. tty
15. uptime
16. wc a.txt
17. diff a.txt b.txt
18. comm a.txt b.txt
19. cp a.txt new_a.txt
20. rm a.txt
Enter your choice (1 to 20)
1
/home/student/Shubhangi
[student@localhost Shubhangi]$ sh program14.sh
Displaying top 20 commands using switch case :
Choice are as follows :
1. pwd
2. date
3. ls
4. cal
5. ps
6. dir
7. du
8. df
9. id
10. tree
11.history 5
12.who
13. who am i
14. tty
15. uptime
16. wc a.txt
17. diff a.txt b.txt
18. comm a.txt b.txt
19. cp a.txt new_a.txt
20. rm a.txt
Enter your choice (1 to 20)
4
February 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

30
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Assuming date as 15-02-2011 write a program to display the date
in the following manner :
February 15,two zero one one ( Tuseday) ( hint: use +%b,d,y,a etc).
**********************************************************************************/

declare -a arr
d=`date +%d`
m=`date +%B`
y=`date +%Y`
i=0
echo -n $m $d ","
while [ $y -gt 0 ]
do
div=`expr $y % 10`
y=`expr $y / 10`
arr[i]=$div
i=`expr $i+1`
done
i=`expr $i-1`
for (( j=$i;j>=0;j-- ))
do
case ${arr[j]} in

0) echo -n "zero ";;


1) echo -n "one ";;
2) echo -n "two ";;
3) echo -n "three ";;
4) echo -n "four ";;
5) echo -n "five ";;
6) echo -n "six ";;
7) echo -n "seven ";;
8) echo -n "eight ";;
9) echo -n "nine ";;
esac
done
day=`date +%A`
echo -n "("$day")"
echo ""

OUTPUT :

[student@localhost ~]$ sh date.sh


February 22 ,two zero one one (Tuesday)

31
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to Program to check that the given number
is palindrome or not.
**********************************************************************************/

echo "Enter a number : "


read num

n=$num
r=0
q=1
rev=0

while [ $q -ne 0 ]
do
rev=`expr $rev \* 10`
q=`expr $n / 10`
r=`expr $n % 10`
rev=`expr $rev + $r`
n=$q
done

if [ $num == $rev ]
then
echo "Number is a palindrome."
else
echo "Number is not a palindrome."
fi

OUTPUT :

[student@localhost Shubhangi]$ sh program16.sh


Enter a number :
121
Number is a palindrome.
[student@localhost Shubhangi]$

32
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to Program to check that the given number
is Armstrong number or not.
**********************************************************************************/

echo -n "Enter the number : "


read num
arm=0
new=$num
while [ $num -ne 0 ]
do
r=`expr $num % 10`
arm=`expr $r \* $r \* $r + $arm`
num=`expr $num / 10`
done
if [ $new -eq $arm ]
then
echo "Entered Number is Armstrong..."
else
echo "Entered Number is not Armstrong..."
fi

OUTPUT :

[student@localhost Shubhangi]$ sh ques17.sh


Enter the number : 153
Entered Number is Armstrong...
[student@localhost Shubhangi]$

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to wish “ Good Morning”, “Good
Afternoon”,”Good Evening ” using system date.
**********************************************************************************/

h=`date +%H`
if [ $h -lt 12 ]
then
echo "Good morning"
elif [ $h -ge 12 -a $h -le 15 ]then
echo "Good afternoon"
elif [ $h -gt 15 -a $h -le 20 ]
then
echo "Good evening"
else
echo ""
fi

OUTPUT :

[student@localhost ~]$ sh wish.sh


Good afternoon

33
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : A company insures its drivers in the following cases :
- if the driver is married
- if the driver is unmarried m male & above 30 years of age.
- if the driver is unmarried, female & above 25 years of age.

In all other cases, the driver is not insured , if the marital status, sex,and age
of the driver are the inputs, write a shell program to determine whether the driver
is to be insured or not. without using logical operator.
**********************************************************************************/

echo "Enter the Name of the Employee : "


read name
echo "Enter the Age of the Employee : "
read age
echo "Enter the Gender of the Employee(M/F) : "
read gender
echo "Enter the Marital Status of the Employee(MA / UM) : "
read status
if [ $status = MA -o $status = UM -a $age -ge 30 -a $gender = M -o $status = UM -a
$gender = F -a $age -ge 25 ]
then
echo "Employee is Insured by the Company..."
else
echo "Employee is NOT Ensured by the Company..."
fi

OUTPUT :

[student@localhost Shubhangi]$ sh prog19.sh


Enter the Name of the Employee :
Rohan
Enter the Age of the Employee :
30
Enter the Gender of the Employee(M/F) :
M
Enter the Marital Status of the Employee(MA / UM) :
UM
Employee is Insured by the Company...
[student@localhost Shubhangi]$

34
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell Program to add series
1/1!+2/2!+3/3!+4/4!+5/5!….n/n!.
**********************************************************************************/

echo "Enter the value of n: "


read n
sum=0
for (( i=1; i<=$n; i++ ))
{
fact=1
for (( x=$i; x>=1; x--))
do
fact=`expr $fact \* $x`
done
sum=`expr $sum + $i / $fact`
}
echo "sum = " $sum

OUTPUT :

[student@localhost ~]$ sh facts.sh


Enter the value of n:
5
sum = 2

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell script which takes a name as parameter and returns
the PID(s) of processes with that name.
**********************************************************************************/

a=`ps | tr -s " " | cut -d " " -f5 | grep -n ^$1 | cut -d ":" -f1`

ps | head -3 | tail -1 | cut -d " " -f1

OUTPUT :

[student@localhost ~]sh shell27.sh bash


12719
[student@localhost ~]sh shell27.sh ps
12748
[student@localhost ~]

35
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to calculate the sum of the following
series upto 50 terms
sum= -13+33-53+73-93 +113-…………
**********************************************************************************/

echo "The sum of the required series is : "

sum1=0
sum2=0
result=0

for (( i=1;i<=5;i=`expr $i + 4` ))
do
sum1=`expr $sum1 + $i \* $i \* $i`
done

echo "Sum of negative terms is : -" $sum1

for (( i=3;i<=7;i=`expr $i + 4` ))
do
sum2=`expr $sum2 + $i \* $i \* $i`
done

echo "Sum of positive terms is : " $sum2

result=`expr $sum2 - $sum1`

echo "Sum of series is : " $result

OUTPUT :

[student@localhost Shubhangi]$ sh program21.sh


The sum of the required series is :
Sum of negative terms is : - 126
Sum of positive terms is : 370
Sum of series is : 244
[student@localhost Shubhangi]$

36
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Shell Program to print the given series pattern (no of lines=5)
1
12
123
1234
12345
**********************************************************************************/

echo "The required pattern is : "


for (( i=1;i<=5;i++ ))
do
for ((j=1;j<=i;j++ ))
do
echo -n $j
done
echo " "
done

OUTPUT :

[student@localhost Shubhangi]$ sh program23.sh


The required pattern is :
1
12
123
1234
12345
[student@localhost Shubhangi]$

37
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Shell Program to print the given series pattern.
1
121
12321
1234321
123454321
**********************************************************************************/

echo "The required pattern is : "


for (( i=1;i<=10;i=`expr $i + 2` ))
do
for (( j=20;j>i;j-- ))
do
echo -n " "
done
for (( k=1;k<=`expr $i / 2 + 1`;k++ ))
do
echo -n $k
echo -n " "
done
for (( l=$i;l>=k;l-- ))
do
echo -n `expr $l - $i + $k - 2`
echo -n " "
done
echo " "
done

OUTPUT :

[student@localhost Shubhangi]$ sh program24.sh


The required pattern is :
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
[student@localhost Shubhangi]$

38
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to make a list of student in a file record
contains roll no, age, name, city. Roll no should start with 1 and should be
automatically incremented by 1 for each new record.
**********************************************************************************/

echo "Enter student details : "

choice=y
rollno=1

while [ $choice = y ]
do
echo "Enter name : "
read name
echo "Age : "
read age
echo "City : "
read city
rollno=`expr $rollno + 1`
echo "$rollno | $name | $age | $city " | cat >> infofile

echo "Do you want more records to enter (y/n) : "


read choice
done

cat infofile

OUTPUT :

[student@localhost ~]sh shell26.sh


Enter student details :
Enter name :
ABC
Age :
21
City :
Delhi
Do you want more records to enter (y/n) :
y
Enter name :
DEF
Age :
22
City :
Kanpur
Do you want more records to enter (y/n) :
n
2 | ABC | 21 | Delhi
3 | DEF | 22 | Kanpur
[student@localhost ~]

39
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Check if file name provided as a command line argument represents
the regular file, directory, character special file, block special file, symbolic
link, pipe and socket.
**********************************************************************************/

echo "Scanning command line arguments : "

for i in $*
do
a=`ls -l $i | cut -d "r" -f1`
if [ $a == 'c' ]
then
echo "$i is character device."
elif [ $a == 'b' ]
then
echo "$i is block device."
elif [ $a == 'p' ]
then
echo "$i is pipe."
elif [ $a == 's' ]
then
echo "$i is socket."
elif [ $a == 'l' ]
then
echo "$i is link."
fi
done

OUTPUT :

[student@localhost ~]sh shell.sh /dev/initctl /dev/stdin /dev/mem


Scanning command line arguments :
/dev/initctl is pipe.
/dev/stdin is link.
/dev/mem is character device.

40
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Accept a string on command line. If it represents a file then
show contents, if it is a directory show directory listing else display error
message.
**********************************************************************************/

a=`ls -l | tr -s " " | cut -d " " -f9 | grep -n ^$1 | cut -d ":" -f1`
b=`ls -l | head -$a | tail -1 | cut -c1`

if [ $b == - ]
then
cat $1
elif [ $b == d ]
then
ls $1
else
echo "Error"
fi

/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program which takes a file name as parameter and
check whether the current user has write access on it or not.
**********************************************************************************/

a=`ls -l $1 | cut -d " " -f1 | cut -c3`


if [ $a == w ]
then
echo "User has write permissions."
else
echo "User does not have write permissions."
echo $a
fi

OUTPUT :

[student@localhost ~]sh shell28.sh Simple2.c


User has write permissions.

41
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to enter employee name, address and salary
of employees in a file emp.dat with each field separated by a colon. Also check
that employee name should not be null and salary should not be less than equal to
zero.
**********************************************************************************/

echo "Employee name : "


read name
echo " Employee salary : "
read sal
echo " Employee address : "
read add

if [ -z $name ]
then
echo "Name is blank."
exit 1
fi

if [ $sal -le 0 ]
then
echo "Salary = 0"
exit 1
fi

echo "$name : $sal : $add " | cat >> emp.dat

42
01811604409
XWindows
Programs

43
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a X-wundows program in C language to show the simple window on the screen.
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#define false 0
#define true 1

int main ()
{
Display *display;

display= XOpenDisplay (NULL);

int iScreenNum= DefaultScreen (display);

int iDisplayWidth= DisplayWidth (display, iScreenNum);


int iDisplayHeight= DisplayHeight (display, iScreenNum);
int iWidth= iDisplayWidth/2;
int iHeight= iDisplayHeight/2;
int iXPos= 0;
int iYPos= 0;

int done= false;

int iBlackColor= BlackPixel (display, iScreenNum);


int iWhiteColor= WhitePixel (display, iScreenNum);

Window win= XCreateSimpleWindow (


display,
DefaultRootWindow (display),
iXPos, iYPos, iWidth, iHeight, 0,
iBlackColor, iWhiteColor);

GC gc= XCreateGC (display, win, 0, NULL);

XStoreName (display, win, "Welcome to X Windows");


XMapWindow (display, win);

XSelectInput (display, win, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |


Button1MotionMask);

XEvent event;
while (!done)

44
01811604409
{
XNextEvent (display, &event);

XDrawString (display, win, gc, 10, 10, "Hello World", strlen("Hello World"));
XDrawString (display, win, gc, 100, 100, "ABC", 3);

}
XCloseDisplay (display);
return 0;
}

45
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a X-wundows program in C language to show the use of graphics component on
the screen(use of line,rectangle,arc,point etc).
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#define false 0
#define true 1

int main ()
{
Display *display;

display= XOpenDisplay (NULL);

int iScreenNum= DefaultScreen (display);

int iDisplayWidth= DisplayWidth (display, iScreenNum);


int iDisplayHeight= DisplayHeight (display, iScreenNum);
int iWidth= iDisplayWidth/2;
int iHeight= iDisplayHeight/2;
int iXPos= 0;
int iYPos= 0;

int done= false;

int iBlackColor= BlackPixel (display, iScreenNum);


int iWhiteColor= WhitePixel (display, iScreenNum);

Window win= XCreateSimpleWindow (


display,
DefaultRootWindow (display),
iXPos, iYPos, iWidth, iHeight, 0,
iBlackColor, iWhiteColor);

GC gc= XCreateGC (display, win, 0, NULL);

XStoreName (display, win, "Welcome to X Windows");


XMapWindow (display, win);

XSelectInput (display, win, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |


Button1MotionMask);

46
01811604409
XEvent event;
while (!done)
{
XNextEvent (display, &event);
XDrawRectangle ( display , win , gc , 100 , 100 , 300 , 300 );
XPoint points[] = { 100,100,150,100,150,150,200,220 };
XDrawLines ( display , win , gc , points, 4, 0 );
XDrawPoint ( display , win , gc , 200 , 200 ) ;
XDrawString (display, win, gc, 10, 10, "Hello World", strlen("Hello World"));
XDrawString (display, win, gc, 100, 100, "ABC", 3);

}
XCloseDisplay (display);
return 0;
}

47
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a X-wundows program in C language to show a button and after pressing it, the
window should disappear.
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#define false 0
#define true 1

int main ()
{
Display *display;

display= XOpenDisplay (NULL);

int iScreenNum= DefaultScreen (display);

int iDisplayWidth= DisplayWidth (display, iScreenNum);


int iDisplayHeight= DisplayHeight (display, iScreenNum);
int iWidth= iDisplayWidth/2;
int iHeight= iDisplayHeight/2;
int iXPos= 0;
int iYPos= 0;

int done= false;

int iBlackColor= BlackPixel (display, iScreenNum);


int iWhiteColor= WhitePixel (display, iScreenNum);

Window win= XCreateSimpleWindow (


display,
DefaultRootWindow (display),
iXPos, iYPos, iWidth, iHeight, 0,
iBlackColor, iWhiteColor);
Window clear= XCreateSimpleWindow (
display,
win,
200, 200, 50, 50, 2,
iBlackColor, iWhiteColor);
GC gc= XCreateGC (display, win, 0, NULL);

XStoreName (display, win, "Welcome to X Windows");


XMapWindow (display, win);
XMapWindow (display, clear);

48
01811604409
XSelectInput (display, win, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |
Button1MotionMask);
XSelectInput (display, clear, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask
);

XEvent event;
while (!done)
{
XNextEvent (display, &event);
XDrawString (display, win, gc, 10, 10, "Hello World", strlen("Hello World"));
XDrawString (display, win, gc, 100, 100, "ABC", 3);

if(event.xany.window==clear){

if(event.type==ButtonPress){
done=true;
}
}
XDrawString (display, clear, gc, 12, 12, "clear", 5);
}
XCloseDisplay (display);
return 0;
}

49
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a X-wundows program in C language to handle different events of keyboard and
mouse and show the appropriate message.
**********************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#define false 0
#define true 1

int main ()
{
Display *display;

display= XOpenDisplay (NULL);

int iScreenNum= DefaultScreen (display);

int iDisplayWidth= DisplayWidth (display, iScreenNum);


int iDisplayHeight= DisplayHeight (display, iScreenNum);
int iWidth= iDisplayWidth/2;
int iHeight= iDisplayHeight/2;
int iXPos= 0;
int iYPos= 0;

int done= false;

int iBlackColor= BlackPixel (display, iScreenNum);


int iWhiteColor= WhitePixel (display, iScreenNum);

Window win= XCreateSimpleWindow (


display,
DefaultRootWindow (display),
iXPos, iYPos, iWidth, iHeight, 0,
iBlackColor, iWhiteColor);

GC gc= XCreateGC (display, win, 0, NULL);

XStoreName (display, win, "Welcome to X Windows");


XMapWindow (display, win);

XSelectInput (display, win, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |


Button1MotionMask);

50
01811604409
XEvent event;
while (!done)
{
XNextEvent (display, &event);
switch (event.type)
{
case ButtonPress:
XDrawString (display, win, gc, 200, 200, "hii u pressed a button", strlen("hii u
pressed a button"));
break;
case KeyPress:
XDrawString (display, win, gc, 300, 300, "hello u pressed a key",strlen("hello u
pressed a key") );
break;

case MotionNotify:
XDrawLine(display, win, gc, event.xbutton.x ,event.xbutton.y ,150,150);
break;
}
XDrawString (display, win, gc, 10, 10, "Hello World", strlen("Hello World"));
}
XCloseDisplay (display);
return 0;
}

51
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a X-wundows program in C language to handle different type of fonts and fill
colours in some drawing object.
**********************************************************************************/

//#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#define false 0
#define true 1

int main ()
{
Display *display;

Colormap cmap;
XColor xc, xc2;
display= XOpenDisplay (NULL);
int iScreenNum= DefaultScreen (display);
unsigned long background, border;
XGCValues values,value1;

int iDisplayWidth= DisplayWidth (display, iScreenNum);


int iDisplayHeight= DisplayHeight (display, iScreenNum);
int iWidth= iDisplayWidth/4;
int iHeight= iDisplayHeight/2;
int iXPos= 0;
int iYPos= 0;
int done= false;

int iBlackColor= BlackPixel (display, iScreenNum);


int iWhiteColor= WhitePixel (display, iScreenNum);

cmap = DefaultColormap(display, iScreenNum);


XAllocNamedColor(display, cmap, "Blue", &xc, &xc2);
values.foreground = xc.pixel;
XAllocNamedColor(display, cmap, "Green", &xc, &xc2);

value1.foreground = xc.pixel;
printf ( "%d", iScreenNum );

Window win= XCreateSimpleWindow (display,

DefaultRootWindow (display),
iXPos, iYPos, iWidth, iHeight, 3,
iBlackColor, iWhiteColor);

52
01811604409
GC gc = XCreateGC(display, win,4,&values);
GC gc1 = XCreateGC(display, win,4,&value1);

XStoreName (display, win, "Welcome to X Windows");


XMapWindow (display, win);
XSelectInput (display, win, ExposureMask | KeyPressMask |ButtonPressMask |

StructureNotifyMask);

XDrawLine (display, win, gc, 100, 100, 200, 200);


XEvent event;

while (!done)
{
XNextEvent (display, &event);
if(event.type == ButtonPress)
{
XDrawLine (display, win, gc, 100, 100, 150, 100);

XDrawRectangle (display, win,gc, 200,200, 225, 225);

XFillRectangle (display, win, gc1, 10, 10, 51, 51);

XFillArc (display, win, gc, 60, 60, 30, 30, 0, 23040);

}
}
XCloseDisplay (display);
return 0;
}

53
01811604409

Você também pode gostar