100% found this document useful (1 vote)
306 views

Assignment Using VI-EDITOR Do The Following Exercise: Assignment-Linux Class - Bca Ii YEAR (2016-17)

This document contains an assignment on Linux commands and shell scripting for a BCA second year class from 2016-2017. It includes exercises on using vi editor commands, Linux commands to manipulate files and directories, and shell script programs to perform tasks like sorting files, checking file types, calculating factorials, and swapping numbers. The assignment is authored by Yashwant Kumar and contains 16 questions to write shell script programs.

Uploaded by

Pranav Shukla P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
306 views

Assignment Using VI-EDITOR Do The Following Exercise: Assignment-Linux Class - Bca Ii YEAR (2016-17)

This document contains an assignment on Linux commands and shell scripting for a BCA second year class from 2016-2017. It includes exercises on using vi editor commands, Linux commands to manipulate files and directories, and shell script programs to perform tasks like sorting files, checking file types, calculating factorials, and swapping numbers. The assignment is authored by Yashwant Kumar and contains 16 questions to write shell script programs.

Uploaded by

Pranav Shukla P
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

ASSIGNMENT

Using VI-EDITOR do the following exercise


1. In a file:-
i. Replace the word ‘has’ with ‘has not’
Coding:-
To replace the word use the following ex mode command:
:1,Ss/has/has not/g

ii. Locate 20th character in a file.


Coding:-
H #to go to top of file
20| # to reach 20th character

iii. Sort lines 21 to 40 in a file using vi-editor.


Coding:-
To sort lines use the following ex mode command
:21,40 !sort

2. In a file copy/cut and paste following text:-


i. At ith line, n lines to jth line.
Coding:-
Copy lines 3 to 7 and paste them at line 11 in a file using vi-editor
3G # to go to 3rd line
4yy # to copy 4 line
11G # to go to 11th line
p # to paste

ii. Yank a few words.

Coding:-

To yank a few words to new position use following command mode


command:
5yw
P

AUTHOR NAME- YASHWANT KUMAR Page 1


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

3. Open two files ‘txtfile’ and ‘newfile’ and copy/cut 5 lines from
txtfile and paste them in newfile using vi-editor.
Coding:-

$ vi txtfile newfile # to open 2 files


“a5yy # to copy 5 lines in buffer
:e new file # switch to newfile
“ap # to paste

4. Open ‘txtfile’ and copy/cut following and paste to the ‘newfile’.


I. Ith to the last line in it
Coding:-

15L # to reach 15th line to the last line (command mode0


“add # to cut line into buffer(command mode)
:e newfile # to switch to newfile (ex mode)
“ap # to paste (command mode)

5. Create macro
i. To paste your name at any position in the file
Coding:-
To create the macro use the following ex mode command:
:map M bhawana

ii. To remove all leading spaces in file


Coding:-
To create the macro use the following ex mode command:
:map r : 1,$s/ //c

iii. To save and quit vi-editor in input mode


Coding:-
To create the macro use the following ex mode command:
:map! Q ^M: wq

AUTHOR NAME- YASHWANT KUMAR Page 2


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

WRITE COMMANDS
i. List all files that match a class.
Coding:-
Use the following command:
$ grep –l “[aA]c[tT]iv” *.*

ii. List all files that do not match a class.


Coding:-
Use the following command:
$ grep –L “[aA][cC]tive” *.*

iii. Change the file permissions.


Coding:-
Use the following command:
$ chmod
g+w+x,o+x,a+r sort

iv. Display the lines in a file that contain a particular word.


Coding:-
$ grep”echo” Search

v. Append the contents of two files in a file JABC.


Coding:-
To append the content of files in other file we will use the following shell
commands:
$ cat txtfile newfile | cat>>JABC

vi. Count the number of files in a directory.


Coding:-
To count the number of files in a directory use the following command:
$ ls |we –l

AUTHOR NAME- YASHWANT KUMAR Page 3


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Write Shell programs


i. Display all users currently logged in detail with column headers.

Coding:-
echo
tput clear
echo
echo “Current Users:
$who –Hu
echo

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 4


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

ii. List all files in current directory and save the list in a file ABC.
Also save the contents of the files in ABC and display the contents in
ABC in sorted order.

Coding:-
echo
tput clear
echo
ls | cat>ABC
sort | cat ABC
echo

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 5


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

iii. Sort the contents of a file ABC and save it in OABC.


Coding:-
tput clear
echo
sort ABC | cat>OABC
cat OABC
echo

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 6


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

iv. To save current date and time, number of files & directories in the
current directory and contents of all the files to a single file NFL.

Coding:-
tput clear
echo Today’s date is : `/bin/date`>Nfl
echo Number of file in current directory is : `/bin/ls|we –l`>>Nfl
for x in `/bin/ls`
do
if [ -f $x ]; then
echo $x>>Nfl
cat $x>>Nfl
fi
done
Output:-

AUTHOR NAME- YASHWANT KUMAR Page 7


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

v. To input a number and test whether it is +ve, -ve or zero


Coding:-
tput clear
echo “ Enter any no: “
read num
if [ $num -lt 0 ] ; then
echo “ The no is negative “
elif [ $ num -gt 0 ] ; then
echo “ The no is positive “
else
echo “ The no is zero “
fi

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 8


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

vi. To test whether a file name is a regular file or a directory or of


other type

Coding:-
tput clear
echo “ Enter the file name: “
read file
if [ !-e$file ] ; then
echo “File does not exit!!”
elif [ -d$file ] ; then
echo “It is a directory.”
elif [ -f$file ] ; then
echo “It is a regular file”
else
echo “It is of other type”
fi

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 9


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

vii. To list only the directories in current path.


Coding:-
tput clear
echo “The directories in current path are:”
for f in `ls`
do
if [ -d $f ] ; then
echo $f
fi
done

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 10


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

viii.To print the greatest of three numbers.


Coding:-
tput clear
echo “Enter 1st number:”
read a
echo “Enter 2nd number:”
read b
echo “Enter 3rd number:”
read c
echo
if [ $a –ge $b ]; then
if [ $a –gt $c ] ; then
echo “$a is greatest.”
else
echo “$c is greatest number”
fi
else
if [ $b –gt $c ] ; then
echo “$b is greatest number”
else
echo “$c is greatest number”
fi
fi

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 11


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

ix. To print 12 terms of Fibonacci series.


Coding:-
echo “12 terms of Fibonacci series are”
let a=0 b=1
echo $a
echo $b
let n=1
while [ $n –ne 12 ]
do
let c=a+b
echo $c
let a=b
let b=c
let n=n+1
done

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 12


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

x. To display all users currently logged in and also check particular


user every 30 second until he logs in.

Coding:-
echo
echo
tput clear
echo
echo
who –Hu
until [ who | grep “BCA2” ]
do
sleep 30
done
echo “ $ just logged in ”
echo

AUTHOR NAME- YASHWANT KUMAR Page 13


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Output:-

xi. To save current date & time, number of files in the current
directory and contents of all the files matching a pattern to a single file
NPFL.

Coding:-
echo
echo
echo today’s date is `date`>NPEL
echo number of files in the directory is `ls|wc –l` >>NPEL
grep –l ‘for’ `ls`>l
for x in `cat l`
do
cat $x>>NPEL
done
echo
echo

AUTHOR NAME- YASHWANT KUMAR Page 14


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Output:-

xii. To display particular messages depending on the weekday.


Coding:-
echo “Today’s date is `date +%D` ”
echo
echo “Today’s day is `date +%A` ”
echo
case “ `date +%u` ” in
1) echo “It is my working day”;;
2) echo “It is resting day”;;
3) echo “It is mid day”;;
4) echo “It is very expensive time”;;
5) echo “It is cool day”;;;
6) echo “It is about to weekend”;;
7) echo “It is weekend”
Esac

AUTHOR NAME- YASHWANT KUMAR Page 15


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Output:-

xiii.To display common messages for following group of days –


Monday & Wednesday, Tuesday & Thursday and Friday & Saturday
and other day.

Coding:-
tput clear
echo “ `date +%D` “
echo “ `date +%A` “
case “ `date +%u` ” in
1|3) echo “It is sunny day”;;
2|4) echo “It is rainy day”;;
5|6) echo “It is cool day”;;
*) echo “It is weekend”
Esac

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 16


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

xiv. To accept a string from the terminal and echo a suitable message if
it doesn’t have at least 9 characters.
Coding:-
echo
echo
tput clear
echo
echo
echo “Enter any String”
read str
if [ `exp $str : ‘.*’` -lt 9 ] ; then
echo “You must enter string having least 9 characters”
else
echo “Your string is accepted”
fi
echo
AUTHOR NAME- YASHWANT KUMAR Page 17
ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

echo

xv. Write a shell script to find the factorial of a number.


Coding:-
tput clear
echo “Enter any number: “
read num
let i=1
let fact=1
let a=num+1
while [ $i –ne $a ]
do
let fact=fact*$i
let i=i+1
done
echo “Factorial of $num is $fact”

AUTHOR NAME- YASHWANT KUMAR Page 18


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Output:-

xvi. Write a shell script to swap two numbers using third variable.
Coding:-
tput clear
echo “Program for swapping two numbers”
echo “Enter 1st number:”
read a
echo “Enter 2nd number:”
read b
let t=$a
let a=$b
let b=$t
echo “Swapped numbers are”
echo “1st number:” $a
echo “2nd number:” $b

AUTHOR NAME- YASHWANT KUMAR Page 19


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 20


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

xvii. Write shell script to print prime numbers between 1 to 20.

Coding:-
Echo “enter the no. upto which you want prime no.”
Read n
For(( i=1; i<=n; i++))
Do
Flag=0
For((j=2;j<i; j++))
Do
If[`expr $i % $j` -eq 0]; then
Flag=1
Fi
Done
If [$flag –eq 0 ]
Then
Echo $i
Fi
Done

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 21


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

xviii. Write a shell script to greatest of three numbers.


Coding:-
Echo “enter first no.”
Read a
Echo “enter second no.”
Read b
Echo “enter third no.”
Read c
If test $a –gt $b ;
Then
Echo “first no. is greatest number”
Elif test $b –gt $c;
Then
Echo “second no. is greatest number”
Elif test $c –gt $a;
Then
Echo “third no. is greatest number”
Fi

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 22


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

xix. Write a shell script to sort the content of a file XYZ and save it in
BCAII.

Coding:-
Echo “enter the content to be searched:\c”
Read content
Echo “enter file to be searched:\c”
Read flname
Echo “searching for $content from file $flname”
Grep “$content” $flname

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 23


ASSIGNMENT- LINUX CLASS- BCA IInd YEAR [2016-17]

xx. Write a shell script to display mathematical table of any number in


the format 3 * 1 = 3.

Coding:-
tput clear
echo “Enter any number: “
read num
let n=1
while [ $n –ne 11 ]
do
let i=num*$n
echo “$num * $n = $i”
let n=n+1
done

Output:-

AUTHOR NAME- YASHWANT KUMAR Page 24

You might also like