Re: bash variable expansion moment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



ken wrote, On 11/14/2009 07:37 PM:
> It's half a nice Saturday later and many attempts have brought no
> satisfaction.  Maybe this can't be done.
> 
> I'm trying to write a function which, when called from one function
> execute in another.  In itself, that's not the problem.  Rather, there's
> one built-in variable which is evaluated in the function definition and
> it's value is then set (too early).
> 
<SNIP>
> 
> I want the function Line to show the line number in the second file
> where it's executed, not the line number from the sourced function.
> 
> Any mavens got the skinny on this?

As I understand the variable is interpreted from the perspective of the line of the file, and bash 
does not inline the function.

A trick around it can be gotten with the following modification of your scripts.
---func-file----------------------
Line()
{
echo This is line "$MyLN" $@
}
#extra
#lines
#desired
#to
#show
#that
#execution
#not
#early, orig
#simply
#placed
#early
#in
#file
LineO()
{
echo This is line "$LINENO" $@
}
-------------------------

----main---------------------
#!/bin/bash

. ./func-file

MyLN=$LINENO Line ... it should be $LINENO
LineO ... it should be $LINENO
-------------------------

-- 
Todd Denniston
Crane Division, Naval Surface Warfare Center (NSWC Crane)
Harnessing the Power of Technology for the Warfighter
_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux