Extracting value of a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting value of a variable
# 8  
Old 09-15-2015
Quote:
Originally Posted by guknvivek
My requirement in simple words,

Var1="Value"
Var2=Var1

I want to assign "Value" to Var2. As per my original post, If I have a number instead of "value" then I am able to retrieve it.

Thanks
So you want to turn a variable name into its contents?

If you have a new enough shell, you can do this:

Code:
VAR="asdf"
VARNAME="VAR"

echo "${!VARNAME}"

You can do the opposite, setting any variable name you want, with this trick:

Code:
VARNAME="VAR"

read "$VARNAME" <<EOF
VALUE
EOF

echo $VAR

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 09-15-2015
DON'T use arithmetic expansion when assigning a variable's contents to another var. Use VAR2="$VAR1".
# 10  
Old 09-15-2015
If you have no idea what the variable name is from the sourced file nor its contents then you could diff the ENVIRONMENT before and after sourcing this would give the variables and their contents.
However here is an idea that finds the variable name, its contents and transfers the contents into another variable...
Code:
#!/bin/sh
# value
# Create a simple test file to open up...
echo 'VAR1=125' > /tmp/variable
# VAR1=125 sourced from $1...
$1
# Obtaining the filename from $1, firstly create a simple array.
VAR2=( $1 )
# The second part of the string is the /full/path/to/filename.
VAR2=${VAR2[1]}
# Save the contents of the file to variable VAR2, VAR1 belongs to the sourced file.
VAR2=$(cat $VAR2)
# Show the variable and its value.
echo $VAR2
# Create a new local variable.
MYVAR=""
# Find the sourced variable name.
for n in $( seq 0 1 ${#VAR2} )
do
	if [ "${VAR2:$n:1}" == "=" ]
	then
		break
	fi
	MYVAR=$MYVAR${VAR2:$n:1}
done
echo "The sourced variable name is $MYVAR..."
# Now dump the contents of $MYVAR, 'VAR1' into a reused variable VAR2.
eval VAR2='$'"$MYVAR"
echo "The sourced variable value is $VAR2..."

Results:-
Code:
Last login: Tue Sep 15 19:48:21 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./value 'source /tmp/variable'
VAR1=125
The sourced variable name is VAR1...
The sourced variable value is 125...
AMIGA:barrywalker~/Desktop/Code/Shell> _

If you want to get all the variables, their contents and transfer to your local variables from a script then this is much more difficult...
# 11  
Old 09-17-2015
Hi,

I have again explained my requirement.

FileA has this content:
Code:
Variable1="Value";

In the script, which is invoked by a single parameter.
./value_extractor.sh Variable1
Code:
. FileA
Retrieved_value=`echo $[$1]`
echo $Retrieved_value
exit 0

# 12  
Old 09-17-2015
Please read my previous post. I explain how to use a variable containing a variable name to get the contents of that other variable.
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 09-17-2015
Puzzled; is this what you are after?
OSX 10.7.5, default terminal running 'sh'...
Code:
#!/bin/sh
# Value_extractor.sh Variable1
echo 'Variable1="This is a string...";' > /tmp/FileA
. /tmp/FileA
eval echo '$'"$1" > /tmp/someval
Retrieved_value=$( cat /tmp/someval )
echo $Retrieved_value

Results:=
Code:
Last login: Thu Sep 17 20:15:40 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> chmod 755 Value_extractor.sh
AMIGA:barrywalker~/Desktop/Code/Shell> ./Value_extractor.sh Variable1
This is a string...
AMIGA:barrywalker~/Desktop/Code/Shell> _

This User Gave Thanks to wisecracker For This Post:
# 14  
Old 09-17-2015
Thanks Corona688.. The '!' helped us to retrieve the value. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring from variable

Hi All, I'm facing issue wherein I have 2 character string like 'CR' and 'DR' and I want to extract just 1st character but am unable to do it. I tried below options but they are returning me 2nd character only, var="CR" echo ${var:1} returns just "R" echo ${var:0} returns "CR" ... (5 Replies)
Discussion started by: arvindshukla81
5 Replies

2. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

3. Shell Programming and Scripting

Extracting characters and storing in some variable

eg: ./myProgram.sh filename.cpp I want to remove the :".cpp" extension from the filename.cpp expected output: filename (3 Replies)
Discussion started by: umesh314
3 Replies

4. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

Extracting variable value from file

Hi All i have created a file as below cat > REJ_FILE_LIST.dat CUST.DAT,$ABC/TEST.DAT|$ABC/TEST1.DAT|$ABC/TEST2.dat Ctrl+d in another script the variable is set as ABC='/data/proj/Cust' the requirement is to read the second column from file REJ_FILE_LIST.dat which contains... (1 Reply)
Discussion started by: zulfi123786
1 Replies

7. Shell Programming and Scripting

Extracting data from SQL using a variable from a file

Though, its not new, I am a newbie to shell scripting. Please help me with the below. There are two steps. First one is to read a table and create a file with the distinct values. I am able to get this done. Second step is to read the file (which has the single value, like a number) and... (1 Reply)
Discussion started by: singas1
1 Replies

8. Shell Programming and Scripting

Extracting a word from a variable

Hi Guys, Need you quick assistance on the below, trying to extract a word from a variable i.e. acmi101acmi102acmi103acmi104 When i use the following code awk '{gsub(/cmi102/,"")};1' it leaves a space in the variable, need to get rid of the space that it leaves. Any ideas. the above... (3 Replies)
Discussion started by: eo29
3 Replies

9. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

10. Shell Programming and Scripting

extracting value in variable??

Hi in Below example. i want "n = aa.txt.gz" ????????????? plz help m=aa.txt n=echo`gzip $m` echo $n (1 Reply)
Discussion started by: manish.s
1 Replies
Login or Register to Ask a Question