LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-23-2005, 06:18 AM   #1
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Rep: Reputation: 15
(bash) how to list all variables?


Hi,

how can I list all variables that are sourced from another file?

Code:
#!/bin/bash
. /tmp/variables
"list_all_varibles" > /root/variables_in_use.txt
 
Old 03-23-2005, 06:31 AM   #2
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
/tmp/variables looks like this:

A=1
B=2
C=3
#D=5
 
Old 03-23-2005, 08:58 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
I think that no matter how you play it, you're going to have to do some work. AFAIK, there is no easy solution. If your variable file will always look like your example, you can write the bash to look for lines that don't start with # and which contain an equals sign (=) and strip off the equals and anything after it.. that would at least provide you with the variables that have been assigned.

That's about the best of which I can think at the moment.

Anyone else?
 
Old 03-23-2005, 09:42 AM   #4
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
ddi you want all the variables sourced from a particular file?
if you wanted all the variables in use then you can use
set

Last edited by zeropash; 03-23-2005 at 09:43 AM.
 
Old 03-23-2005, 11:16 AM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 66
And maybe you could store the output of set, source the file, store another output of set, then
compare the outputs
 
Old 03-23-2005, 11:18 AM   #6
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by zeropash
ddi you want all the variables sourced from a particular file?
if you wanted all the variables in use then you can use
set
I'm not very familiar with the set command, but it seems that this works:
Code:
#!/bin/bash
set -a
. /tmp/variables
env > /root/variables_in_use.txt
edit:
or this seems to work as well (and I think this is what you were talking about):
Code:
#!/bin/bash
. /tmp/variables
set > /root/variables_in_use.txt
Those list also all normal environment variables, but it's ok.

Thanks!

Last edited by Duudson; 03-23-2005 at 11:23 AM.
 
Old 03-23-2005, 11:21 AM   #7
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
Double post.
 
Old 03-23-2005, 11:28 AM   #8
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
sdiff can help to filter the "unnecessary" stuff out:

Code:
#!/bin/bash
env > env.1
. /tmp/variables
env > env.2
sdiff -s env.1 env.2 |cut -d">" -f2 > /root/variables_in_use.txt
HTH.
 
Old 03-23-2005, 11:52 AM   #9
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
dustu76:

Nice!! (=
 
Old 03-23-2005, 12:31 PM   #10
Duudson
Member
 
Registered: Dec 2004
Distribution: RHEL3, FC3
Posts: 53

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by dustu76
sdiff can help to filter the "unnecessary" stuff out:

Code:
#!/bin/bash
env > env.1
. /tmp/variables
env > env.2
sdiff -s env.1 env.2 |cut -d">" -f2 > /root/variables_in_use.txt
HTH.
Env doesn't work in that example, but set does.

sdiff seems to clean most of the "useless" variables away.

Thanks!
 
Old 03-24-2005, 07:37 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,269

Rep: Reputation: 165Reputation: 165
set | grep -f variables-in-use
this works, but no blanks allowed in the data file

DATA
Code:
billym.primadtpdev>cat ~/1
A=1
B=2
C=3
#D=5

SCRIPT
Code:
billym.primadtpdev>cat ~/2
. ~/1
echo we get:
set | grep -f ~/1
RESULT
Code:
billym.primadtpdev>ksh ~/2
we get:
A=1
B=2
C=3
 
Old 03-24-2005, 12:35 PM   #12
rnturn
Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0), SuSE (10.x, 11.x, 12.2), Solaris (8-10), Tru64
Posts: 889

Rep: Reputation: 45
Quote:
Originally posted by keefaz
And maybe you could store the output of set, source the file, store another output of set, then
compare the outputs
That's pretty much the way I'd handle this.
Code:
set>/tmp/variables.0
. file-containing-variable-defs
set>/tmp/variables.1
echo "file-containing-variable-defs defined the following:"
cat /tmp/variables.0 /tmp/variables.1 | sort | unit -u
rm /tmp/variables.[01]
I suppose env would work just as well in the above (as some others suggested). If you only want the variable names and not their values, just pipe the result from "uniq" into "cut -d= -f1"

I wrote a shell function a long time ago that did something like (I think) Duudson was looking for. It was to allow someone to display the contents of a config file that was being used in a script. It read the config file a line at a time rather than sourcing it and only displayed the non-comment lines before defining the variable. (I didn't want to deal with temp files at the time even though sourcing the file would have been faster.)
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
variables in bash haddad Linux - General 6 09-22-2004 05:29 PM
How can I get the full list of environment variables (RedHat 7.2)? yuzuohong Linux - General 1 06-07-2003 03:28 AM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM
Bash variables pk21 Programming 2 01-09-2003 03:31 PM


All times are GMT -5. The time now is 10:49 PM.

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