eval and variable assignment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting eval and variable assignment
# 8  
Old 04-05-2010
MySQL eval and variable assignment

Hi Frans,

i am passing 3 variables(which contain filenames) to the script and storing these in a string called FILES. Here FILES is not an array.later i am assigning this string FILES to an array using set -A definition. Please advise on how to retrieve the whole filename at once after i assign the FILES string to an array.

Regards,
Mohan
# 9  
Old 04-05-2010
What i meant is building an array (FilesToGet) in place of a string, so you won't need to use the set -A.
what is the result of ?
Code:
for ((i=0; i<${#FilesToGet[@]}; i++))
do
    echo "$i - ${FilesToGet[$i]}"
done

It would be fine if you post the entire script (between code tags!)
# 10  
Old 04-05-2010
MySQL eval and variable assignment

Hi Frans,

Please find the code below enclosed between dashed lines.what is a code tag?

-------------------------------------------------
Code:
#!/usr/bin/ksh -p
 
 #set -v -x
 
# Source OpUtils environment
eval $(/opt/PROpUtils/default/bin/Prism)
 
# Source gmi environment
eval $(/opt/PRgmi/default/bin/Prism)
 
while getopts p:u:i:l:r:f:c:w:d: name
do
    case $name in
    p)    PROCESSNAME=${OPTARG};;
    u)    eval USERHOST=\$${OPTARG};;
    i)    eval USERID=\$${OPTARG};;
    l)    eval LOCALDIR=\$${OPTARG};;
    r)    eval REMOTEDIR=\$${OPTARG};;
    f)    eval FILE=\"\$${OPTARG}\"
          FILES="\"$FILE\" $FILES";;
    c)    eval COPYPREFIX=\$${OPTARG};;
    w)    eval WAITSECS=\$${OPTARG};;
    d)    eval DURATION=\$${OPTARG};;
    \?)   printf "Usage: %s: [-p process name] [-u user@hostname] [-i userid][-l localdir] [-r remotedir] [-f file(s)] [-c prefix locally] [-w retrywait secs] [-d durat
ion] \n" $0
          exit 2;;
    esac
done
shift $(($OPTIND - 1))
 
echo "Getting Password "
 

 
if [ -f ${LOCALDIR}/../locks/${PROCESSNAME}.lock ] ; then
if kill -0 $(<${LOCALDIR}/../locks/${PROCESSNAME}.lock); then
kill $(<${LOCALDIR}/../locks/${PROCESSNAME}.lock)
fi
fi
 
TMPFILE=/var/tmp/${PROCESSNAME}.tmp
print $$ >${LOCALDIR}/../locks/${PROCESSNAME}.lock
 
print "`date` - Started - Getting $FILES\nremote: $USERHOST:$REMOTEDIR\nlocal: $LOCALDIR\n";
 
set -A filesToGet $FILES
 
totalFiles=${#filesToGet[@]}
 
until [[ ${#filesToGet[@]} -eq 0 ]];do
 
if [ ${TIMEELAPSED} ]
then
   print "`date` - Sleep Time Elapsed: ${TIMEELAPSED}\n"
else
   integer TIMEELAPSED=0
fi
 
if [ $TIMEELAPSED -gt $DURATION ]
then
    print "There were still ${#filesToGet[@]} file(s) to get therefore exiting with exit status 1."
    exit 1
fi
 
curFileIndex=0
 
while [[ $curFileIndex -lt $totalFiles ]];do
 
file=${filesToGet[$curFileIndex]}
 
if [ $file ]
then
        echo In $file if condition
        if [ ! -f ${LOCALDIR}/../locks/${file}.lock ]; then
                print $$ >${LOCALDIR}/../locks/${file}.lock
        fi
 
        echo " $WGET_HOME/wget --no-check-certificate --user=${USERID} --password=${SERVER_PASSWORD} -O ${LOCALDIR}/${file} ${USERHOST}/${REMOTEDIR}/${file}"
$WGET_HOME/wget --no-check-certificate --user=${USERID} --password=${SERVER_PASSWORD} -O ${LOCALDIR}/${file} ${USERHOST}/${REMOTEDIR}/${file}
 
        EXIT_STATUS=$?
        echo "Exit status"$EXIT_STATUS
 
        if [[ $EXIT_STATUS -eq 1 ]]
        then
                echo "`date` - The file $file was not available."
        else
        if [ $EXIT_STATUS -ne 0 ]
        then
                echo "`date` - Wget failed and returned exit code: $EXIT_STATUS"
                exit 2
        else
 
                echo "`date` - Got file ${file}."
                unset filesToGet[$curFileIndex]
 
                if [ $COPYPREFIX ]
                then
                        echo "`date` - Now moving file ${file} to $COPYPREFIX$file"
                        mv ${LOCALDIR}/${file} ${LOCALDIR}/${COPYPREFIX}${file}
                fi
 
         fi
        fi
fi
 
        curFileIndex=$curFileIndex+1
 
        echo Removing lock file
        rm -f ${LOCALDIR}/../locks/${file}.lock
 
done
 
if [[ ${#filesToGet[@]} -ne 0 ]]
then
    if [ $WAITSECS ]
    then
       sleep $WAITSECS
       integer TIMEELAPSED=$TIMEELAPSED+$WAITSECS
    fi
fi
done
print "`date` - **** Finished ****"
 
rm -f ${LOCALDIR}/../locks/${PROCESSNAME}.lock
 
exit 0


--------------------------------------------------

edit by bakunin: after the explanation frans gave you I'm sure you bring your own code-tags to the party, won't you?

Last edited by bakunin; 04-05-2010 at 09:16 AM..
# 11  
Old 04-05-2010
For code tags :
  1. select the text you want to be displayed as code
  2. click on the '#' button in toolbar of the editor.
You could replace
Code:
f)    eval FILE=\"\$${OPTARG}\"
          FILES="\"$FILE\" $FILES";;

by the following statement
Code:
f) eval $FILES[${#FILES[@]}]=\"\\"$OPTARG\\"\"

and in the line
Code:
print "`date` - Started - Getting $FILES\nremote:  $USERHOST:$REMOTEDIR\nlocal: $LOCALDIR\n";
# replace $FILES by ${FILES[*]} (to treat $FILES as an array)

Are all the eval statements really necessary ? (for USERHOST, LOCALDIR ...)
couldn't you proceed as for the first parameter like
Code:
VARNAME=${OPTARG};;


Last edited by frans; 04-05-2010 at 09:18 AM.. Reason: question
# 12  
Old 04-05-2010
Quote:
Originally Posted by mohanpadamata
i am passing 3 variables(which contain filenames) to the script and storing these in a string called FILES. Here FILES is not an array.later i am assigning this string FILES to an array using set -A definition.
This won't work in any way: when you store the various elements in a string you have to separate them somehow so that you can later split the string into array elements. Since you said above that your filenames can contain blanks i am at a loss which delimiter character you want to use.

I hope this helps.

bakunin
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