0% found this document useful (0 votes)
65 views13 pages

Experiment No:-AIM:-: Source Code

The document contains the aims, source code, and output sections for 12 shell scripting experiments. The experiments include programs to: 1) swap the values of two variables, 2) check if a string is a palindrome, 3) perform math operations on command line arguments, 4) count words, characters, spaces, and symbols in text, 5) find the sum of digits in a 5 digit number, 6) generate a Fibonacci series, 7) check if a year is a leap year, 8) print alternate digits of a 7 digit number, and 9) check if a number is even or odd.

Uploaded by

Divyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views13 pages

Experiment No:-AIM:-: Source Code

The document contains the aims, source code, and output sections for 12 shell scripting experiments. The experiments include programs to: 1) swap the values of two variables, 2) check if a string is a palindrome, 3) perform math operations on command line arguments, 4) count words, characters, spaces, and symbols in text, 5) find the sum of digits in a 5 digit number, 6) generate a Fibonacci series, 7) check if a year is a leap year, 8) print alternate digits of a 7 digit number, and 9) check if a number is even or odd.

Uploaded by

Divyank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

EXPERIMENT NO:AIM:- Write a shell program to exchange the values of two

variables.

SOURCE CODE:echo Enter value for a:


read a
echo

Enter value for b:

read b
clear
echo

Values of variables before swaping

echo A=$a
echo B=$b
echo

Values of variables after swaping

a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo A=$a
echo B=$b

OUTPUT:-

EXPERIMENT NO:-

AIM:- Write a shell program to check whether a given


string is palindrome or not.
SOURCE CODE:echo "Enter a string to be entered:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done

echo "String is palindrome"

OUTPUT:-

EXPERIMENT NO:-

AIM:- Write a shell program to add, subtract and multiply the 2


given numbers passed as command line arguments.
SOURCE CODE:echo "Enter the two numbers"
read a b
add=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
echo Addtion of $a and $b is $add
echo Subtraction of $b from $a is $sub
echo Multiplication of $a and $b is $mul

OUTPUT:-

EXPERIMENT NO:-

AIM:- Write a shell program to count number of words,


characters, white spaces and special symbols in a given text.

SOURCE CODE;echo Enter a text


read text
w=`echo $text | wc -w`
w=`expr $w`
c=`echo $text | wc -c`
c=`expr $c - 1`
s=0

, alpha=0

j=`

n=1
while [ $n -le $c ]
do
ch=`echo $text | cut -c $n`
if [ $ch = $j ]
then
s=`expr $s + 1`
f
case $ch in
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) alpha=`expr $alpha + 1`;;
esac
n=`expr $n + 1`
done
special=`expr $c - $s - $alpha`

echo Words=$w
echo Characters=$c
echo Spaces=$s
echo Special symbols=$special

OUTPUT:-

EXPERIMENT NO:-

AIM;- Write a shell program to find the sum of all the


digits in a given 5 digit number.
SOURCE CODE:echo "enter the the num "
read no
length=`expr length $no`
while [ $length -ne 0 ]
do
b=`expr substr $no $length 1`
ans=`expr $ans + $b`
length=`expr $length - 1`
done
echo "Sum of Digit is : $ans"

OUTPUT:-

EXPERIMENT NO:-

AIM:- Write a shell script to generate fibonacci series.


SOURCE CODE;echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done

OUTPUT:-

EXPERIMENT NO:-

AIM;- Shell Script to check whether given year is leap year or not.
SOURCE CODE;echo Enter the year
read y
a=`expr $y % 4`
b=`expr $y % 100`
c=`expr $y % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ]
then
echo $y is leap year
else
echo $y is not leap year
fi

OUTPUT:-

EXPERIMENT NO:-

AIM:- Shell Script to print alternate digit when a 7 digit number is


passed.

SOURCE CODE;echo Enter a 7 digit number


read num
n=1
while [ $n -le 7 ]
do
a=`echo $num | cut -c $n`
echo $a
n=`expr $n + 2`
done

OUTPUT:-

EXPERIMENT NO:-

AIM;- Shell Script to find whether number given is even or odd.


SOURCE CODE;echo "Enter the numnber"
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is an even number"
else
echo "$n is an odd number"
fi

OUTPUT;-

You might also like