To print value for a $variable inside a $variable or file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To print value for a $variable inside a $variable or file
# 1  
Old 02-16-2014
To print value for a $variable inside a $variable or file

Hi guys,

I have a file "abc.dat" in below format:


Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as literals and not giving the values for those variables. Below is the script I am using:

echo `echo $(cat abc.dat|head -1|awk -F'|'+ '{print $2}')`

The above command gives me $F_PATH. But now how to get the value for this parameter, I am unable to do and kinda stuck.

Appreciate your help in advance.

Last edited by Scott; 02-16-2014 at 07:48 AM.. Reason: Code tags
# 2  
Old 02-16-2014
eval echo `echo $(cat abc.dat|head -1|awk -F'|'+ '{print $2}')`

# 3  
Old 02-16-2014
OSX 10.7.5, default shell.
I am assuming you want just some values so therefore I created some to test with.
A version using __builtins__ only.
(Note:- your code on this machine causes odd errors so......)
# Create a file as per the original.
echo "FILE_PATH||||\$F_PATH
SYST_NM||||\${SRC_SYST}" > /tmp/text
# Use "hexdump" as a proof test to check that it is as the original.
hexdump -C < /tmp/text
# Set up the terminal ready.
# Thanks Corona688 for this little snippet.
# Just simulate some path...
# Assume below is a number.
# Extracing the variables...
read -d '' line < /tmp/text
echo "$line"
# This is the working code using __builtins__ only...
for n in $( seq 0 1 ${#line_array[@]} )
	if [ "${line_array[n]}" == '$F_PATH' ]
		eval line=$(echo "${line_array[n]}")
		# This should be "/tmp"...
		echo "$line"
	if [ "${line_array[n]}" == '${SRC_SYST}' ]
		eval line=$(echo "${line_array[n]}")
		# This should be "3"...
		echo "$line"
# End of working code and return terminal IFS to default...
exit 0

Last login: Sun Feb 16 16:14:23 on ttys001
AMIGA:barrywalker~> chmod 755
AMIGA:barrywalker~> ./
00000000  46 49 4c 45 5f 50 41 54  48 7c 7c 7c 7c 24 46 5f  |FILE_PATH||||$F_|
00000010  50 41 54 48 0a 54 41 42  4c 45 5f 4c 49 53 54 7c  |PATH.TABLE_LIST||
00000020  7c 7c 7c 61 7c 62 7c 63  0a 53 59 53 54 5f 4e 4d  ||||a|b|c.SYST_NM|
00000030  7c 7c 7c 7c 24 7b 53 52  43 5f 53 59 53 54 7d 0a  |||||${SRC_SYST}.|
AMIGA:barrywalker~> _

# 4  
Old 02-16-2014
awk -F'|' '
        $NF ~ /\$/ {
                gsub ( /\$|\{|\}/, X, $NF)
                print ENVIRON[$NF]
' abc.dat

# 5  
Old 02-16-2014
@Yoda: works fine if the variable is part of the environment, i.e. it has been exported, not just assigned only.
# 6  
Old 02-17-2014
Here is an example using bash:

$ cat exp_var
while read a b c d var
  if [ "${var:0:1}" = "$" ]
     echo $var=${!var}
done< abc.dat

$ ./exp_var

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Resolve variable inside another variable

Hello Everyone, I am trying to resolve a variable inside another variable.Let me go straight to the example. Input: Query=$Table_1 Join $Table_2 (Query itself is a variable here) Now for two different cases I am assigning different values to Table_1 and Table_2 Case 1:... (14 Replies)
Discussion started by: vinay4889
14 Replies

2. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

3. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

4. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

5. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

6. UNIX for Dummies Questions & Answers

Using a variable inside a file to cat another.

I have a question to do and it's somewhat confusing. It says, and I quote "Create a file called file_1 with three lines of text in it. Create a shell variable called "f_name", assign it the string "file_1". Use the cat command and the variable "f_name" to display the contents of the file... (3 Replies)
Discussion started by: MaestroRage
3 Replies

7. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

8. Shell Programming and Scripting

How to replace variable inside the variable

hi sir, i need your help for this script inside /rnmucdr/ednms05/ken/xMNBDF045_Script.sql content variable like this select * from invoice where bill_date=$BILLDATE and startNum=$STARTPARTNNUM and total_partn=$TOTALPARTN if i just paste this replace with the $SCRIPT it works great,if... (31 Replies)
Discussion started by: mani_um
31 Replies

9. Shell Programming and Scripting

variable inside awk '{print $c}'

i'm trying to do this (in bash darwin); echo "give me some words: " read a c=2 # this is get by other ways echo $a | awk '{print $c}' # i want to print the column given # by de $c variable if there is someone understand what i pretend... (3 Replies)
Discussion started by: Tártaro
3 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question