I have to avoid the execution of a certain command in a bash script.
I thought to use a preexec trap to do so.
Let's say I want to avoid the command 'source' just for axample. What I did is basically the following:
#!/bin/bash
function preexec ()
{
if test $( echo "$BASH_COMMAND" | cut -d " " -f1 ) == "source"
then
echo ">>> do not execute this"
else
echo ">>> execute this"
fi
}
trap 'preexec' DEBUG
echo "start"
source "source.sh"
echo "go on"
exit 0
the idea works fine, but at this point I don't know how to avoid the execution of said command.
Any idea how to solve this?
One workaround would be to define an alias for that command that does nothing and undefine it after the script has completed. The alias must be declared within the script itself for this to work:
alias source=:
## The actual script source here...
unalias source
Redefine the source
command by using a function called source
.
Functions can be exported.
source() { builtin source /dev/null; return 0; }
source() { read < /dev/null; return 0; }
source() { :; return 0; }
export -f source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With