assign multiple rows value to a variable using eval


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting assign multiple rows value to a variable using eval
# 1  
Old 05-16-2012
assign multiple rows value to a variable using eval

background : Solaris, ksh

Code:
metresult="ooo
> pp"
ts=89
eval append_${ts}="$metresult"
bash: pp: command not found

I want to create a variable which has in a part of its name a dynamically-established number (stored in another variable) usually I do this with eval command. The problem I have now is that I want to assign to this variable a multiple row value and I get the error you see above ("pp" is the word from the second row from $metresult value.

If I want to assign a single-row-value to such a variable using eval everything works fine :

Code:
 metresult="ooo"
ts=89
eval append_${ts}="$metresult"
echo $append_89
ooo

I do something wrong syntactically speaking and I wonder if you have another approach. I do not want workarounds with arrays a.s.o. I only want to know this : how to assign multiple rows value to a variable using eval command

THX
# 2  
Old 05-16-2012
Quote:
Originally Posted by black_fender
The problem I have now is that I want to assign to this variable a multiple row value and I get the error you see above ("pp" is the word from the second row from $metresult value.
This is the culprit. The "eval" sends the line through the evaluation process a second time, but the first time it has already eaten away your quotation and this second time the newline terminates the line. You have to escape the quotation to protect it for the correct evaluation pass to interpret it.

Notice the difference between:

Code:
echo "abc"                # evaluated once, but the "" are missing in the output
echo "\"abc\""            # evaluated once, the inner "" are there, without escaping
eval echo "\"abc\""       # second evaluation ate the second pair of ""
eval echo "\\"abc\\""     # two evaluations need two-time escaped quoations

I hope this helps.

bakunin

Last edited by bakunin; 05-16-2012 at 12:19 PM..
This User Gave Thanks to bakunin For This Post:
# 3  
Old 05-16-2012
This was tested in the 'ksh' shell in Solaris since thats what you posted as your environment, it concatenates the variable with two lines(metresult) with the variable(ts):
Code:
$ metresult="ooo\n> pp"
$ ts=89
$ ts=$ts$metresult
$ print $ts
89ooo
> pp

# 4  
Old 05-16-2012
I'd use real array's if possible. Otherwise eval append_${ts}='"$metresult"' so that "$metresult" is intact during 2nd evaluation without any backslashes to worry about. when I have to use eval I usually single quote everything except what I specifically want evaluated prior. eval 'append_'$ts'="$metresult"'
# 5  
Old 05-16-2012
Quote:
Originally Posted by neutronscott
when I have to use eval I usually single quote everything except what I specifically want evaluated prior.
This is correct, but in this case the single quotes are doing the same as the escaping backslashes: they prevent everything inside from being evaluated at the first pass - including the double quotes, which lose their special meaning to the shell when inside the single quotes.

You can watch this effect when using several numbers of eval's on the same line (which is - save for demonstration - of course of no practical value although syntactically correct). Correct escaping with several backslashes will manage even this tricky predicament, while single quotes will get interpreted away like double quotes do.

I hope this helps.

bakunin
# 6  
Old 05-17-2012
Quote:
Originally Posted by bakunin
This is the culprit. The "eval" sends the line through the evaluation process a second time, but the first time it has already eaten away your quotation and this second time the newline terminates the line. You have to escape the quotation to protect it for the correct evaluation pass to interpret it.

Notice the difference between:

Code:
echo "abc"                # evaluated once, but the "" are missing in the output
echo "\"abc\""            # evaluated once, the inner "" are there, without escaping
eval echo "\"abc\""       # second evaluation ate the second pair of ""
eval echo "\\"abc\\""     # two evaluations need two-time escaped quoations

I hope this helps.

bakunin

Yes, Bakunin. This approach got my problem solved. I had to "save" a second pair of "" for the 2nd evaluation by using \ .

Many thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Safe way to eval variable declarations?

Is there a safe way to evaluate variable declarations within a script whether they come from a .conf file, user input, or stdin? Example .conf file: server=ftp.xxxx.com port=21 user="$USER" # Hopefully allow this type of substitution domain="$DOMAIN" server="$(malicious... (4 Replies)
Discussion started by: Michael Stora
4 Replies

2. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

Split single rows to multiple rows ..

Hi pls help me out to short out this problem rm PAB113_011.out rm: PAB113_011.out: override protection 644 (yes/no)? n If i give y it remove the file. But i added the rm command as a part of ksh file and i tried to remove the file. Its not removing and the the file prompting as... (7 Replies)
Discussion started by: sri_aue
7 Replies

4. Shell Programming and Scripting

assignment to variable from eval command

Hi Gurus, I am having 2 parameters as below parm1=value1 parm2=parm1 I want to evaluate parm1 value using eval echo \$$parm2 and later i want to assign this value to other variable which i will be using in if statement like : if ]; then do this....... fi could you please suggest... (5 Replies)
Discussion started by: k_vikash
5 Replies

5. UNIX for Dummies Questions & Answers

How to assign scores to rows based on column values

Hi, I'm trying to assign a score to each row which will allow me to identify which rows differ. In the example file below, I've used "," to indicate column separators (my actual file has tab separators). In this example, I'd like to identify that row 1 and row 5 are the same, and row 2 and row... (4 Replies)
Discussion started by: auburn
4 Replies

6. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

7. Shell Programming and Scripting

Passing eval value to a variable

Hello, I have a script that does an scp to a server and then gets the number of process running on that server, the o/P should be stored in a variable for further processing eval `echo "ssh -q $Infa_user@$host 'csh -c $CMD '"` where CMD="ps -ef | grep -i ${INFA_REPO} | grep -v grep | wc... (2 Replies)
Discussion started by: amit1_x
2 Replies

8. Shell Programming and Scripting

bin/sh eval variable assignment

Why can't I do this? eval "TEST=5;echo $TEST;"; THIS WORKS!! TEST=5;echo $TEST; (2 Replies)
Discussion started by: blasto333
2 Replies

9. Shell Programming and Scripting

How to assign eval value as Variable..

Im facing problem in assigning value of eval array variable as normal variable.. x=0 eval DATA${x}="FJSVcpcu" x=`expr $x + 1` eval DATA${x}="FJSVcsr" if x=0, type -> eval echo \$DATA$x , its give me FJSVcpcu i want assign this value into an variable as variable=`eval echo... (3 Replies)
Discussion started by: neruppu
3 Replies

10. Shell Programming and Scripting

eval a variable that has a .

Hi, Is there any way that I can eval the following - eval abc.csv=def.csv I am getting the - bash: command not found error. thanks. (3 Replies)
Discussion started by: ttshell
3 Replies
Login or Register to Ask a Question