All codes written by me, tested and working!!
Program 1: To write a program getting Armstrong number below 1000.
Program 1: To write a program getting Armstrong number below 1000.
Algorithm:
1. Start
2. Initialize
the variables.
3. In
while loop, break the numbers in to Units, Tens, Hundreds using reminder and
division method.
4. After
dividing each number make the cube of each number and add them.
5. If
the number is equal to the sum, save that number as Armstrong number.
6. Continue
procedure in while loop until 1000 numbers.
7. Print
the Armstrong number.
8. Stop.
Sample Input/Output
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg1arm2.sh
#!/bin/bash
i=1
while((i<=1000))
do
c=$i
d=$i
b=0
a=0
while((c>0))
do
a=$((c%10))
b=$((b
+ a*a*a))
c=$((c/10))
done
if((b==d)); then
echo
"$i"
fi
i=$((i+1))
done
rushi@rushi-Inspiron-N5010:~/shellscript$
bash pg1arm2.sh
1
153
370
371
407
Result:
Shell scripting program can be used to find the Armstrong numbers using While
and If structures.
Program 2: Write a program to find an
occurrence of a particular character in a string using Shell scripting.
Algorithm:
1. Start
2. Take
input from user, save input in a variable.
3. Find
the length of the character using wc –c command.
4. Use
cut command to cut each character using its index as input to the cut command.
5. Compare
each character with demanded character.
6. Increment
the count if successful.
7. Print
the count value.
8. Stop.
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg2occ.sh
echo Enter the string
read userstr
echo enter the character to be found
read strch
l=`echo $userstr | wc -c`
echo "Length of the entered word is `expr $l -
1`"
echo "The occurrence of a character in a given
text is"
grep -o "$strch"
<<<"$userstr" | wc -l
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg2occ.sh
Enter the string
rushi
enter the character to be found
i
Length of the entered word is 5
The occurance of a character in a given text is
1
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg2occ.sh
Enter the string
rushi
enter the character to be found
x
Length of the entered word is 5
The occurrence of a character in a given text is
0
Result:
Using length count we can count characters using wc –c and comparing
each character with the desired character, we can find occurrence of the
character.
Program 3: To replace a particular
character into string using shell scripting.
Algorithm:
1. Start
2. Enter
and store the string
3. Get
the character to be replaced.
4. Enter
the character with which it should be replaced.
5. Using
cut command, separate each character from string and compare each character
with character to be replaced.
6. If
comparison command is successful, replace the character and print.
7. Stop.
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg3replace.sh
echo "Enter the Text"
read userstr
echo "enter the character to be raplaced in
given string"
read charq
echo "enter the character with which the $char
to be replaced"
read newch
len=`echo $userstr | wc -c`
len=`expr $len - 1`
echo The length of the Entered string is `expr $len`
i=1
echo -e "New string is\n"
while [ $i -le $len ]
do
cutch=`echo
$userstr | cut -c $i`
if
[ "$cutch" = "$charq" ]
then
cutch=$newch
echo
-n $cutch
else
echo
-n $cutch
fi
i=`expr $i + 1`
done
echo -e "\n"
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg3replace.sh
Enter the Text
rushi
enter the character to be raplaced in given string
i
enter the character with which the to be replaced
y
The length of the Entered string is 5
New string is
rushy
Result:
Shell scripting program can be used to replace the particular character.
Program 4: To compare two strings using
shell scripting.
Algorithm:
1. Start
2. Take
input from user for string1 and string2.
3. Find
the length of both strings
4. If
length are equal then compare directly by using rational operator “=”
5. If
condition is true then print the result as both strings are same else not equal
6. Stop
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg4comparestring.sh
echo "Enter string 1"
read string1
echo "Enter string 2"
read string2
len1=`echo $string1 | wc -c`
len2=`echo $string2 | wc -c`
echo The length of the string1 is `expr $len1 - 1`
echo The length of the string2 is `expr $len2 - 1`
if [ $len1 = $len2 ]
then
if
[ $string1 = $string2 ]
then
echo
"Yeah! Text are same!"
else
echo
"Oops, Not same!"
fi
else
echo
"Oops, Not same!"
fi
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg4comparestring.sh
Enter string 1
rushi
Enter string 2
rushi
The length of the string1 is 5
The length of the string2 is 5
Yeah! Text are same!
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg4comparestring.sh
Enter string 1
rushi
Enter string 2
gajjar
The length of the string1 is 5
The length of the string2 is 6
Oops, Not same!
Result:
We can compare the string directly using rational
operator unlike pointer concept in C language.
Program 5: Write a program to find a sum
and average for a given set of number using shell scripting.
Algorithm:
1. Start
2. Get
the value of number of inputs from user.
3. Get
one by one number and add them successively
4. Average
the number by dividing the value get from user at step 2.
5. Print
the sum value and average value.
6. Stop.
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg5sumavg.sh
echo "How many numbers you want to enter?"
read count
i=1
add=0
avg=0.0
echo "Start entering numbers"
while [ "$i" -le "$count" ]
do
read
number
add=`expr
$add + $number`
i=`expr
$i + 1`
done
avg=`expr $add / $count`
echo "Addition is $add"
echo "Average is $avg"
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg5sumavg.sh
How many numbers you want to enter?
5
Start entering numbers
1
2
3
4
5
Addition is 15
Average is 3
Result:We
can get values from user using the while loop and we can find sum and average
of given numbers.
Program 6: Write a program to find a
number of vowels in a string using shell scripting.
Algorithm:
1. Start
2. Get
the string from the user.
3. Using
wc –c command, find the length of the string.
4. Using
the cut command separate each character and compare with a,e,i,o,u using
case structure.
5. When
the case is matched, increment the count.
6. Print
the count value.
7. Stop
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat pg6vowel.sh
echo "Enter the string to count the
vowels"
read string1
len=`echo $string1 | wc -c`
len=`expr $len - 1`
echo "The length of the word is $len"
echo -n "The vowels in the word are "
i=1
count=0
while [ $i -le $len ]
do
res=`echo
$string1 | cut -c $i`
case
"$res" in
a)
count=`expr
$count + 1`;;
e)
count=`expr
$count + 1`;;
i)
count=`expr
$count + 1`;;
o)
count=`expr
$count + 1`;;
u)
count=`expr
$count + 1`;;
*)
count=`expr
$count`;;
esac
i=`expr $i + 1`
done
echo -n -e "$count \n"
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg6vowel.sh
Enter the string to count the vowels
rushigajjar
The length of the word is 11
The vowels in the word are 4
Result: Using
case structure we can find the vowels in a string by sending each character.
Program 7: To
copy strings using character by character copying using shell scripting.
Algorithm:
1. Start.
2. Get
the string1 and string2
3. Find
length of both strings and consider the highest length.
4. Using
cut command put the character in the second string.
5. Display
the string.
6. Stop.
Sample Input/Output:
rushi@rushi-Inspiron-N5010:~/shellscript$
cat
pg7charbychar.sh
echo "Enter the string"
read string1
echo "Enter the string to be copied"
read string2
len1=`echo $string1 | wc -c`
len2=`echo $string2 | wc -c`
len1=`expr $len1 - 1`
len2=`expr $len2 - 1`
#finding the longest string length
if [ "$len1" -gt "$len2" ]
then
len="expr
$len1"
else
len="$len2"
fi
i=1
j=1
k=1
while [ $i -le $len ]
do
while
[ "$j" -le "$i" ]; do
copycutchar=`echo
$string1 | cut -c $i`
echo
-n $copycutchar
j=`expr
$j + 1`
done
j=1
k=`expr
$i + 1`
while
[ "$k" -le "$len2" ]; do
string1cut=`echo
$string2 | cut -c $k`
echo
-n "$string1cut"
k=`expr
$k + 1`
done
i=`expr $i + 1`
done
echo -e "\n"
rushi@rushi-Inspiron-N5010:~/shellscript$
./pg7charbychar.sh
Enter the string
VIT
Enter the string to be copied
UNIVERSITY
VNIVERSITYIIIVERSITYTTTVERSITYERSITYRSITYSITYITYTYY
Result:
We can find a interleave the words in a shell scripting.
1 comment:
Great Stuff..all workin lika charm. Amazing work.. Thanks
Post a Comment