1837097 Members
2231 Online
110112 Solutions
New Discussion

variable substitution

 
SOLVED
Go to solution
Kris_5
Occasional Advisor

variable substitution

Hello Folks,

The following problem I am facing is very silly.

I am trying to get the value of variable
$script from a text file.

See below

$ script=$(grep "^#/script=" tcrontab2958.txt|cut -f2 -d=)
$
$ echo $script
$OC_APPL/xibs/prs_exec.sh


$ cat $script
cat: Cannot open $OC_APPL/xibs/prs_exec.sh: No such file or directory

------------
But both the variable $OC_APPL and file prs_exec.sh are present.
---------------

$ echo $OC_APPL
/opt/ORACLE/oclin/40/dev/aot
$ ll $OC_APPL/xibs/prs_exec.sh
-rwxr-x--- 1 opapps oclin 0 Dec 5 16:25 /opt/ORACLE/oclin/40/h

The question is why the variable $script is unable to substitute the variable $OC_APPL in its value.

TIA,

Kris
10 REPLIES 10
Mark Greene_1
Honored Contributor

Re: variable substitution

change:

script=$(grep "^#/script=" tcrontab2958.txt|cut -f2 -d=)

to:

script=`grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`

also check to see the working directory is appropriate for the file, or add the full pathname if you can.

--
mark
the future will be a lot like now, only later
maria paschali
Frequent Advisor

Re: variable substitution

Hi,

I don't know if you checked this, but see if your permissions and groups are correct.

It might not be able to see the file because you don't have read access to that file because it is owned by another user and or is in another group.

I hope this helps

Maria
Kris_5
Occasional Advisor

Re: variable substitution

Yes, I have privileges to see the file and the file is existing in the present directory. See the results of commands following the cat $script command. Thanks,
Mark Greene_1
Honored Contributor

Re: variable substitution

sorry, I missed this bit:

The question is why the variable $script is unable to substitute the variable $OC_APPL in its value

You need to use "eval" to get the shell to do the substitutio of $OC_APPL before it assigns it to $script.

--
mark
the future will be a lot like now, only later
Kris_5
Occasional Advisor

Re: variable substitution

Hi,

It is not working that way. See below. Thanks,

$ `grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`
sh: $OC_APPL/xibs/prs_exec.sh: not found.

$ eval `grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`

$ eval $(grep "^#/script=" tcrontab2958.txt|cut -f2 -d=)

$ script=$(eval `grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`)
$ echo $script


$ cmd=`grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`
$ script=`eval $cmd`
$ echo $script

Kris
Darrell Allen
Honored Contributor
Solution

Re: variable substitution

Hi Kris,

Instead of cat $script try:

cat `eval echo $script`

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Mark Greene_1
Honored Contributor

Re: variable substitution

Sorry for not being clearer, this line:

script=`grep "^#/script=" tcrontab2958.txt|cut -f2 -d=`

will extract the line from file tcrontab2958.txt that matches the grep. In reading your last response, am I correct in thinking that this line will contain the text OC_APPL? If so, then there is no way to get the shell to evaluate that, as it is just sees that as text.

If OC_APPL is a valid variable within the script that is doing the above grep, then you can put it together with the $script variable like:

script=$OC_APPL$script.

If this does not correctly address the situation, perhaps if you could post the line from the file you are grepping out, that would help.

--
mark
the future will be a lot like now, only later
Deepak Extross
Honored Contributor

Re: variable substitution

Would it be possible to modify your tcontab file to replace
"$OC_APPL/xibs/prs_exec.sh" with "${OC_APPL}/xibs/prs_exec.sh " ?
Steven Sim Kok Leong
Honored Contributor

Re: variable substitution

Hi Kris,

Use a combination of sed and xargs. I have tested it to work for your case. In the below testing, the variable OC_APPL contains the path of subdirectory a.

=====================================
$ echo $OC_APPL
a
$ echo $script
$OC_APPL/xibs/prs_exec.sh
$ echo $script|sed -e "s/\$OC_APPL/$(echo $OC_APPL)/g"|xargs ls
a/xibs/prs_exec.sh
$ echo $script|sed -e "s/\$OC_APPL/$(echo $OC_APPL)/g"|xargs cat
correct
=====================================

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Steven Sim Kok Leong
Honored Contributor

Re: variable substitution

Hi,

What Darrell suggested will also work. His method is much neater than mine.

=========================================
$ echo $script
$OC_APPL/xibs/prs_exec.sh
$ cat `eval echo $script`
correct
=========================================

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com