LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 05-03-2006, 07:10 PM   #1
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Rep: Reputation: 15
Question Scripting: accessing a variable stored in a variable?


ok,
I don't know how many args are going to be input for this script.
so I want to do a while loop.

the counter <= MAX

I'm trying to get what is stored in $1, $2, etc.

filename=$"$count"

Thanks,
Tom
 
Old 05-03-2006, 11:41 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

If you use bash, $# holds the amount of parameters given. Man bash fordetails.

This is also true for ksh.

Hope this helps.
 
Old 05-05-2006, 06:11 PM   #3
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
Thanks,

that's half.

I want a while count <= $#
do
ls $$count
done

you could write a case for $1 to $9 but ... does that make sense?
 
Old 05-05-2006, 06:30 PM   #4
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by jlliagre
Try:
Code:
eval echo \$$y
I found this. it worked.
 
Old 05-05-2006, 06:46 PM   #5
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
There are a number of ways to loop over arguments

Code:
while [[ -n $1 ]]; do
      ls $1
      shift
done
or

Code:
for arg in $@; do
      ls $arg
done
or (in zsh, won't work in bash as it can't expand variables inside the {..}, apparently):

Code:
for i in {1..$#}; do
      ls $argv[$i]
done
and there are undoubtedly a few other ways too. Note, in bash you'll want to quote all the $variables in case they have spaces in them.
 
Old 05-05-2006, 08:47 PM   #6
tomolesonjr
Member
 
Registered: Mar 2006
Location: Iowa
Distribution: Suse 10.0
Posts: 70

Original Poster
Rep: Reputation: 15
Code:
#!/bin/bash
max=$(expr $# + 1)
count=1
while [ $count -lt $max ]
do
	

	for FILE in $(eval echo \$$count)
	do

		ls -l $FILE

	done
	
count=$(expr $count + 1)

done
I got this to work. I'm going to try the other ways. cool.
 
  


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
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM
shell scripting: variable problem XST1 Programming 7 01-30-2005 01:45 PM
error in accessing structure variable in kernel modules dypgrp Programming 3 01-17-2005 07:42 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration