Find the answer to your Linux question:
Results 1 to 4 of 4
Hello - I have a bash script that uses "logger" to log messages. The way I am using it is: MYNAME=$0 MYPID=$$ LOGGER="/usr/bin/logger -t $MYNAME[$MYPID]($LINENO) -p daemon.error" ... echo 'some ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2

    how to delay variable expansion?

    Hello -

    I have a bash script that uses "logger" to log messages. The way I am using it is:
    MYNAME=$0
    MYPID=$$
    LOGGER="/usr/bin/logger -t $MYNAME[$MYPID]($LINENO) -p daemon.error"

    ...

    echo 'some stuff' | $LOGGER

    everything works properly, but the (built-in) $LINENO variable is expanded on the "LOGGER" line instead of the "echo" line.

    How would I cause the $LINENO expansion to be delayed, or protected, until the echo command?

    Thanks for your help!

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    I think changing LOGGER into a function should solve the problem:

    Code:
    #!/bin/bash
    MYNAME=$0
    MYPID=$$
    function LOGGER {
        /usr/bin/logger -t "$MYNAME[$MYPID]($1)" -p daemon.error $2
    }
    
    LOGGER $LINENO "some stuff"

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2
    Thanks secondmouse, yes, that would solve the problem, although it would require an extra parameter on every line I want to log. I was thinking along the lines that some quoting, bracketing, eval or other magic could delay the expansion of the LINENO var (or all vars) until $LOGGER is actually executed. I am actively searching for an answer, I will post a solution when I find one. In the meantime, if you or anyone has any more ideas...

    Thanks!

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Scandora,
    I am not sure it's worth trying to search for the exact solution that you wanted because if you do
    Code:
    echo 'some stuff' | $LOGGER
    the part echo 'some stuff' does not carry LINENO info that'll be passed to $LOGGER even the line itself implies LINENO

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...