LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 11-06-2009, 12:43 PM   #1
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Rep: Reputation: 15
[SOLVED] for loop with variables


I'm writing a small script for which I would appreciate any help from you learned and generous fellows.

I need to echo a long list of variables like this:
Code:
filename=members.txt
filetype=text
filepath=/root/
etc.

These variables are not fixed like above but are the result of the output of several commands.
My first (naive) thought was to do:

Quote:
VARGROUP=(filename filetype filepath)

for i in vargroup ; do
echo "$i=$i"
done
I obviously get
Code:
members.txt=members.txt
etc.
since the echo expression expands both instances.
Is there a way to echo the unexpanded variable (the name of the variable) inside the for loop?

Thanks in advance and sorry for my lack of (or misuse of) technical vocabulary.

Last edited by musonio; 11-06-2009 at 02:46 PM.
 
Old 11-06-2009, 12:49 PM   #2
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
I'm not very sure what you want, but if you want to output $i=members.txt you can by doing
Code:
echo "\$i=$i"
 
Old 11-06-2009, 12:55 PM   #3
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
ammorais:
Thanks for your quick reply, but that would output:
Code:
$i=members.txt
$i=text
$i=/root/
What I want is:

Code:
filename=members.txt
filetype=text
filepath=/root/
Any other possibility
 
Old 11-06-2009, 01:20 PM   #4
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
Code:
#!/bin/bash

filename=members.txt
filetype=text
filepath=/root/


VARGROUP=("filename filetype filepath")

for i in $VARGROUP ; do
eval b=\$$i
echo "$i=$b"
done
 
Old 11-06-2009, 02:45 PM   #5
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
I knew I had come to the right place.
THANKYOU.
You have saved me hours of manual work.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does this loop clear variables? novaraz Programming 6 10-25-2008 08:40 AM
Using for loop on 2 variables muazfarooqaslam Linux - Newbie 8 02-01-2008 08:03 AM
How to check variables in a FOR loop? richikiki Programming 6 07-10-2006 03:57 AM
shell variables becoming zero outside the loop cool244 Programming 4 05-20-2006 03:33 PM
shell variables losing value outside while loop cool244 Programming 1 05-19-2006 10:04 AM


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