Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When add a .sh script into the $HOME/.profile.d/, I can't login on my Debian pc.

29 views
Skip to first unread message

Hongyi Zhao

unread,
Feb 19, 2017, 10:18:33 AM2/19/17
to
Hi all,

For preserving eternal bash history commands typed by myself, I created
the following script named as bash_eternal_history.sh which is located in
~/.profile.d. The content of ~/.profile.d/bash_eternal_history.sh is as
follows:

------------
#!/bin/bash
export history_dir=${HOME}
run_on_prompt_command()
{
local HISTTIMEFORMAT="[%F %T] "
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]

local date_command_part="${BASH_REMATCH[0]}"
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST_ME" ]
then
echo $$ $USER "$(history 1)" >> "$history_dir"/.bash_eternal_history
PERSISTENT_HISTORY_LAST_ME="$command_part"
fi
}
export -f run_on_prompt_command
export PROMPT_COMMAND="run_on_prompt_command"
bhgrep() { awk 'sub(/([^ ]+ +){5}/,"") && !a[$0]++'
"$history_dir"/.bash_eternal_history |grep -E --color "$@" ; }
export -f bhgrep
------------


Then I appened the following contents to ~/.profile:

----------
if [ -d $HOME/.profile.d ]; then
for i in $HOME/.profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
----------


Now I logout and re-login, my pc just gets stuck at login prompt -
showing it over and over again.

Why does this happen? How to solve this issue?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Thomas 'PointedEars' Lahn

unread,
Mar 7, 2017, 7:52:14 PM3/7/17
to
Hongyi Zhao wrote:

> For preserving eternal bash history commands typed by myself, I created
> the following script named as bash_eternal_history.sh which is located in
> ~/.profile.d. The content of ~/.profile.d/bash_eternal_history.sh is as
> follows:
>
> ------------
> #!/bin/bash
> […]
> export PROMPT_COMMAND="run_on_prompt_command"
>
> Then I appened the following contents to ~/.profile:
>
> ----------
> if [ -d $HOME/.profile.d ]; then
> for i in $HOME/.profile.d/*.sh; do
> if [ -r $i ]; then
> . $i

Should be

if [ -r "$i" ]; then
. "$i"

Always quote variable references, unless you know what you are doing.

> fi

Because there is no “else” branch, this can be simplified to

[ -r "$i" ] && . "$i"

But if “[ -r "$i" ]” exited with failure, “$?” would indicate failure, while
with the “if” statement if would indicate success (as “no condition tested
true”). This could be important if subsequent statements evaluated “$?”.

> done
> unset i
> fi
> ----------
>
>
> Now I logout and re-login, my pc just gets stuck at login prompt -
> showing it over and over again.
>
> Why does this happen? How to solve this issue?

I am not sure about either. Maybe it has to do with the fact that you are
executing a command in the value of $PROMPT_COMMAND directly; this could
cause the shell go into infinite recursion. I have never seen or attempted
to do that; all invocations there so far have been in a subshell, e.g.

export PROMPT_COMMAND='…; [ -n "$(which git-root)" ] && { export
GIT_ROOT=$(git root 2>/dev/null); export GIT_ROOT_RELATIVE=$(git root --
relative 2>/dev/null) };'

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

Lew Pitcher

unread,
Mar 8, 2017, 12:14:20 PM3/8/17
to
On Tuesday March 7 2017 19:52, in comp.unix.shell, "Thomas 'PointedEars' Lahn"
<Point...@web.de> wrote:

[Apologies for posting my reply to PointedEars posting instead of OP's
posting; OP's post is no longer available on my server, and I just had a
thought about the question below]
It sounds to me like one of the .sh scripts in $HOME/.profile.d executes an
'exit' or 'return' builtin. If so, then, because the script is being
"sourced", it would execute the command in the context of the login script (I
believe that bash "sources" the ~/.profile script), and exit the shell. With
this termination of the user's root shell, the inittab (or OP's system
equivalent) would respawn a getty (or equivalent) which would then prompt for
login. And so the cycle would repeat.

OP: Log in using another (preferrably priviledged) account, and check the
scripts in the original account's $HOME/.profile.d. Remove any exit or return
statements in those scripts and try the login again.

HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request


Hongyi Zhao

unread,
Jun 19, 2017, 8:16:45 PM6/19/17
to
On Wed, 08 Mar 2017 01:52:09 +0100, Thomas 'PointedEars' Lahn wrote:

> export PROMPT_COMMAND='…;

Do you have redefined your PROMPT_COMMAND? IMO, This is a tricky thing.
So, what's your PROMPT_COMMAND used for now?

Regards

> [ -n "$(which git-root)" ] && { export
> GIT_ROOT=$(git root 2>/dev/null); export GIT_ROOT_RELATIVE=$(git root --
> relative 2>/dev/null) };'





--
0 new messages