I want to know if a script has been launched "secretly" or "anonymously" or "without leaving trace", aka. with a space before. In such event, the script is not added to the history.
./my_script.sh
vs
./my_script.sh (there is a space before the script)
I came with the following solution:
if [ $0 = `history | tail -n 1 | cut -d ' ' -f 3` ]; then
echo "No hidden launch (no space before script)"
else
echo "Hidden launch (space before script)"
fi
It's working except if the script is launched twice sequentially. Any better idea?
It's assumed that the option HISTCONTROL=ignoreboth is in bashrc. Even with or without such option, the question remains.
I want to know if a script has been launched "secretly" or "anonymously" or "without leaving trace", aka. with a space before. In such event, the script is not added to the history.
./my_script.sh
vs
./my_script.sh (there is a space before the script)
I came with the following solution:
if [ $0 = `history | tail -n 1 | cut -d ' ' -f 3` ]; then
echo "No hidden launch (no space before script)"
else
echo "Hidden launch (space before script)"
fi
It's working except if the script is launched twice sequentially. Any better idea?
It's assumed that the option HISTCONTROL=ignoreboth is in bashrc. Even with or without such option, the question remains.
Share Improve this question edited Mar 19 at 23:07 gregoiregentil asked Mar 19 at 19:07 gregoiregentilgregoiregentil 1,9273 gold badges31 silver badges64 bronze badges 9 | Show 4 more comments1 Answer
Reset to default 0This looks like a variant of this question:
https://superuser/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command
A bash function that run prior to execution might be able to detect the extra space and then log the command or take another action.
history
is available only in interactive shells? – Arkadiusz Drabczyk Commented Mar 19 at 19:55find ... -exec ...
) it won't be in the bash history. Even if it was run from bash, it won't be in the history if it was run via an alias or function, or within a script (where history is disabled by default). It can also fail if it was run as part of a complex command likeothercommand; ./my_script.sh
. – Gordon Davisson Commented Mar 19 at 23:52