Professional Documents
Culture Documents
OF
COMPUTER APPLICATIONS & MANAGEMENT
(A-4, PACHIM VIHAR, NEW DELHI-110063)
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
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
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
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/
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
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
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
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
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
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
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
47. Write the steps to add a user and give some important option using man useradd.
Answer :-
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 :-
49. Write the steps to delete a user and give some important option using man userdelete.
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
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
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
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.
**********************************************************************************/
OUTPUT :
18
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 03411604409
Program : Write a shell program to calculate simple interest.
**********************************************************************************/
OUTPUT :
19
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to calculate the x^y using bc.
**********************************************************************************/
OUTPUT :
20
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a Fibonacci series.
**********************************************************************************/
while [ $i -le $n ]
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
i=`expr $i + 1`
done
OUTPUT :
21
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to calculate the % of marks.
**********************************************************************************/
OUTPUT :
22
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to copy one file to another file.
**********************************************************************************/
OUTPUT :
23
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to print odd numbers between 1 to 100.
**********************************************************************************/
OUTPUT :
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Script to find a file or directory which has been last modified.
**********************************************************************************/
OUTPUT :
24
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a sequence 1, 4, 9, 16,
upto 10 times.
**********************************************************************************/
OUTPUT :
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell program to generate a sequence 1 5 10 17
upto 10 times.
**********************************************************************************/
OUTPUT :
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.
**********************************************************************************/
OUTPUT :
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Write a shell script to print the table from 2 to 50 but print in
horizontal manner.
**********************************************************************************/
26
01811604409
OUTPUT :
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.
**********************************************************************************/
OUTPUT :
28
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Create a interactive program to display top 10 command using
switch case.
**********************************************************************************/
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 :
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
OUTPUT :
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.
**********************************************************************************/
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 :
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.
**********************************************************************************/
OUTPUT :
/**********************************************************************************
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 :
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.
**********************************************************************************/
OUTPUT :
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!.
**********************************************************************************/
OUTPUT :
/**********************************************************************************
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`
OUTPUT :
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-…………
**********************************************************************************/
sum1=0
sum2=0
result=0
for (( i=1;i<=5;i=`expr $i + 4` ))
do
sum1=`expr $sum1 + $i \* $i \* $i`
done
for (( i=3;i<=7;i=`expr $i + 4` ))
do
sum2=`expr $sum2 + $i \* $i \* $i`
done
OUTPUT :
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
**********************************************************************************/
OUTPUT :
37
01811604409
/**********************************************************************************
Name : Shubhangi Tandon
Enrollment No. : 01811604409
Program : Shell Program to print the given series pattern.
1
121
12321
1234321
123454321
**********************************************************************************/
OUTPUT :
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.
**********************************************************************************/
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
cat infofile
OUTPUT :
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.
**********************************************************************************/
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 :
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.
**********************************************************************************/
OUTPUT :
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.
**********************************************************************************/
if [ -z $name ]
then
echo "Name is blank."
exit 1
fi
if [ $sal -le 0 ]
then
echo "Salary = 0"
exit 1
fi
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;
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;
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;
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;
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;
value1.foreground = xc.pixel;
printf ( "%d", iScreenNum );
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);
StructureNotifyMask);
while (!done)
{
XNextEvent (display, &event);
if(event.type == ButtonPress)
{
XDrawLine (display, win, gc, 100, 100, 150, 100);
}
}
XCloseDisplay (display);
return 0;
}
53
01811604409