LinuxQuestions.org
Review your favorite Linux distribution.
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, 08: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 08:30 AM.
 
Old 09-21-2012, 08:30 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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, 08:32 AM   #3
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
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, 08:34 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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, 08:35 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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 08:37 AM.
 
Old 09-23-2012, 08:41 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 09: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 09:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration