eval and variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting eval and variable assignment
# 1  
Old 04-01-2010
MySQL 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 commandline and assigning the values of these
variables to another set of variables using while get opts command as below,

f) eval FILE=\$${OPTARG}
FILES="$FILE $FILES";;

Issue - the array variable FILES is not populated with filenames here.

2) Issue with filenames - I have filenames which consists of spaces and special characters.
eg: REP - inven - 1.TXT
I am using same concept as above. i.e. passing variables to script. i am getting error
with eval command.

Please advise in both scenarios on how to avoid issues.

Thanks in advance..

Regards,
Mohan
# 2  
Old 04-01-2010
Please post the entire script.
# 3  
Old 04-01-2010
To populate an array the correct syntaxes are:
Code:
ARRAY=( "Elem1" "Elem 2" "Third element" "Four has index 3" )
# or
ARRAY[$index]="Element X"

Try to put quotes + escaped quotes
Code:
eval "FILE=\"\$${OPTARG}\""

# 4  
Old 04-05-2010
MySQL eval and variable assignment

Hi Frans,

Thank you very much. First step is working for me.

regarding the step#2, i maintain all the job related variables in a config script which gets evaluated at the start of every job. The below variables are giving error while evaluating,

Var1="REP0022 - Yesterday's account savings_ 1.TXT"

Error :
ksh -: not found

I tried enclosing above filename in single quotes but of no use.

Thanks in advance.

Regards,
Mohan
# 5  
Old 04-05-2010
You should initialize the variable like
Code:
STRING="Var1=\"REP0022 - Yesterday's account savings_ 1.TXT\""
eval "$STRING"

# 6  
Old 04-05-2010
MySQL eval and variable assignment

HI Frans,

Thank you. I am able to assign the filenames to a variable now . I have 3 variables like this. all of these are assigned to an array. i am able to get part of the filename (i.e. till the space as a value)while retrieving .

Following is the code :

storing the values in FILES string after we receive the values from commandline,
f) eval FILE=\"\$${OPTARG}\"
FILES="$FILE $FILES";;

assigning the FILES string to an array : set -A filesToGet $FILES

Retrieving each file from the array : file=${filesToGet[$curFileIndex]}

but this way it is considering only part of the filename.i.e. till the first space as a first filename. Please suggest how can i retrive the whole filename i.e. REP00029 - Yesterdays Postings Total_ 1.TXT

Once again thanks in advance..

Regards,
Mohan
# 7  
Old 04-05-2010
Maybe you should directly construct the array like this:
Code:
# to add $FILE (string) to $FILES (array)
$FILES[${#FILES[@]}]="$FILE" # ${#FILES[@]} gives the nb of elements in the array => the first empty index (first is 0)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

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

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

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

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

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

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

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

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question