Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitution question

Status
Not open for further replies.

ns705

Programmer
Feb 4, 2004
29
GB
Hi,

can anybody help me please?

I have the following code:
. . .
VARIABLE=sometext
. . .
var="abcd t1-$VARIABLE-t3 -eq 0 && xyz t1-t2-t3 -lt 8"
. . .
I need to have
newvar="t1-sometext-t3 -eq 0 && xyz t1-t2-t3 -lt 8"

I attempted to do it in the following way:
newvar=`echo $var | awk ' { m=split($0,Arr,"rcd") } END { print Arr[2] }'`
It works in command line but does not work in ksh script.
All my efforts to do it via eval were not successful.

Can you please help me with it?

Thanks in advance.

Anta

 
It works in command line but does not work in ksh script
???
Can you please post the ksh script ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Seems to work for me:
Code:
#!/usr/bin/ksh
VARIABLE=sometext 
var="abcd t1-$VARIABLE-t3 -eq 0 && xyz t1-t2-t3 -lt 8"
echo $var
Cheers, Neil
 
Thank you.

The script is too large (about 40K). It includes some its parts via ". other-scripts". It contains a lot of functions.
I'm very sorry - I can not post it here.
Those code I mentioned is in one of the internal functions.
This function is on one of lowest levels. It can run in batch (via &), too.

The function is:
statfunc () {
. . .
case $newch in
0 ) . . . ;;
1 ) >>> var is the mentioned variable
. . .
newvar=`echo $var | awk ' { m=split($0,Ar,"rcd") } END { print Ar[2] }'`
p1st=`echo $newvar | awk ' { n=split($0,Ar,"-") } END { print Ar[1] }'`
p2nd=`echo $newvar | awk ' { n=split($0,Ar,"-") } END { print Ar[2] }'`

filename=$dirc/block-$p1st-$p2nd

. . . ;;
* ) . . . ;;
esac
}

When I run the above commands as command line's one - I have that filename created in the correct way.
But when it's run as a part of script - I can see that $VARIABLE is not substituted with its value.

I'm sorry I can not provide enough info on my problem.
Maybe you could direct me to a right direction.

I attempted to solve the problem via eval or in other ways,
for example:
the below code does not substitute $VARIABLE value in script
newvar=`echo $var | sed "s/\$VARIABLE/$VARIABLE/g"`
but simple echo in the same script
echo $COND | sed "s/\$CTX/$CTX/g"
outputs info with the need substitution.

Thanks in advance.

Anta

 
You may try to export VARIABLE in the main script.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm sorry - nothing helps.

Anta
 
just a note:

giving 2 scripts: a.sh and b.sh, a calls b:

a.sh:
-----
VAR1="somthing"
b.sh <calls to b.sh>

b.sh:
----
echo $VAR1

if -in a.sh- you have the 2nd line:
./b.sh << it will open a new sh (or ksh), so the VAR1 is lost
. ./b.sh << it will use the same sh, so VAR1 remains.

just a though...
 
Thank you.
The problem is I can see that $VARIABLE value in echo cmd, but I have a feeling that shell (ksh) does not see it in the command:
newvar=`echo $var | awk ' { m=split($0,Ar,"rcd") } END { print Ar[2] }'`
where $var contains $VARIABLE.
It seems VARIABLE was not lost (echo outputs its value).
I can not make shell to see that variable in the mentioned command.

Anta
 
Not sure I understand the question but ...
Code:
VARIABLE=sometext
#---using double quotes 
var="abcd t1-$VARIABLE-t3 -eq 0 && xyz t1-t2-t3 -lt 8"
newvar=${var#* } #----chop off first word
echo newvar=$newvar
echo ok

#----using single quotes 
var='abcd t1-$VARIABLE-t3 -eq 0 && xyz t1-t2-t3 -lt 8'
newvar=${var#* }
echo newvar=$newvar
echo bad

#---fix with sed
echo $newvar|sed "s/\$VARIABLE/$VARIABLE/"|read newvar
echo newvar=$newvar
echo ok again
...gives...

newvar=t1-sometext-t3 -eq 0 && xyz t1-t2-t3 -lt 8
ok
newvar=t1-$VARIABLE-t3 -eq 0 && xyz t1-t2-t3 -lt 8
bad
newvar=t1-sometext-t3 -eq 0 && xyz t1-t2-t3 -lt 8
ok again
 
Thank you, Ygor.

I have attempted to act via sed in the same way.
I have seen the need value in echo cmd, but when I tried to use this value in further commands I have no seen it - only the variable name.

When I began to use some new variable:
eval VARIABLEnew\=$VARIABLE
and then use this new var in further text/string creation - everything was okay.
I'm not strong in ksh script creation, and to tell the truth I have done this (have "opened" that var value) but I can not say I have understood why it worked.

Thank you.

Anta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top