Linux File
Linux File
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
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)
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
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
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
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
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
SHELL PROGRAMMING
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
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
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
Shell Script:-
OUTPUT :
Page 10 of 25
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
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”;;
OUTPUT :
Page 12 of 25
echo
echo
tput clear
echo
echo
case “’date +%u’”in
1 |3)echo “It is my working day”;;
esac
echo
echo
echo
OUTPUT :
Page 13 of 25
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
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
OUTPUT :
Page 16 of 25
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:-
OUTPUT :
Page 18 of 25
Shell Script:-
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
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
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
readyr
x=`expr $yr % 4 `
else
if
OUTPUT :
Page 22 of 25
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
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
Shell Script:-
echo
i=5
while [ $i -ge 0 ]
do
echo " $i "
let i=i - 1
done
OUTPUT :