LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-21-2012, 07:14 AM   #1
shambhavi
LQ Newbie
 
Registered: Sep 2012
Location: india
Posts: 11

Rep: Reputation: Disabled
Question shell script


$ecursive shell script that accepts any number of arguments and prints them in reverse order.
code:
Code:
 len=$#
  while [ $len -ne 0 ]
do
eval echo '$' $len
len=`expr $len - 1`
done
But when i used sh filename.sh hello hi, It is returning
Code:
 $2
 $1
as the output.
How to correct it?

Last edited by shambhavi; 09-21-2012 at 07:30 AM.
 
Old 09-21-2012, 07:30 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
can you please format this correctly using [ code ] tags? I've no idea what is bash and what is your improvised formatting.
 
Old 09-21-2012, 07:32 AM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
This code should do the work:

Code:
#!/bin/bash

#  array from input
_ARRAY=( $* )

#iterate over the array in reverse order.
for ((i=${#_ARRAY[@]}-1; i>=0; i--)); do
  echo "${_ARRAY[$i]}"
done
Code:
./filename.sh 3 5 74 2
2
74
5
3
 
Old 09-21-2012, 07:34 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
rosehosting.com, please don't give verbatim answers to homework...

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
 
Old 09-21-2012, 07:35 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by shambhavi View Post
How to correct it?
Remove the space from '$' $len

EDIT: that makes your solution work but does not meet the requirement to produce a recursive solution.

Last edited by catkin; 09-21-2012 at 07:37 AM.
 
Old 09-23-2012, 07:41 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'll just add my two three cents:

1) Never use eval. Not unless you know exactly what you are doing. And if you know what you are doing, you will almost never need it.

2) There is never any good reason to use expr in any modern shell. They all have perfectly good integer arithmetic built in.

3) Indirect/variable variables are also unnecessary in most modern shells, and sloppy and non-transparent in any case. There are better techniques, such as the array solution that rosehosting posted.
 
Old 09-23-2012, 08:22 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Well it's actually not necessary to use arrays there.
Code:
#!/bin/bash
for (( I = $#; I; --I )); do
    echo "${!I}"
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration