0% found this document useful (0 votes)
7 views25 pages

Linux File

The document provides a comprehensive guide on using the vi editor and shell programming, detailing various commands and scripts for file manipulation, user management, and arithmetic operations. It includes step-by-step instructions for tasks such as replacing text, copying and cutting lines, and creating macros in vi, as well as writing shell scripts for number testing, file type checking, and basic arithmetic. Each section contains examples and expected outputs for clarity.

Uploaded by

ravi pandey
Copyright
© © All Rights Reserved
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)
7 views25 pages

Linux File

The document provides a comprehensive guide on using the vi editor and shell programming, detailing various commands and scripts for file manipulation, user management, and arithmetic operations. It includes step-by-step instructions for tasks such as replacing text, copying and cutting lines, and creating macros in vi, as well as writing shell scripts for number testing, file type checking, and basic arithmetic. Each section contains examples and expected outputs for clarity.

Uploaded by

ravi pandey
Copyright
© © All Rights Reserved
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/ 25

Page 1 of 25

VI - EDITOR

1. Replace the word ‘has’ with ‘has not’ in a file using vi editor.
Ans:- To replace the word use the following ex command mode:
:1,$s/has/has not/g

2. Copy line 3 to 7 and paste them in line 11 in a file using vi editor.


Ans:- To copy lines 3 to 7 follow these steps in command mode:
3G # to go to 3rd line
4yy # to copy 4 line
11G # to go to 11th line
p # to paste

3. Cut and paste 4 lines in a file using vi editor.


Ans:- To cut and paste lines in file use the following command
Mode command:
4dd
P

4. Locate 20th character in a file.


Ans:- H # to go to the top of file.
20| # to reach the 20th character.

5. Open two files ‘txtfile’ and ‘newfile’ and copy 5 lines from ‘textfile’
and paste them in ‘newfile’ using vi editor.
Ans:- $vi txtfilenewfile # to open 2 files.
“a5yy # to copy 5 lines in buffer.
:e new file # switch to ‘newfile’.
“ap # to paste.
Page 2 of 25

6. Open two files ‘txtfile’ and ‘newfile’ and cut 5 lines from ‘textfile’ and
paste them in ‘newfile’ using vi editor.
Ans:- $vi txtfilenewfile # to open 2 files.
“a5yy # to cut 5 lines in buffer.
:e new file # switch to ‘newfile’.
“ap # to paste.

7. Open ‘txtfile’ and copy 10th to the last line in it and open another
file ‘newfile’ and paste them in it using vi editor.
Ans:- 10L # to reach 10th line to the last line(command mode)
“ayy # to copy line into a buffer(command mode)
:e newfile # to switch a newfile(ex mode)
“ap # to paste(command mode)

8. Open ‘txtfile’ and cut 15th to the last line in it and open another file
‘newfile’ and paste them using vi editor.
Ans:- 15L # to reach the 15th line to the last line(command mode)
“add # to cut line into a buffer(command mode)
:e new file # to switch to newfile (ex mode)
“ap # to paste(command mode)

9. Yank a few words in a file to new position using vi editor.


Ans:- To yank a few words to new position use following
command mode
command: 5yw
p
Page 3 of 25

10. Cut and paste 5 words in a file to new position using vi editor.
Ans:- To cut and paste words into new position use following command mode
command: 5dw
p

11. Yank a few words in a file to another file using vi editor.


Ans:- “a5yw # to yank words into a buffer(command mode)
:e newfile # to switch another file (ex mode)
“ap # to paste(command mode)

12. Cut and paste 5 words in a file to a new file using vi editor.
Ans:- “a5dw # to cut words into a buffer(command mode)
:e file # to switch another file(ex mode)
“ap # to paste(command mode)

13. Create a macro to paste your name at any position using vi editor.
Ans:- To create a macro use the following ex mode command
:map Manish

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


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

15. Create a macro to remove all leading spaces in file using vi editor.
Ans:- To create a macro use the following ex mode command:
:map r : 1,$s/ //c
Page 4 of 25

16. Write a macro in input mode to save and quit vi editor.


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

17. Display all the users currently logged in detail with column
headers.
Ans:- To display all the users currently logged in use the following
Command mode:
Command: $who –Hu

18. List all files in current directory and save the list in a file ABC.
Also save the contents of the in ABC. Write the command to display
the contents in ABC in sorted order.
Ans:- Use the following shell command:
$ls |sort |cat>ABC; cat ABC

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


Ans:- Use the following shell command:
$ sort ABC | cat >> OABC

20. List all files that match a class.


Ans:- Use the following command:
$ grep –1 “[aA]c[tT]iv” *.*
Page 5 of 25

21. How can you add user, delete and modify user.
Ans:- $ useradd –m Manish–p “Kaka” # to add user
$ usermod –s /bin/csh/Manish # to modify user
$ userdel Manish # to delete user account

22. Display the lines in a file that contains a particular word.


Ans:- $ grep “echo” Fibonacci

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


Ans:- To append the contents of the files in other file we will use the following
shell command:
$ cat txtfilenewfile | cat >> JABC

24. Count the no of file in a directory.


Ans:- To cout the number of file in a directory use the following
command : $ls | wc - l
Page 6 of 25

SHELL PROGRAMMING

1. Write a shell program to test whether a number is +ve , -ve or


zero.
Shell Script:-

echo
tput clear
echo
echo
echo “Enter the number : ”
read num
echo
if[$num -1t 0];then
echo “Entered no is Negative”
elif[ $num -gt 0 ]; then
echo “Entered no is Positive”
else
echo “Entered no is Zero”
fi
echo
echo

OUTPUT :
Page 7 of 25

2. Write a shell program to test whether a filename is regular file


or a directory or of other type.
Shell Script:-

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

OUTPUT :-
Page 8 of 25

3. Write a shell program to list only the directories in current


path.
Shell Script:-

echo
echo
tput clear
echo
echo “The list of the directory in the current path is : ”
for file in ‘ls’
do
if [ -d $file ]; then
echo $file
fi
done
echo
echo
echo

OUTPUT :
Page 9 of 25

4. Write a shell program to print the greatest of three numbers.

Shell Script:-

echo “Enter the first number : ”


read a
echo “Enter the second number”
read b
echo “Enter the third number ”
read c
if [ $a -ge $b ]; then
If [ $a -gt $c ]; then
echo “$a is greatest number”
else
echo “$c is greatest number”
if
else
if [ $b -gt $c ]; then
echo “$b is greatest number”
else
echo “$c is greatest number”

OUTPUT :
Page 10 of 25

5. Write a shell program to print 12 terms of Fibonacci series.


Shell Script:-

echo
echo
tput clear
echo
echo
echo “The Fibonacci series up to 12term:”
let a=0 b=1
let n=0
while[ $n –ne 12 ]
do
echo $a
let c=a+b
let a=b
let b=c
let n=n+1
done
echo

OUTPUT :
Page 11 of 25

6. Write a shell program to display particular message depending


on the weekday...
Shell Script:-

echo
tput clear
echo “Today’s date is ‘date + %D’”
echo “Today’s day is ‘date + %A’”
case “’date+%u’” in
1)echo “It is my working day”;;

2)echo “It is the resting day”;;

3)echo “It is mid-day”;;

4)echo “It is cool day”;;

5)echo “It is very expensive time”;;

6)echo “It is about to weekend”;;

7)echo “It is weekend”;;


esac
echo

OUTPUT :
Page 12 of 25

7. Write a shell program to display common message for


following group of days Mon & Wed, Tue& Thu, Fri & Sat and
other days.
Shell Script:-

echo
echo
tput clear
echo
echo
case “’date +%u’”in
1 |3)echo “It is my working day”;;

2 | 4)echo “It is my sports day”;;

5 | 6)echo “It is cool day”;;

* )echo “It is weekend”;;

esac
echo
echo
echo

OUTPUT :
Page 13 of 25

8. Write a shell program to display today is exam is BCA 2.

Shell Script:-

echo
echo
tput clear
echo
echo
echo “today’s date is ‘date +%D’”
echo
for x in 3 6 9 12 15 18 20 24 27
do
if [ ‘date +%d’ = $x ];then
echo “Today is exam of BCA-II”
if
done
echo
echo

OUTPUT :
Page 14 of 25

9. Write a shell program to wish “Good Morning “or “Good


Evening”.
Shell Script:-

echo
echo
tput clear
echo
echo
if [ ‘date +%p’ = “AM” ]; then
echo “Good Morning”
else
echo “Good Evening”
if
echo
echo

OUTPUT :
Page 15 of 25

10. Write a shell program to Swap two numbers using three


variables.
Shell Script:-

echo “Enter first number”


read a
echo “Enter second number”
read b
echo “Before swapping: a=$a b=$b”
c=$a
a=$b
b=$c
echo “After swapping: a=$a b=$b”
echo
echo

OUTPUT :
Page 16 of 25

11. Write a shell program to accept a string from the terminal


and echo a suitable message if it doesn’t have at least 9
characters.

Shell Script:-

echo
echo
tput clear
echo
echo
echo “Enter Your name:”
read name
t=`echo “$name” | wc -c`
if [ $t -lt 9 ] ;then
echo “You must enter string having at least 9 character”
else
echo “your name is accepted”
if
echo
echo

OUTPUT :
Page 17 of 25

12. Write a shell program to accept a string from the terminal and echo a
suitable message if it doesn’t have at least 9 characters
Ex: 3 * 1 = 3
Shell Script:-

echo “Enter any number”


read a
f=1
for(( i=1 ; i<=10 ; i++ ))
do
let f=$f*$i
echo “ $a * $i = $f ”
done
echo

OUTPUT :
Page 18 of 25

13. Write a Shell Program to print enter number is prime number or


not.

Shell Script:-

echo “Enter any number”


read a
f=1
i=2

while [ $i -lt $a ] ; then


t=$i%$a’

if [ $t -eq 0];then
f=0
if
let i=i+1
done

if [ $f -eq 1 ]; then
echo “$n is a Prime number”
else
echo “$n is not a Prime number”
if

OUTPUT :
Page 19 of 25

14. Write a shell script to find factorial of a number.


Shell Script:-

echo
echo “Enter any no”
read n
let i=2
let f=1
while [ $i -le $n ]
do
let f = $f * $i
let i = $i+1
done
echo “Fectorial of $n is $f ”

OUTPUT :-
Page 20 of 25

15. Write a program for number odd or even series.


Shell Script:-

echo
echo " Maximum Length of Number"
read n
echo " Even Number Odd Number"
for((i=0;i<=$n;i++))
do
x=`expr $i % 2`
if [ $x -eq 0 ];then
echo " $i "
else
echo " $i "
fi
done

OUTPUT :-
Page 21 of 25

16. Write a program for leap year.


Shell Script:-

echo "Enter the Year”

readyr

x=`expr $yr % 4 `

if[$x -eq 0 ] ; then

echo "Leap Year"

else

echo "Not Leap Year"

if

OUTPUT :
Page 22 of 25

17. Write a program for Armstrong no. or not.


Shell Script:-

echo
echo “Enter any number “
read n
let k=n
let sum=0
while [ $n -ne 0 ]
do
let j=n%10
let sum=sum+(j*j*j)
let k=k/10
done
if [ $n -eq $sum ];then
echo “The number is Armstrong”
else
echo “Not a Armstrong”
if

OUTPUT :
Page 23 of 25

18. Write a shell script to perform arithmetic operation using switch


case
Shell Script:-

echo ”Enter any number “


read a
echo “Enter second number”
read b
echo “Enter your choice”
echo “1: Addition”
echo “2: subtraction”
echo “3: multiplication”
echo “4: division “
read choice
case $choice in
1)let c=a+b
echo “Addition of $a+$b is $c”;;
2)let c=a-b
echo “subtraction of $a-$b is $c”;;
3) let c=a*b
echo “Multiplication of $a-$b is $c”;;
4)let c=a/b
echo “division of $a-$b is $c”;;
*)echo “Wrong choice”;;
Esac

OUTPUT :
Page 24 of 25

19. Write Script to see current date, time, username, and current
directory.

Shell Script:-

echo
echo " see current date and time "
echo
date
echo
echo " see current user name "
echo
who
echo
echo " see current directory "
pwd

OUTPUT :
Page 25 of 25

20. Write script to print nos as 5, 4, 3,2,1,0 using while loop.

Shell Script:-

echo
i=5
while [ $i -ge 0 ]
do
echo " $i "
let i=i - 1
done

OUTPUT :

You might also like