7A) Non-recursive shell script to accepts arguments and print them in the Reverse order [VTU SS&OS]

vtu ss & os 7a
Code:
Explanation:
A simple program to accept command line arguments, and print them in reverse order.

executing procedure:
sh 7a.sh ant bat cat

where ant,bat,cat are inputs. which are to be displayed back in reverse format.

echo "Non Recursive shell Script"
echo "The total number of arguments specified:$#"
echo "The given $# arguments are :$*"

$# -> total number of arguments
$* -> list of all arguments

temp1=$#
echo "The arguments in reverse order:"

hold the total number of arguments in a variable temp1, and use it in loop to dislpay each input word in reverse format, each time decrementing temp1 by 1, until it becomes 0.

while [ $temp1 -ge 1 ] 
do
eval echo \$$temp1
temp1=`expr $temp1 - 1`
done 
echo "End of program"

while temp1 is greater than 1, we display element at 'temp1'th position (this is done using \$$ operator) , then decrease temp1 by 1 using expr command. (dont forget to place them inside grave accent symbol `. and not single quotes '.)
This way all elements from last index to first index is displayed until temp1 becomes 0.

so output for above given input should be
cat bat ant

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!