Reuse Variable..

Hi.

I have these two variables:

My objective here is to reuse that $file_name variable again and again by resetting the $cv value.

for example, if i reissue the cv="$(print 'CV01')" command, thus $file_name is now should be "CP99978_CV01.TXT", not "CP99978_CV01.TXT" anymore.

How I'm gonna do that? I've tried several method but it doesn't to work until I'm giving up.. :frowning:

Thank you.

Didn't get you completely.
if you want to reuse the variable in another variable having extra character,
use {} notation.

echo "CP99978_${cv}.TXT"

Do u mean if u change the value of the cv variable then u want the value of the filename also be changed automatically. with out giving this line

file_name="$(echo "CP99978_$cv.TXT")"

Correct me If im wrong?

---------- Post updated at 02:28 AM ---------- Previous update was at 02:27 AM ----------

If ur facing a reference problem then
I think you can solve your problem by referring the following snippet

function setToXXX() {
  echo changing value of $1
    eval "$1='$FOO'"
    }

    FOO=hello
    BAR=$FOO ;
    echo function, FOO is $FOO
    echo function, BAR is $BAR

    echo "FOO IS CHANGING" ;
    FOO="CHANGED VALUE" ;
    setToXXX BAR # you must call this function 

    echo after function, BAR is $BAR

Thank you very much anchal khare and abubacker for your prompt response.

Really appreciate.

To make it clear.. This is my original objective:

In other words, I'm gonna repeat that sqlldr command for another 3 CVs..

Since, the filename only distinctive by that CV.. That's why I don't want to issue that filename command again and again.

Thank you.

---------- Post updated at 03:37 PM ---------- Previous update was at 03:33 PM ----------

So, is that the only way to achive this i.e. by creating function?

I don't know it could be that difficult.. :smiley:

I thought I only need to change something so that the $filename would reflect the current value of $cv.

Why can't it anyway? Anyone?

you want to repeat the task with 3 cvs then you can use a loop too..

for CV in CV000 CV09
do
 file_name="CP99978_${CV}.TXT"  ## no need of command substitution here 
 ## do sqlldr part..or other stuffs.
done

just for info:

cv="$(echo "CV000")"

is equivalent to

cv="CV000" 

Thank you so much!!

Finally, a great command generalization.. :wink:

But anyway, I still curious, why if we set it normally, that $file_name variable won't take the latest value assigned to the $cv? :frowning:

This is correct. and the filename will take the latest value.
(only of those which are assigned before defining filename variable.)
if you assign a new value to CV after filename then filename is unaffected (clear though!).

cv=1
cv=2
file=a_$cv
cv=3

$file will still contain the a_2 ( cv=2).

shell reads the commands one by one.. top to bottom.

moreover, not sure which shell you are using.. but i can see lots of syntax errors and improvement in your code as par my knowledge.

job_name="$(echo "load_didrnge.sh")" ==> job_name="load_didrnge.sh"
table_name="$(echo "DIDRNGE")" ==> table_name="DIDRNGE"
cv="$(echo "CV000")" ==> cv="CV000"


cv="$(print 'CV09')" ==> cv="CV09"

# invalid commnad substitution. syntx err!.
file_name=`CP99978_`$cv`.TXT` ==> file_name=CP99978_${cv}.TXT 

# again same thing
file_name="$(awk '{print $1}' $cv )" ==> file_name="$(echo $cv | awk '{print $1}')" or file_name="$(awk '{print $1}' <<< $cv)" 

hope it helps.

This might have been covered above.
The original statement contains too many quotes and as others have pointed out unnecessary "print" and "echo" commands. Remember that quotes act in pairs, though in this case it has no adverse effect.

Can be simplified as:

cv="CV09"
file_name="CP99978_${cv}.TXT"

Now does this actually help:

In this test code we will try four values of ${cv}.

for cv in "2007" "2008" "2009" "2010"
do
          file_name="CP99978_${cv}.TXT"
          echo "${file_name}"
done

CP99978_2007.TXT
CP99978_2008.TXT
CP99978_2009.TXT
CP99978_2010.TXT

---------- Post updated at 01:31 ---------- Previous update was at 00:57 ----------

My attempt to rearrange the original post follows this quote from the O/P:

Some inspired guesswork here. Could be wrong. Untested. Check before use. Any likely typos from the original such as "CV09" and "crispadm/admcrisp" (not absolute path) are carried forward verbatim.
The idea is to illustrate parameter substitution in shell with no attempt to check the Oracle commands. I have attempted to get the line continuation characters correct "\" to make some sense of the Oracle commands. I now think that there are two commands.

FINAL FINAL ATTEMPT AT REARRANGING

job_name="load_didrnge.sh"
table_name="DIDRNGE"
for cv in "CV000" "CV001" "CV09"
do
    file_name="/tmw/oradata14/CP99978_${cv}.TXT"
    log="/tmw/oradata14/monthly_log/CP99978_${cv}.TXT.log"
    bad="/tmw/oradata14/monthly_log/CP99978_${cv}.TXT.bad"
    #
    sqlldr crispadm/admcrisp data="${file_name}" \
        control="/home/oracle9/dba_area/adslmpr.ctl" \
        log="${log}" \             
        bad="${bad}" \
        errors=5000000 skip=1
    #
    sqlplus /home/oracle9/dba_area/general/PROCEDURE_TRACKING_LOG.sql \
        "${job_name}" "${file_name}" "${table_name}" "${log}"
done

Guessed at sqlplus to run the second program.
Not happy that ${log} appears on both command lines, but without knowledge of the correct syntax or intentions we'll leave it to the author to decide.

1 Like