You are not logged in.
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 testI 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 testand not:
log $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}") $LINENO this is a testevery 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
you can't.
use a(nother) function.
Last edited by N_BaH (2019-01-22 13:20:07)
Offline
Hi N_BaH, thanks for the reply.
What do you mean by a(nother) function?
all different - all equal
Offline
$(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?
Offline
Thanks seth, try that tomorrow morning.
all different - all equal
Offline
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 testThat'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
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
you might have to enable expand_aliases with shopt to use this in a script; as this is not bash's default behaviour.
Offline
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
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
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
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
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.
Offline
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
shopt -p expand_aliasesreturns
shopt -s expand_aliasesSo 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
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.
Offline
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
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' ]] && startxthen 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 i3So, 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
.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
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.
Offline
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