Using Bourne shell : if you enter A B C D E F G.......................n after the command,how will you write a programme to reverse these positional parameters?

Showing Answers 1 - 1 of 1 Answers

Jitu

  • Feb 9th, 2007
 

Hi All,
You can use this command:-
$ echo "$@" | rev
Please make me clear if i am wrong.
Thanks,
Jitu

  Was this answer useful?  Yes

Amit

  • Aug 12th, 2007
 

i=$#
while(($i>=0))
do
echo $i
((i=$i-1))
done

  Was this answer useful?  Yes

UncaAlby

  • Nov 7th, 2008
 

str="alpha beta gamma"
echo $str | awk '{
for (i=NF; i>0; i--) printf ("%s ",$i);
printf("n");
}'

Results:

gamma beta alpha

If you're talking about parameters passed into a Shell script, that can be modified to the following:

echo $* | awk '{
for (i=NF; i>0; i--) printf ("%s ",$i);
printf("n");
}'



The previous answer using "awk" reversed all the characters, resulting in:

ammag ateb ahpla

Which I don't think is what was meant in the phrase, positional parameters.

Although, if reversing all the characters is what you wanted, then that's one good way to do it.

grohde

  • Jan 4th, 2011
 

Try this shell only version (uses Korn shell):

i=$#

while [[ $i > 0 ]]
do
  eval echo '$'$i
  ((i=$i-1))
done

  Was this answer useful?  Yes

grohde

  • Jan 4th, 2011
 

How about recursively.  Assuming a script named revarg.sh:

#!/bin/sh

arg=$1

if [ $# -gt 1 ]
then
  shift
  ./revarg.sh "$@"
fi

echo $arg

  Was this answer useful?  Yes

abdas

  • Jul 26th, 2011
 

$ cat rev2
i=$#
echo "$@" > file_1

while [ $i -gt 0 ]
do
cut -f $i -d" " file_1 >> file_2
i=`echo $i-1|bc`
done

echo `cat file_2`;
rm -f file_1 file_2;

$ sh rev2 A B C D E
E D C B A

  Was this answer useful?  Yes

shilpa keesara

  • Aug 7th, 2011
 

Using KSH while loop#!/bin/ksh
#Get the count of total number of arguments passed while executing this script
int i = $#; #total number of arguments passed#
names=$@; #list of actual arguments passed#
while [ ${i} -gt 0 ]
do
echo '${names[i]}';
i=`expr i - 1`;
done
exit 0;

  Was this answer useful?  Yes

vish

  • Aug 4th, 2014
 

Code
  1. for (( c=$#; c>=1; c-- ))

  2. do

  3. eval echo $$c

  4. done

  Was this answer useful?  Yes

eric

  • Sep 10th, 2014
 

Code
  1. for((c=$#;c>0;c--))

  2. do

  3. eval echo $$c

  4. done

  Was this answer useful?  Yes

Mayank Mishra

  • Sep 15th, 2014
 

for((c=$#;c>0;c--))
do
eval echo -n "$$c "
done

  Was this answer useful?  Yes

aaseem

  • Sep 21st, 2014
 

a=$#;
while [ $a -gt 0 ]
do
eval echo $$a
a=`expr $a - 1 `
done

  Was this answer useful?  Yes

gmatri

  • Oct 20th, 2014
 

Code
  1. count=$#  

  2. while [[ $count -gt 0 ]]; do

  3.   eval echo $$count

  4.   (( count -= 1 ))

  5. done

  Was this answer useful?  Yes

abdul

  • Nov 7th, 2014
 

i=$#

while [ $i -gt 0 ]
do
echo `echo "$*" | cut -d " " -f $i`
i=`expr $i - 1`
done

  Was this answer useful?  Yes

Mohit

  • Nov 14th, 2014
 

Code
  1. echo "$*"|awk {for(i=NF;i>0;i--) {printf $i " " }} ;

  Was this answer useful?  Yes

Vishal

  • Jan 27th, 2015
 

Code
  1. len=`echo $#`

  2. while [ $i -ge 1 ]

  3. do

  4. echo $i

  5. i=`expr $i - 1`

  6.  

  7. done

  8.  

  Was this answer useful?  Yes

Sanjeev

  • Apr 3rd, 2015
 

echo "$*" | rev

  Was this answer useful?  Yes

anu

  • Sep 16th, 2015
 

#!/bin/sh
#to print arguments in stdin in reverse order
for token in `echo $*| rev`
do
echo $token
done

  Was this answer useful?  Yes

Gaurav Sharma

  • May 16th, 2017
 

use the below statement in program body.
echo $*|rev

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions