Professional Documents
Culture Documents
S# PROGRAMS Page No
1. FILE Commands
2. System Configuration
3. Pipes, Redirection and tee commands
4. Displaying Current date, user name, file listing and directories
5. Filter commands
6. Remove the files with Zero size
7. Sum of the individual digits
8. Greatest among the given set of numbers
9. Palindrome checking
10. Print the multiplication table
1.FILE Commands
Aim:
Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.
Algorithm:
$rm a
$cp a b
$cat a b
$mv a Desktop\ran
$cmp a b
$wc a
$split a -l 1 b
$diff a b
Output
Aim:
Algorithm:
ksgcas@ksgcas-Vertion-Series ~/Desktop $ w
14:47:55 up 2:14, 2 users, load average: 1.20, 0.96, 0.57
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ksgcas tty7 :0 12:33 2:14m 2:08 0.17s cinnamon-sessio
ksgcas pts/1 :0 13:51 1.00s 0.10s 0.00s w
Write a Shell Script to implement the following: pipes, Redirection and tee commands.
Algorithm:
1.Pipe (|)
$cd Desktop
Desktop$ ls | head -3
Desktop$ ls >>out1
3.tee
Desktop$ ls -1 | wc -l | tee out3
Desktop$cat out3
Output:
ksgcas@ksgcas-Vertion-Series ~ $ cd Desktop/
Aim:
Write a shell script for displaying current date, user name, file listing and directories by getting
user choice.
ALGORITHM:
Step 1: Start the process.
Step 2: Open the terminal in linux.
Step 3: Type the gedit command type the program option1 for current date(date),option 2 for
username(who),option 3 for file listing and directories(ls) and option 4 for existing the
program.
Step 4: Save the file to pgm 4.sh
Step 5: Set the permission using chmod +x /Desktop/pgm4.sh
Step 7: Stop the process.
Program
choice=0
while [ $choice -ne 4 ]
do
tput clear
if [ $choice -eq 1 ]
then
echo "Today is `date +%B` `date | cut -f 3 -d ""`, `date | cut -f 6 -d ""`"
if [ $temp -eq 0 ]
then
choice=4
fi
done
Output
1. Today date
2. Total login user
3. File Listing and Directories
4. Exit
Enter your choice
2
As of now ksgcas tty7 2018-01-06 09:14 (:0)
ksgcas pts/1 2018-01-06 10:47 (:0) user are login to the system
Are you continue (1 for yes/0 for n)
5. Filter commands
Aim:
write a shell script to implement the filters commands
Algorithm:
Aim:
write a shell script to remove the files which has file size as zero bytes
Algorithm:
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/de.sh
List of files with 0 size
Desktop/file1
Desktop/file5
Do you want delete 0 size files(y/n)? y
0 size files are now deleted..
7. sum of individual digits
Aim:
Write a shell script to find the sum of the individual digits of a given number.
Algorithm:
Step 1: Start the process.
Step 2: open the terminal in linux.
Step 3: type the gedit command.
Step 4: type the program for finding the sum of individual digits of a given number.
Step 5: Input number n
Step 6: Set sum=0,sd=0
Step 7: Find single digit in sd as n%10 it will give(left most digit).
Step 8: Construct sum no as sum=sum+sd
Step 9: Decrment n by 1
Step10: Is n is greater than zero, if yes goto step 3,otherwise next step.
Step11: print sum.
Step12: stop the process.
Program:
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find sum of all digit for given number"
echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for numner is $sum"
output:
Write a shell script to find the greatest among the given set of numbers using command line
arguments.
Algorithm:
if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given">&2
exit 1
fi
n1=$1
n2=$2
n3=$3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is Bigest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 is Bigest number"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 is Bigest number"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "All the three numbers are equal"
else
echo "I can not figure out which number is biger"
fi
OUTPUT
ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm8.sh
ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm8.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm8.sh 124 56 33
124 is Bigest number
ksgcas@ksgcas-Vertion-Series ~ $
9.Palindrome
Aim:
Write a shell script for palindrome checking.
Algorithm:
Step 1: start the process.
Step 2: open the terminal in linux.
Step 3: type the gedit command.
Step 4: type the program for finding the palindrome checking.
Step 5: get a string from user.
Step 6: find the reverse of the given string and store into the revstr.
Step 7: compare given string and reversed string, if they are equal print palindrome otherwise
print not print palindrome.
Program:
echo -n "Enter a string:"
read str
for i in $(seq 0 ${#str}) ; do
revstr=${str:$i:1}$revstr
done
echo "Given String" $str
echo "Reverse String" $revstr
if [ "$str" = "$revstr" ] ; then
echo "It is palindrome"
else
echo "Not a palindrome"
fi
output:
Aim:
Write a shell script to print the multiplication table of the given argument using for loop
Algorithm: