LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-07-2004, 02:47 PM   #1
zoomzoom
Member
 
Registered: Aug 2003
Posts: 55

Rep: Reputation: 15
small bash script issue


Hallo,

maybe this fits better in the newbie forum:

Code:
for ((i=$#;i>0;i--)); do ...; done
in the do-part, i know want to output the $1,$2,$3,... depending on the current value of i.

I tried various things like 'echo $($i0)' or 'echo $$i0' or 'echo ${$i0}' ...

So, how do you do this?

thx
bye
 
Old 06-07-2004, 03:22 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Try this one...

echo -n "$i"
 
Old 06-07-2004, 03:31 PM   #3
zoomzoom
Member
 
Registered: Aug 2003
Posts: 55

Original Poster
Rep: Reputation: 15
doesn't work. this outputs the variable i.
What i want to output is the VARIABLE/PARAMETER $1,$2,...

example:

echo $1, if i=1,
echo $2, if i=2,
echo $3, if i=3, and so on...

i hope i explained it better now ...

Last edited by zoomzoom; 06-07-2004 at 03:37 PM.
 
Old 06-08-2004, 09:46 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If you want to loop through the arguments themselves
you could assign i to these values to begin with:

#!/bin/bash
for i in "$*"; do
echo $i
done
 
Old 06-08-2004, 12:41 PM   #5
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
for i in 1 2 3 4 5 6 7
do
echo $i
done
 
Old 06-08-2004, 12:55 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Unhappy

Sorry, marghorp, but your scripts prints out the numbers of I rather than the corresponding arguments to the script.
 
Old 06-08-2004, 01:07 PM   #7
marghorp
Senior Member
 
Registered: Jan 2004
Location: Slovenia
Distribution: Slackware 10.1, SLAX to the MAX :)
Posts: 1,040

Rep: Reputation: 45
Sorry for that, I completely missed the point here. Kind a noob myself when it comes to bash scripting, yet.
 
Old 06-08-2004, 06:33 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
maybe approaching it from a different angle will help. I assume you have a bunch of variables assigned, like $1="foo" $2="bar" $3="gah", etc.

Could you put those values into an array and then loop thruogh the array? You could do something like this in that case (granted, this is tcsh, but bash is similar):

Code:
set array=(foo bar gah)
foreach i ($array)
  echo $i
end
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Very small bash script enemorales Programming 8 02-25-2005 02:47 AM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 09:09 AM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 11:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:19 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