GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Operating System  >  Shell Scripting
Go To First  |  Previous Question  |  Next Question 
 Shell Scripting  |  Question 30 of 44    Print  
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?

  
Total Answers and Comments: 4 Last Update: November 07, 2008     Asked by: Nehashri 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: UncaAlby
 
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.

Above answer was rated as good by the following members:
jrgalaura
February 09, 2007 03:45:38   #1  
Jitu        

RE: Using Bourne shell : if you enter A B C D E F G......
Hi All,

You can use this command:-

$ echo "$@" | rev

Please make me clear if i am wrong.


Thanks,

Jitu

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 12, 2007 03:28:35   #2  
Amit        

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

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
May 27, 2008 03:19:40   #3  
kamalakar Navana Member Since: May 2008   Contribution: 1    

RE: 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?
str="A B C D E F G"
echo $str | awk '{
for(i=length($0);i>=1;i--)
printf("%s",substr($0,i,1));
}'

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
November 07, 2008 04:40:26   #4  
UncaAlby Member Since: October 2008   Contribution: 13    

RE: 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?
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape