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 07-25-2010, 11:38 PM   #1
${manintheyellowhat}
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Rep: Reputation: 0
Question on variable naming and assignment...


can i use the value of one variable to generate a name for another variable?

for example i want to use the counter from a "do while" loop to name and define a variable each time the loop executes. for example

objectnames1=`ls -a`
objectnames2=`ls -a`
etc.

i don't have a script yet but each time through the loop i intend to cd to a particular directory and then define a variable containing a list of each object in that directory as values. for the rest of the script to work, each variable generated has to be unique, and i can't think of a good way to accomplish this.

if using a value from one variable to name another isn't possible, can anyone think of a more elegant solution? i know limited syntax but i'm willing to read up...
 
Old 07-26-2010, 04:19 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well you can probably use arrays or have a look at indirect references
 
Old 07-26-2010, 04:37 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
I'd probably use arrays. But if you have to use them intensively I'd better use another language, like python or perl. That is, unless you have no other option.

I also suggest you to take a look at this:

http://mywiki.wooledge.org/ParsingLs
 
Old 07-26-2010, 04:41 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
bash has basic arrays for you. If you have bash 4.0, you can use associative arrays.
For your case
Code:
#!/bin/bash
declare -a array
array=( $(ls -a) )
echo ${array[*]}
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}  #and so on
unless you are going to use these directory listing in later part of your script, otherwise, if its only a one time event, just use a for loop to go over your files (or using find etc ). No need to store them into arrays

Code:
#!/bin/bash
shopt -s nullglob
for file in *
do
  # do your stuff
done

Last edited by ghostdog74; 07-26-2010 at 04:43 AM.
 
Old 07-26-2010, 06:27 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
I agree arrays are probably what you need, but as per i92guboj I'd use Perl instead, judging by the brief description of your problem, and in that case, possibly hashes.
Depends exactly what the rest your requirement is...
 
Old 07-26-2010, 06:04 PM   #6
${manintheyellowhat}
LQ Newbie
 
Registered: Jul 2010
Posts: 2

Original Poster
Rep: Reputation: 0
this being my first time shell scripting for any purpose, i didn't even know that bash had support for arrays. thanx everyone for pointing me in the right direction. did some reading... learned the syntax... problem solved.
 
Old 07-26-2010, 07:41 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Quote:
problem solved
Please mark call accordingly
 
  


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
Bash Variable Naming Question? Speedy2k Linux - Newbie 7 06-01-2010 08:23 AM
Variable assignment in Lisp. zaxonus Programming 2 03-18-2010 06:10 AM
[SOLVED] [BASH] variable assignment help RaptorX Programming 2 08-30-2009 10:28 AM
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 02:39 PM
variable naming conventions patpawlowski Programming 6 02-25-2004 01:49 PM

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

All times are GMT -5. The time now is 05:37 AM.

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