You are not logged in.

#1 2019-01-22 12:01:19

Yann
Member
Registered: 2017-11-23
Posts: 235

[SOLVED] command with dynamics variables in a variable

Hi everyone,

I wrote a function being a personal logger:

function log { echo -e "$(date +%H:%M:%S) "${@:2}"" >> $HOME/$USER.log # just an example ; }

I would like to display the file and the line of the script calling this function.

in .bash_profile:
11 ...
12 log this is a test
13 ...

will output:

18:13:12 .bash_profile 12: this is a test

I find out $LINENO to get the current line so I can do in .bash_profile:

11 ...
12 log $LINENO this is a test
13 ...

And I got:

$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")

to get the current file path

I would like to write:

log $CURRENT_SCRIPT_PATH $LINENO this is a test

and not:

log $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}") $LINENO this is a test

every time I need to call this function.

(I did not find a way to get the line and the current file from the called function, I guess this is impossible)

I tried during 3 hours all the $() "" '' "''" "``" ${!}, global aliases (not working) etc.

How can I have a variable owning a command having variables that can change according to the current situation.
CURRENT_SCRIPT_PATH=$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")

I am sure someone can solve my problem in 10 seconds,

Thanks for your time

Last edited by Yann (2019-01-26 13:20:25)


all different - all equal

Offline

#2 2019-01-22 13:19:57

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: [SOLVED] command with dynamics variables in a variable

you can't.
use a(nother) function.

Last edited by N_BaH (2019-01-22 13:20:07)

Offline

#3 2019-01-22 13:35:56

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Hi N_BaH, thanks for the reply.
What do you mean by a(nother) function?


all different - all equal

Offline

#4 2019-01-22 17:32:58

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,674

Re: [SOLVED] command with dynamics variables in a variable

$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")

wtf is this supposed to be?

realpath ${BASH_SOURCE[0]}

??

If you want to shortcut this, use a function (or add it to your log function?) but actually ${BASH_SOURCE[0]} should be pretty much what you want anyway?

Online

#5 2019-01-22 20:34:40

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Thanks seth, try that tomorrow morning.


all different - all equal

Offline

#6 2019-01-23 11:27:44

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Tried ..

Indeed

$(realpath ${BASH_SOURCE[0]})

seems to do the same thing than

$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")

Thanks ..

However, if I put it in my log function in my .functions file, it returns /home/xxx/.functions files every time I call it.. This is the problem.
I don't see any other solution than use $(realpath ${BASH_SOURCE[0]}) when I am calling the function from the file such as:

log $(realpath ${BASH_SOURCE[0]}) $LINENO this is a test

That's okay, but if I could avoid it and have it in the functions would be better. What do you think?


all different - all equal

Offline

#7 2019-01-23 13:12:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,531
Website

Re: [SOLVED] command with dynamics variables in a variable

Use an alias:

function _log { echo -e "$(date +%H:%M:%S) "${@:2}"" >> $HOME/$USER.log }
alias log='_log $(realpath ${BASH_SOURCE[0]}) $LINENO'

Last edited by Trilby (2019-01-23 13:14:03)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2019-01-23 14:05:55

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: [SOLVED] command with dynamics variables in a variable

you might have to enable expand_aliases with shopt to use this in a script; as this is not bash's default behaviour.

Offline

#9 2019-01-23 14:17:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,531
Website

Re: [SOLVED] command with dynamics variables in a variable

N_BaH wrote:

you might have to enable expand_aliases with shopt to use this in a script; as this is not bash's default behaviour.

What?

expand_aliases affects tab completion in interactive sessions.  What does that have to do with using an alias in a script, and what exactly is not a default behavior?

EDIT:  On another note, a single alias will do, and even with no bashisms:

alias log='printf "%s %s %d: " "$(date +%H:%M:%S)" "$(readlink -f $0)" "$LINENO"; echo'

I don't believe LINENO is a POSIX specification, but it's not a 'bashism' as it is available in a much wider range of shells than things like BASH_SOURCE.

Last edited by Trilby (2019-01-23 14:30:40)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#10 2019-01-23 14:30:04

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: [SOLVED] command with dynamics variables in a variable

alias expansion is not enabled inside scripts.

Aliases are not expanded when the shell is not interactive, unless[...]

doesn't this log stuff has to go in a script ?

Offline

#11 2019-01-23 14:34:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,531
Website

Re: [SOLVED] command with dynamics variables in a variable

I stand corrected.  It seems bash doesn't expand aliases at all in scripts (even if it's defined in that same script) - that's entirely new to me and a bit disturbing as BASH is generally a superset of POSIX shell features, but in this case it entirely violates that basic behavior.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#12 2019-01-23 14:35:57

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Hi Trilby,
thanks, this is a good solution. However, I mostly use this logger for my login files. From ".bash_profile" I call ".login" that call ".login_env", and others, I customized a lot my personal system files. I declared and export some global functions (such as my logger) in my .bash_functions.global but did not succeed to create "global" aliases that the sub shells will inherit. Is there any way to have global aliases, any export alias command, etc?
Thanks


all different - all equal

Offline

#13 2019-01-23 14:47:27

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,674

Re: [SOLVED] command with dynamics variables in a variable

Did you see N_BaH's comment about shopt'ing in alias expansion?
To see what aliases are defined in your target script, simply have it run "alias" and watch the output.
Since you want to display the currently logging script and not the inherited source, aliases are the proper approach.

Online

#14 2019-01-23 15:09:12

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Sorry, I did not refresh and mist several messages. Yes, the expand_aliases solution would be great.
I added:

shopt -s expand_aliases
alias test='echo coucou'

in one of my login script but that doesn't work. This alias is not known in any shell. Did I miss something?


all different - all equal

Offline

#15 2019-01-23 15:16:10

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

shopt -p expand_aliases

returns

shopt -s expand_aliases

So it seems that expand_aliases is enable on my system. Howerver if I run:

alias test='echo coucou'

and then "bash", the subshell doesn't know it. What do I miss?
Thanks for your time.


all different - all equal

Offline

#16 2019-01-23 16:37:11

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,674

Re: [SOLVED] command with dynamics variables in a variable

It's not inherited by subshells - you still have to source the definition somehow.
I suggest you post a set of real life files on your approach.

Online

#17 2019-01-23 17:03:21

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,599

Re: [SOLVED] command with dynamics variables in a variable

Fully agree with seth here + you get the benefit more people are able to help, because is's hard, at least for me, to tell where this going...

Offline

#18 2019-01-23 20:47:10

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Ok.

This is my .bash_profile summarized:

#!/bin/sh

LOGIN_ENV_PATH=$HOME/.login_env # owns mostly export VARIABLES
source $LOGIN_ENV_PATH

shopt -s expand_aliases
alias test='echo coucou'

test # print coucou


# start x.org
[[ $(tty) == '/dev/tty1' ]] && startx

then my .xinitrc (called by startx):

# unlock numluck key
numlockx &

#xhost display
xhost +

# set the dns servers to the /etc/resolv.conf file
#use resolvconf to copy the content of resolv.conf.head to resolv.conf
sudo resolvconf -u

COMPTON_CONF_PATH=$HOME/.config/compton.conf
compton --config $COMPTON_CONF_PATH &

xrandr --output $MONITOR_DEVICE --auto --output $MONITOR_EXT --auto --right-of $MONITOR_DEVICE

test # return command not known

exec i3

So, I would like to define a global alias, so the shopt -s expand_aliases seems good. But this doesn't work as I expected. The test alias works in .bash_profile but when it calls another command such as startx, this sub-shell (is it correct?) doesn't owns the test alias.

Is it more clear?

Thansk for your time.


all different - all equal

Offline

#19 2019-01-23 20:59:21

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: [SOLVED] command with dynamics variables in a variable

.bash_profile defines current [interactive]* shell's environment.

.xinitrc is another environment that is related to no shell environment.

expand_aliases is useless in .bash_profile as it is "sourced" in interactive shell.

as it's been said already, you have to have the whole file where alias is defined (is not really needed/useful ?), or rewrite it's definition in every script you want it to be called, or have it in a separate file and source it into each script...

*edit

Last edited by N_BaH (2019-01-23 22:06:14)

Offline

#20 2019-01-23 21:10:13

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,674

Re: [SOLVED] command with dynamics variables in a variable

In an interactive login shell, that is.

@Yann, "man bash" see the INVOCATION section.
In a nutshell, nothing is sourced by default by a non-interactive shell (ie. whenever bash is used to execute a script) - bashrc and bash_profile are only for interactive shells (to make the users life easier - or somethimes harder…)

You will have to explicitly source whatever you want to see in your script, ie. "source $LOGIN_ENV_PATH" in this case belongs into your xinitrc.

Online

#21 2019-01-26 13:19:49

Yann
Member
Registered: 2017-11-23
Posts: 235

Re: [SOLVED] command with dynamics variables in a variable

Sorry for the late answer. I took a time to read more about bash logical. It's clearer now, I did not know well these working particularities (login, interactive, etc.).
I am going to reconsider the logical of my logger then. Knowing more that I can or can not do now I consider this thread as solved.
Thanks a lot for your time !


all different - all equal

Offline

Board footer

Powered by FluxBB