Reuse Variable..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reuse Variable..
# 1  
Old 03-08-2010
Power Reuse Variable..

Hi.

I have these two variables:

Quote:
cv="$(print 'CV09')"
file_name="$(echo "CP99978_$cv.TXT")"
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.. Smilie

Thank you.
# 2  
Old 03-08-2010
Didn't get you completely.
if you want to reuse the variable in another variable having extra character,
use {} notation.

Code:
echo "CP99978_${cv}.TXT"

# 3  
Old 03-08-2010
MySQL Solution

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
Code:
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
Code:
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

# 4  
Old 03-08-2010
Thank you very much anchal khare and abubacker for your prompt response.

Really appreciate.

To make it clear.. This is my original objective:
Quote:
job_name="$(echo "load_didrnge.sh")"
table_name="$(echo "DIDRNGE")"
cv="$(echo "CV000")"
cv="$(print 'CV09')"
file_name=`CP99978_`$cv`.TXT`

file_name="$(awk '{print $1}' $cv )"
log=/tmw/oradata14/monthly_log/CP99978_$cv.log

sqlldr crispadm/admcrisp data=/tmw/oradata14/CP99978_CV001.TXT control=/home/oracle9/dba_area/adslmpr.ctl log=/tmw/oradata14/monthly_log/CP99978_CV001.TXT.log \
bad=/tmw/oradata14/monthly_log/CP99978_CV001.TXT.bad errors=5000000 skip=1
set cv="$(echo "CV001")"
/home/oracle9/dba_area/general/PROCEDURE_TRACKING_LOG.sql "$job_name" "$file_name" "$table_name" "$log"
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.. Smilie

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?
# 5  
Old 03-08-2010
you want to repeat the task with 3 cvs then you can use a loop too..

Code:
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:
Code:
cv="$(echo "CV000")"

is equivalent to
Code:
cv="CV000"

# 6  
Old 03-08-2010
Bug

Quote:
Originally Posted by anchal_khare
you want to repeat the task with 3 cvs then you can use a loop too..

Code:
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:
Code:
cv="$(echo "CV000")"

is equivalent to
Code:
cv="CV000"

Thank you so much!!

Finally, a great command generalization.. Smilie

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? Smilie
# 7  
Old 03-08-2010
Quote:
that $file_name variable won't take the latest value assigned to the $cv?
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!).

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



Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Reuse format strings

I have a collection of format strings for sscanf, such as "%02d%*1s%02d%*1s%02d" to read in certain formatted strings, such as dates, times, etc. I wonder if there is a way to use them in printf without some changes? The example above would not work - at least I can't think of any ways to... (4 Replies)
Discussion started by: migurus
4 Replies

2. Solaris

Reuse old/configured server for new purpose problems.

Greetings. First of all I consider myself a newbe in Linux, that's why I'm seeking your help so please be patient! I was given an old server (Solaris) that has Oracle (10.2.0) installed with a few databases. I'm supposed to reuse this server for this new application which will process data... (11 Replies)
Discussion started by: RedSpyder
11 Replies

3. Shell Programming and Scripting

How to reuse values in Shell script

Hi I am trying to write a shell script and wanted to re-use the value which I have entered already. Here is the sample code. echo "Enter Value : \c" ; read val echo "Enter number: $val\c" ; read num Now I wanted to change the value as showing in 2nd Line or if I will use enter, it... (15 Replies)
Discussion started by: SushilAnand
15 Replies

4. Emergency UNIX and Linux Support

Reuse a LUN

I have a LUN (From HP-Storage VA7110) that is claimed on 2 servers, but is in used in one of the VG on Server-1 . Now I want to shut Server-1 and re-use that LUN on server-2 . Server-1 Path-1 : /dev/rdsk/c4t0d1 Path-2: /dev/rdsk/c6t0d1 Server-2 Path-1: /dev/rdsk/c5t0d1 Path-2:... (8 Replies)
Discussion started by: Shirishlnx
8 Replies

5. Shell Programming and Scripting

How to declare variables once and reuse them in other scripts?

Hello everyone. I'm trying to create a conf file with variables that my other scripts will use. I have several scripts that use the same variables, and since I don't know how to read them from an external file, i define them in each script (and then if i want to change one's value i need to... (4 Replies)
Discussion started by: moshe88
4 Replies

6. UNIX for Dummies Questions & Answers

Reuse argument from current command?

I'm currently using zsh. Sometimes I find myself wishing to reuse an argument from earlier in the command. I know that I can, of course, assign an argument to a variable ahead of time, and then easily use that variable more than once. I know about the ability to reuse arguments from previous... (0 Replies)
Discussion started by: marshaul
0 Replies

7. Linux

suggest some ideas for reuse

hi can you all help me to develop anything in unix that could be reused. any module or application could be helpful (0 Replies)
Discussion started by: infyanurag
0 Replies

8. HP-UX

Reuse disk from other HP-UX

Hello, I have 2 hp-ux both running 11.23, I have move one of a harddisk from "UNIX A" to "UNIX B", so how can I read back the data in "UNIX B"? Thanks (5 Replies)
Discussion started by: zetadhell
5 Replies

9. UNIX for Advanced & Expert Users

Password reuse utility

Does anyone know of a password reuse utility for Solaris 7 or 8? Security people are telling me that I need one. Thanks (1 Reply)
Discussion started by: rtoba
1 Replies

10. UNIX for Dummies Questions & Answers

How to reuse same major number

Hi, I am working on device drivers.Once If register a device i'll get one major no. If i unregister and register again i'll get a different major no.What i have to do to get same major no. each time :( (0 Replies)
Discussion started by: Agnello
0 Replies
Login or Register to Ask a Question