This is based on another question of mine. I am trying to prevent executions of certain commands using a script. I got the script to work perfectly in interactive shells, but for noninteractive shells it doesn't prevent its execution.
/home/user/stop.sh (sourced in .bashrc)
#!/usr/bin/env bash
shopt -s extdebug; stop_cmd () {
[ -n "$COMP_LINE" ] && return # not needed for completion
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # not needed for prompt
local this_command=$BASH_COMMAND;
echo $this_command" Not Allowed";
return 1
};
trap 'stop_cmd' DEBUG
/home/user/temp.sh
#!/usr/bin/env bash
ls
I used the BASH_ENV variable suggested by @Inian to get my script into script files that use non-interactive shells.
In a new shell
#:export BASH_ENV=/home/user/stop.sh
#:ls
ls Not Allowed --> This is because of the source in .bashrc
#: --> Prompt appears. ls did not run
#:./temp.sh
./temp.sh: /usr/share/bashdb/bashdb-main.inc: No such file or directory
./temp.sh: warning: cannot start debugger; debugging mode disabled
ls Not Allowed --> Because of the $BASH_ENV
Directory contents displayed --> ls ended up running
#: --> Prompt appears after executing temp.sh
But this behavior is not displayed if I source stop.sh
within temp.sh
directly and it works like a charm.