#!/bin/bash
#Here we use the row major representation of a two-dimensional matrix.
#Array ‘x’ is the original matrix and ‘r’ is the matrix after rotation.
#Matrix rotations in multiples of 90 degrees can be done by calling the rotate function repeatedly.
#perform clockwise-rotation by 90 degrees
rotate () {
t=(“$@”)
let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
r[$(($n*$i+$j))]=${t[$(($n*$(($n-$j-1))+$i))]}
j=$(($j+1))
done
i=$(($i+1))
done
}
#read the matrix order
read -p “Enter square matrix order: ” n
#accept elements
echo “Enter the elements”
let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
read x[$(($n*$i+$j))]
j=$(($j+1))
done
i=$(($i+1))
done
#call once to rotate clockwise by 90 degrees
rotate “${x[@]}”
#include the following line for 180 degree rotation
#rotate “${r[@]}”
#include the following line for 270 degree rotation
#rotate “${r[@]}”
#include the following line for 360 degree rotation
#rotate “${r[@]}”
#display the original matrix
echo “Original matrix”
let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
echo -n -e ${x[$(($n*$i+$j))]}”\t”
j=$(($j+1))
done
echo
i=$(($i+1))
done
#display matrix after rotation
echo “Matrix after rotating by 90 degrees is”
let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
echo -n -e ${r[$(($n*$i+$j))]}”\t”
j=$(($j+1))
done
echo
i=$(($i+1))
done
exit 0