0% found this document useful (0 votes)
31 views

Linux Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Linux Programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Ex.

No : 1
FILE COMMANDS IN LINUX

Aim

To write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

Algorithm

Step 1: Start the Program


Step 2: Display the list of choice
Step 3: Get the user choice as input
Step 4: If the choice is 1, Create the new file using vi editor.
Step 5: If the choice is 2, display content of the file using cat command.
Step 6: If the choice is 3, Copy one file to another file using cp command.
Step 7: If the choice is 4, remove the file using rm command.
Step 8: If the choice is 5, move the file using mv command.
Step 9: If the choice is 6, compare the two files using cmp command.
Step 10: If the choice is 7, display the no.of lines,words,characters in the file using wc
command.
Step 11: If the choice is 8, split the single file into multiple files using split command.
Step 12: If the choice is 9, display the difference of the two files content using diff
command.
Step 13: If the choice is 10, exit.
Step 14: Stop the program.

Coding

echo "File Command"


echo "1. CREATE"
echo "2. DISPLAY"
echo "3. COPY"
echo "4. REMOVE"
echo "5. MOVE"
echo "6. COMPARE"
echo "7. WOAD COUNT"
echo "8. SPLIT"
echo "9. DIFFERENCE"
echo "10.Exit"
echo -n "Enter Your Choice : "
read ch
if [ "$ch" = 1 ]
then
echo "CREATE COMMAND"
echo " Enter Your File Name"
read a
vi $a
echo "File is Created"

1
sh f1.sh
elif [ "$ch" = 2 ]
then
echo "DISPLAY"
ls
echo "Enter the File Name to be Display"
read a
cat $a
sh f1.sh
elif [ "$ch" = 3 ]
then
echo "COPY COMMAND"
ls
echo "Enter File Name for Already Created :"
read f1
echo "Enter File Name for Copied File : "
read f2
cp $f1 $f2
echo "File is Copied"
cat $f2
sh f1.sh
elif [ "$ch" = 4 ]
then
echo "REMOVE COMMAND"
ls
echo "Enter the File Name for Remove :"
read file1
rm $file1
echo "The File is Removed"
ls
sh f1.sh
elif [ "$ch" = 5 ]
then
echo "MOVE COMMAND"
ls
echo " Enter the File Name to be Moved"
read m
echo " Enter the Moved File Path "
read p
mv $m $p
echo " File is Moved"
ls
sh f1.sh
elif [ "$ch" = 6 ]
then
ls
echo "COMPARE COMMAND"
echo "Enter the Already Exist First File Name"
read file1

2
echo "Enter the Already Exist Second File Name"
read file2
echo "First File Content"
cat $file1
echo "Second File Content"
cat $file2
cmp $file1 $file2
sh f1.sh
elif [ "$ch" = 7 ]
then
echo "WORD COUNT COMMAND"
echo "Enter the Already Exist File Name"
read file1
echo "The Content Of the $file1 is"
cat $file1
echo "The Word Count of $file1 is :"
wc $file1
sh f1.sh
elif [ "$ch" = 8]
then
echo "SPLIT COMMAND"
ls
echo "Enter File Name to be Split"
read file1
split -d $file1
ls
sh f1.sh
elif [ "$ch" = 9 ]
then
echo "DIFFERENCE COMMAND"
ls
echo "ENTER THE First File"
read file1
echo "Enter the Second File"
read file2
echo "First File Content"
cat $file1
echo "Second File Content"
cat $file2
diff $file1 $file2
sh f1.sh
elif [ "$ch" = 10 ]
then
echo "exit"
fi

3
Ex.No : 2
SYSTEM CONFIGURATION DETAILS

Aim

To write a shell script to show the following system configuration:


a. currently logged user and his log name
b. current shell , home directory , Operating System type , current Path setting , current
working directory
c. show currently logged number of users, show all available shells
d. show CPU information like processor type , speed
e. show memory information

Algorithm
Step 1: Start the Program
Step 2: Display the User name using user command.
Step 3: Display Log name using logname command.
Step 4: Display current shell using shell command.
Step 5: Display home directory using home command.
Step 6: Display operating system type using ostype command.
Step 7: Display current path using path command.
Step 8: Display current working directory using pwd command.
Step 9: Display current logged no.of users using who command.
Step 10: Display available shells using /etc/shells command.
Step 11: Display CPU information using grep "model name" /proc/cpuinfo
command.
Step 12: Display Memory information using vmstat.
Step 13: Stop the program.

Coding
clear
echo "SYSTEM CONFIGURATION DETAILS"
echo "----------------------------------------------------- "
echo "a. Currently Loged User : $USER"
echo " Log Name : $LOGNAME"
echo "b. Current Shell : $SHELL"
echo " Home Directory : $HOME"
echo " Operating System Type : $OSTYPE"
echo " Current Path Setting : $PATH"
echo " Current Working Directory : $PWD"
echo "c. Current Logged No.Of.Users :"
who | wc -l
echo " Available Shells:"
cat /etc/shells
echo "d. CPU Information:"
grep "model name" /proc/cpuinfo
echo "e. Memory Information:"
vmstat

4
Ex.No : 3
PIPES, REDIRECTION AND TEE COMMANDS

Aim

To write a Shell Script to implement the following: pipes, Redirection and tee commands.

Algorithm

Step 1: Start the Program


Step 2: Display the list of choice
Step 3: Get the user choice as input
Step 4: If the choice is 1, the output of one command is input of another by using pipe
command.
Step 5: If the choice is 2, the content of file1 is input using cat command.
Step 6: If the choice is 3, to get file1, file2 from user, the output of file1and file2 is
redirected to file1.
Step 7: If the choice is 4, to get file1 from user, the output is stored into temp file and
display it.
Step 8: If the choice is 5, to quit the choice.
Step 9: Stop the program.

Coding

echo "1.PIPES"
echo "2.STANDARD INPUT REDIRECTION"
echo "3.STANDARD OUTPUT REDIRECTION"
echo "4.TEE COMMAND"
echo "5.QUIT"
echo -n "ENTER YOUR CHOICE : "
read ch
if [ "$ch" = 1 ]
then
ls -l | grep "^-"
sh pro3.sh
elif [ "$ch" = 2 ]
then
ls
echo "Enter the File Name"
read file1
cat<$file1
sh pro3.sh
elif [ "$ch" = 3 ]
then
ls
echo "Enter the File Name1"
read file1
echo "Enter the File Name2"

5
read file2
cat $file1 >> $file2
echo "Content of File1"
cat $file1
echo "Content of File2"
cat $file2
sh pro3.sh
elif [ "$ch" = 4 ]
then
ls
echo "Enter the File Name"
read file1
cat $file1 | tee temp
echo "Content of Temp"
cat temp
sh pro3.sh
elif [ "$ch" = 5 ]
then
echo "Exit"
fi

6
Ex.No : 4

DISPLAYING CURRENT DATE, USER NAME, FILE LISTING AND DIRECTORIES

Aim

To write a shell script for displaying current date, user name, file listing and directories by
getting user choice.

Algorithm

Step 1: Start the Program.


Step 2: Display the list of choice.
Step 3: Get the user choice as input.
Step 4: If the choice is 1, to display the current date using date command.
Step 5: If the choice is 2, to display the user name using who am I command.
Step 6: If the choice is 3, to display the list of files and directories using ls –l command.
Step 7: If the choice is 4, to exit the choice.
Step 8: Stop the program.

Coding

echo "1. DATE"


echo "2. USER NAME"
echo "3. LISTING FILES & DIRECTORIES"
echo "4. EXIT"
echo -n "Enter Your Choice : "
read ch
if [ "$ch" = 1 ]
then
echo "The Date is :"
date
sh pro4.sh
elif [ "$ch" = 2 ]
then
echo "The User Name is :"
who am i
sh pro4.sh
elif [ "$ch" = 3 ]
then
echo "Listing Files and Directories"
ls -l
sh pro4.sh
elif [ "$ch" = 4 ]
then
echo "Exit"
fi

7
Ex.No : 5
FILTER COMMANDS IN LINUX

Aim

To write a shell script to implement the filter commands.

Algorithm

Step 1: Start the Program


Step 2: Display the list of choice
Step 3: Get the user choice as input
Step 4: If the choice is 1, Get the file name and display the content of the file then using
grep command to find and display the word in the given file.
Step 5: If the choice is 2, Get the file name and display the content of the file then using
wc command to find and display the no of lines, words and characters in the
given file.
Step 6: If the choice is 3, Get the file name and display the content of the file then using
cut command to cut the given content and display the remaining content in the
given file.
Step 7: If the choice is 4, Get the file name and display the content of the file then using
tr command to convert the content in uppercase to lowercase and vice versa and
display it.
Step 8: If the choice is 5, to exit the choice.
Step 9: Stop the program

Coding

echo "1. GREP FILTER"


echo "2. WC FILTER"
echo "3. CUT FILTER"
echo "4. TR FILTER"
echo "5. EXIT"
echo -n "ENTER YOUR CHOICE : "
read ch
if [ "$ch" = 1 ]
then
ls
echo "Enter the File Name : "
read a
echo "The File Content is : "
cat $a
echo "The Result is "
grep "file2" $a
sh pro5.sh
elif [ "$ch" = 2 ]
then

8
ls
echo "Enter the File Name : "
read a
echo "The File Content is : "
cat $a
echo "The File Contain the following No.of.Lines,Words & Characters :"
wc $a
sh pro5.sh
elif [ "$ch" = 3 ]
then
ls
echo "Enter the File Name : "
read a
echo "The File Content is :"
cat $a
echo "The Cut File Content is :"
cut -c1,3 $a
sh pro5.sh
elif [ "$ch" = 4 ]
then
ls
echo "Enter the File Name : "
read a
echo "The Content is : "
cat $a
echo "The Tr command Result is :"
cat $a|tr "[a-z]" "[A-Z]"
sh pro5.sh

elif [ "$ch" = 5 ]
then
echo "Exit"
fi

9
Ex.No : 6
REMOVING ZERO BYTE FILES

Aim

To write a shell script to remove the files which has file size as zero bytes.

Algorithm

Step 1: Start the Program.


Step 2: Display the current path and list of files, directories.
Step 3: Search the zero byte file in the current path.
Step 4: Remove the each zero byte files one by one.
Step 5: Stop the program.

Coding

echo "Current Path"


pwd
echo "List of Files & Directories of Current Directory"
ls

for i in *
do
if [ -f $i -a ! -s $i ]
then
rm $i
sleep 1
echo "$i file is Removed"
fi
done

ls

10
Ex.No : 7
SUM OF THE INDIVIDUAL DIGITS

Aim

To write a shell script to find the sum of the individual digits of a given number.
.

Algorithm

Step 1: Start the Program.


Step 2: Get input from user.
Step 3: Separate the digit using the code a=$(($n%10)), n=$(($n/10)).
Step 4: Add the separated number using the code sum=$(($sum+$a)).
Step 5: Check the sum is single digit or double digit, if sum is double digit continue the
process.
Step 6: Display the result.
Step 7: Stop the program.
Coding

echo "Enter the Number : "


read n
a=0
sum=0
while [ $n -gt 0 ]
do
a=$(($n%10))
sum=$(($sum+$a))
n=$(($n/10))
done
if [ $sum -gt 9 ]
then
n=$sum
sum=0
fi
while [ $n -gt 0 ]
do
a=$(($n%10))
sum=$(($sum+$a))
n=$(($n/10))
done
echo "The SUM OF THE INDIVIDUAL DIGITS OF A GIVEN NO IS $sum"

11
Ex.No : 8
GREATEST AMONG THE GIVEN NUMBERS

Aim

To write a shell script to find the greatest among the given set of numbers using command

line arguments.
.

Algorithm

Step 1: Start the Program


Step 2: Get the list of numbers as input using command line argument.
Step 3: Compare each argument one by one by using shift command.
Step 4: Display the greatest among the given set of numbers.
Step 5: Stop the program.

Coding

n=$#-1
big=$1
for((x=1;x<=n;x++))
do
if [ $big -lt $2 ]
then
big=$2
fi
shift
done
echo "The Biggest Number is $big"

12
Ex.No : 9
PALINDROME CHECKING

Aim

To write a shell script for palindrome checking.


.

Algorithm

Step 1: Start the Program.


Step 2: Get the number / String as input.
Step 3: Calculate the length of number / string.
Step 4: To find out the given number / string in reverse order.
Step 5: Check the given number / string and reverse of number / string are equal or not.
Step 6: Display the result.
Step 7: Stop the program.

Coding

clear
echo "Enter the String"
read str
len=${#str}
flag=1
for((i=0;i<=len/2;i++))
do
c1="${str:$i:1}"
c2="${str:$len-$i-1:1}"

if [ $c1 != $c2 ]
then
flag=0
echo "String is not palindrome"
break
fi
done

if(( $flag==1))
then
echo "Input String is Palindrom"
fi

13
Ex.No : 10
MULTIPLICATION TABLE

Aim

To write a shell script to print the multiplication table of the given argument using for loop.
.

Algorithm

Step 1: Start the Program.


Step 2: Get number of rows as input.
Step 3: Get the table value as input.
Step 4: Display the table with the given number of rows using the code $(($x*$n)).
Step 5: Stop the program.

Coding

clear
echo "Enter the Row Value :"
read r
echo "Enter the Table Value :"
read n
for((x=1;x<=r;x=x+1))
do
echo "$x * $n = $(($x*$n))"
done

14

You might also like