I have one scenario here. I want to extract the value of a variable as mentioned below using a shell script (bash).
I am having a file say File A. Inside the file, I have a variable assigned a string value.
File A:
Var1="value of the variable"
In a particular script "variable_name.sh", I am getting the input as Var1. Inside the script I am sourcing the File as well.
Executed as ./variable_name.sh Var1
I want to store the Var1's value inside another variable in the script as mentioned below.
Var_val=$1
extracted_variable=`echo "$[$Var_val]"`
Output required:
value of the variable
But when I try this, it is returned as 0. If I assign any number to Var1 (say Var1=100) then it is returning the value as 100.
Could you please help me in extracting the string value that is stored in Var1?
Not sure if this is what you are after:-
Longhand using OSX 10.7.5, default bash terminal...
Last login: Tue Sep 15 09:01:15 on ttys000
AMIGA:barrywalker~> echo "VAR1='This is a test.'" > /tmp/variable
AMIGA:barrywalker~> source /tmp/variable
AMIGA:barrywalker~> VAR_VAL=$VAR1
AMIGA:barrywalker~> echo "$VAR_VAL"
This is a test.
AMIGA:barrywalker~> _
If you evaluate a text variable, it will evaluate to 0. Quote the variable when used as a parameter to a function as it contains spaces. You don't need the echo when assigning to a new variable.
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...
#!/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:-
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...
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> _