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).
Here's the one file (func-file): ------------------------- Line() { echo This is line "$LINENO" $@ } -------------------------
That one is called by this one: ------------------------- #!/bin/bash
. ./func-file
Line ... it should be $LINENO ------------------------
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?
tia
You could pass the value of $LINENO to Line() as a function argument:
Here's the one file (func-file): ------------------------- Line() { echo This is line $@ } -------------------------
That one is called by this one: ------------------------- #!/bin/bash
. ./func-file
Line $LINENO ------------------------
Macintosh-5:/tmp joshuagimer$ cat func-file Line() { echo This is line $@ } Macintosh-5:/tmp joshuagimer$ cat test.sh #!/bin/bash
. ./func-file
Line $LINENO Macintosh-5:/tmp joshuagimer$ bash test.sh This is line 5
Thanks Josh
On Sat, Nov 14, 2009 at 5:37 PM, ken gebser@mousecar.com wrote:
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).
Here's the one file (func-file):
Line() { echo This is line "$LINENO" $@ }
That one is called by this one:
#!/bin/bash
. ./func-file
Line ... it should be $LINENO
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?
tia
-- War is a failure of the imagination. --William Blake
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Joshua,
Thanks for the reply. But while what you offer would indeed work, but it's not what I was looking for.
I simplified my example for clarity, was looking to put other text and variables in Line(), this in order to greatly simplify the code which calls it.
To rephrase what I'm trying to do:
A function containing environmental variables in one file would be called in another file. The function would, then, pass (e.g.) $LINENO as if it were a literal, but in the line where $Line is invoked it would be evaluated and the value output.
As said, I'm not sure this can be done at all. My only grounds for hope is that bash already does so much so well... I'm constantly amazed at what can be done with it. So maybe this is too.
Thanks again for the response.
On 11/15/2009 06:36 AM Joshua Gimer wrote:
You could pass the value of $LINENO to Line() as a function argument:
Here's the one file (func-file):
Line() { echo This is line $@ }
That one is called by this one:
#!/bin/bash
. ./func-file
Line $LINENO
Macintosh-5:/tmp joshuagimer$ cat func-file Line() { echo This is line $@ } Macintosh-5:/tmp joshuagimer$ cat test.sh #!/bin/bash
. ./func-file
Line $LINENO Macintosh-5:/tmp joshuagimer$ bash test.sh This is line 5
Thanks Josh
On Sat, Nov 14, 2009 at 5:37 PM, ken <gebser@mousecar.com mailto:gebser@mousecar.com> wrote:
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). Here's the one file (func-file): ------------------------- Line() { echo This is line "$LINENO" $@ } ------------------------- That one is called by this one: ------------------------- #!/bin/bash . ./func-file Line ... it should be $LINENO ------------------------ 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? tia -- War is a failure of the imagination. --William Blake _______________________________________________ CentOS mailing list CentOS@centos.org <mailto:CentOS@centos.org> http://lists.centos.org/mailman/listinfo/centos
-- Thx Joshua Gimer
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
On Sun, Nov 15, 2009 at 1:23 PM, ken gebser@mousecar.com wrote:
Joshua,
Thanks for the reply. But while what you offer would indeed work, but it's not what I was looking for.
I simplified my example for clarity, was looking to put other text and variables in Line(), this in order to greatly simplify the code which calls it.
To rephrase what I'm trying to do:
A function containing environmental variables in one file would be called in another file. The function would, then, pass (e.g.) $LINENO as if it were a literal, but in the line where $Line is invoked it would be evaluated and the value output.
As said, I'm not sure this can be done at all. My only grounds for hope is that bash already does so much so well... I'm constantly amazed at what can be done with it. So maybe this is too.
Thanks again for the response.
Hey
I am sorry to point out that unfortunately on this list top posting is not considered to be good practice. Please would you keep this in mind for further posts. I hope this notice does not deter you form posting further. It is just meant as advice as many people complain about this issue.
Hope you have a nice day.
Cheers Didi
On Sun, Nov 15, 2009 at 08:23:59AM -0500, ken wrote:
A function containing environmental variables in one file would be called in another file. The function would, then, pass (e.g.) $LINENO as if it were a literal, but in the line where $Line is invoked it would be evaluated and the value output.
I'm not quite sure what you're saying. Typically variables are not expanded at 'parse' time, but at run time.
eg
x=1
function foo { echo $x }
x=100 foo
You can see that "$x" is not expanded when it's first met (in the definition of foo) but when foo is run.
$LINENO is a special internal variable that points to the current running line, and so will change. It's not being evaluated when the function is compiled, but when you _run_ the function $LINENO will point to the line inside the function. That's what it is there for. Do not use this variable inside your program for anything other than debugging purposes.
Are you trying to do something like this?
function bar { typeset var=$1 eval typeset val='$'$var echo "$var=$val" }
x=10 y=100 bar x bar y
Output of this is x=10 y=100
Or are you trying to do debugging like this:
trap 'echo At line $LINENO, x=$x' DEBUG x=10 x=20 echo Some output x=30 trap '' DEBUG # Stop tracking what X is set to x=40 x=50
Here you'll see the DEBUG trap is called just before the command is executed so you'll see things like At line 2, x= At line 3, x=10 At line 4, x=20 Some output At line 5, x=20 At line 6, x=30
On 11/15/2009 08:54 AM Stephen Harris wrote:
On Sun, Nov 15, 2009 at 08:23:59AM -0500, ken wrote:
A function containing environmental variables in one file would be called in another file. The function would, then, pass (e.g.) $LINENO as if it were a literal, but in the line where $Line is invoked it would be evaluated and the value output.
I'm not quite sure what you're saying. Typically variables are not expanded at 'parse' time, but at run time.
....
From the original post (somewhere got edited out):
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).
Here's the one file (func-file): ------------------------- Line() { echo This is line "$LINENO" $@ } -------------------------
That one is called by this one: ------------------------- #!/bin/bash
. ./func-file
Line ... it should be $LINENO ------------------------
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.
=====================================
The problem is that $LINENO is evaluated in the function definition, and not when called. So I'm thinking to change "$LINENO" in the function definition to some other syntax so that it won't be evaluated until called.
tnx
On Sun, Nov 15, 2009 at 01:50:30PM -0500, ken wrote:
The problem is that $LINENO is evaluated in the function definition, and not when called. So I'm thinking to change "$LINENO" in the function
No it's not. Variables are _not_ evaluated when the function is defined; they're evaluated at execution. otherwise simple things like
y=1 x() { echo $y } y=10 x would not work.
You can show this, simply, with "typeset -f" eg x() { echo Line is $LINENO } typeset -f
The result is x () { echo Line is $LINENO }
Note that the variable has _not_ been evaluated; it's still there in the function definition.
Your problem is that LINENO is a special variable that changes value as the program executes.
As per the manpage: LINENO Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function.
it's clear that when you enter a function it is reset to 1. Thus: function y { echo in y, LINENO=$LINENO } echo LINENO=$LINENO y results in LINENO=5 in y, LINENO=3 because the echo statement is on the third line of the function.
You can not use $LINENO inside a function and expect it to represent the line where the function is called.
So, again, reading the manpage, you need to look at the array BASH_LINENO; in particular BASH_LINENO[0] is the line number of the line that calls your function.
So... function x { echo We were called from line ${BASH_LINENO[0]} }
echo LINENO=$LINENO before the call x echo LINENO=$LINENO after the call may result in LINENO=6 before the call We were called from line 7 LINENO=8 after the call
Is this what you wanted to do?
On 11/15/2009 02:22 PM Stephen Harris wrote:
On Sun, Nov 15, 2009 at 01:50:30PM -0500, ken wrote:
The problem is that $LINENO is evaluated in the function definition, and not when called. So I'm thinking to change "$LINENO" in the function
No it's not. Variables are _not_ evaluated when the function is defined; they're evaluated at execution. ....
See my example below.
Is this what you wanted to do?
Stephen, thanks for your reply, but you're not seeing what I want to do. Let me post my example once again:
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).
Here's the one file (func-file) with the function definition: ------------------------- Line() { echo This is line "$LINENO" $@ } -------------------------
That one is called by this one below: ------------------------- #!/bin/bash
. ./func-file
Line ... it should be $LINENO ------------------------
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.
I want the output to be:
This is line 5 ... it should be 5
but it's not. The num output in "This is line [num]" is whatever the line number is in the function definition. (I.e., $LINENO is evaluated in the function. Try it if you don't believe me.)
What I'm looking for is the proper syntax to wrap around $LINENO in the function definition (in func-file) so that it's not evaluated there but is evaluated when the function is called in the second file.
On Sun, Nov 15, 2009 at 06:21:40PM -0500, ken wrote:
Is this what you wanted to do?
Stephen, thanks for your reply, but you're not seeing what I want to do. Let me post my example once again:
You're not reading what I wrote.
Line() { echo This is line "$LINENO" $@
As I said in my previous mail, use ${BASH_LINENO[0]} instead, which tells you the line it was called from.
Thus:
$ cat func-file Line() { echo This is line "${BASH_LINENO[0]}" $@ }
$ cat x #!/bin/bash
. ./func-file
Line ... it should be $LINENO
$ ./x This is line 5 ... it should be 5
but it's not. The num output in "This is line [num]" is whatever the line number is in the function definition. (I.e., $LINENO is evaluated in the function. Try it if you don't believe me.)
I explained to you _exactly_ why you are seeing the behaviour you are seeing. Your interpretation of what is happening is wrong. $LINENO gets _reset_ when you enter a function.
What I'm looking for is the proper syntax to wrap around $LINENO in the
You can _not_ use $LINENO in this manner. You _must_ use the BASH_LINENO array. That's what it was created for.
function definition (in func-file) so that it's not evaluated there but is evaluated when the function is called in the second file.
You clearly don't understand how scripts are evaluated. I've given you the answer; I've proven it with output from the shell; I've referenced the documentation; I've explained how this works.
I even told you the solution and what variable you should use.
I give up.
On 16-Nov-2009 ken wrote:
On 11/15/2009 06:32 PM Stephen Harris wrote:
On Sun, Nov 15, 2009 at 06:21:40PM -0500, ken wrote:
....
echo This is line "${BASH_LINENO[0]}" $@
,,,,
That's all I needed. Thanks.
You might also want to check out bash's built in `caller` command.
-Philip
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 -------------------------