#Bash script to add two matrix
matrix1=(1 2 3 4 5 6 7 8 9) #Matrix of size 3 by 3
matrix2=(11 12 13 14 15 16 17 18 18 19) #Matrix of size 3 by 3
rows=3
cols=3
echo "First matrix"
for((i=0; i<rows; i++))
do
for((j=0; j<cols; j++))
do
index=$((i*cols+j))
echo -n "${matrix1[index]} "
done
echo
done
echo "Second matrix"
for((i=0; i<rows; i++))
do
for((j=0; j<cols; j++))
do
index=$((i*cols+j))
echo -n "${matrix2[index]} "
done
echo
done
k=0
matrix3=()
for((i=0; i<rows; i++))
do
for((j=0; j<cols; j++))
do
index=$((i*cols+j))
matrix3[k]=$((${matrix1[index]} + ${matrix2[index]}))
k=$((k+1))
done
done
echo "Addition of two matrix"
for((i=0; i<rows; i++))
do
for((j=0; j<cols; j++))
do
index=$((i*cols+j))
echo -n "${matrix3[index]} "
done
echo
done
print numbers in ascending order
declare nos[5]=(4 -1 2 66 10)
# Prints the number befor sorting
echo "Original Numbers in array:"
for (( i = 0; i <= 4; i++ ))
do
echo ${nos[$i]}
done
#
# Now do the Sorting of numbers
#
for (( i = 0; i <= 4 ; i++ ))
do
for (( j = $i; j <= 4; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
#
# Print the sorted number
#
echo -e "\nSorted Numbers in Ascending Order:"
for (( i=0; i <= 4; i++ ))
do
echo ${nos[$i]}
done
Reverse String In Shell Script
read -p "Enter string:" string
len=${#string}
for (( i=$len-1; i>=0; i-- ))
do
# "${string:$i:1}"extract single single character from string.
reverse="$reverse${string:$i:1}"
done
echo "$reverse"
Shell Script to check if a string is palindrome or not
echo "Enter a String"
read input
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo "$input is palindrome"
else
echo "$input is not palindrome"
fi
Shell script to find number of vowels in script
clear
echo
read str
len=`expr length $str`
count=0
while [ $len -gt 0 ]
do
ch=`echo $str | cut -c $len`
case $ch in
[(aeiouAEIOU)]
count=`expr $count + 1`
echo $ch
;;
len=`expr $len - 1`
done
echo $count