LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (bash) how to list all variables? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-list-all-variables-305017/)

Duudson 03-23-2005 06:18 AM

(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


Duudson 03-23-2005 06:31 AM

/tmp/variables looks like this:

A=1
B=2
C=3
#D=5

TheLinuxDuck 03-23-2005 08:58 AM

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?

zeropash 03-23-2005 09:42 AM

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

keefaz 03-23-2005 11:16 AM

And maybe you could store the output of set, source the file, store another output of set, then
compare the outputs

Duudson 03-23-2005 11:18 AM

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!

Duudson 03-23-2005 11:21 AM

Double post.

dustu76 03-23-2005 11:28 AM

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.

TheLinuxDuck 03-23-2005 11:52 AM

dustu76:

Nice!! (=

Duudson 03-23-2005 12:31 PM

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!

bigearsbilly 03-24-2005 07:37 AM

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


rnturn 03-24-2005 12:35 PM

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.)


All times are GMT -5. The time now is 01:52 AM.