Bash preexecute
Posted
by
Alex_Bender
on Super User
See other posts from Super User
or by Alex_Bender
Published on 2013-10-27T15:34:19Z
Indexed on
2013/10/27
15:55 UTC
Read the original article
Hit count: 397
I'm trying to write bash command wrapper, which will be patch bash current command on the fly. But i'm faced with the problem. As i'm not a good Shell user, i can't write right expression of variable assignment in string. See bellow:
I'm set trap to preexecute, through this:
alex@bender:~$ trap "caller >/dev/null || xxx \"\${BASH_COMMAND}"\" DEBUG;
I want change variable BASH_COMMAND, do something like BASH_COMMAND=xxx ${BASH_COMMAND}
but i don't know, how i need escaping variables in this string
NOTE: xxx -- my custom function, which must return some value, if in end of command situated word teststr
function xxx(){
# find by grep, if teststr in the end
`echo "$1" | grep "teststr$" >/dev/null`;
# if true ==> do
if [ "$?" == "0" ]; then
# cut last 6 chars (len('teststr')==6)
var=`echo "$1" | sed 's/......$//'`;
echo "$var";
fi }
How can i do this stuff?:
alex@bender:~$ trap "caller >/dev/null || ${BASH_COMMAND}=`xxx $BASH_COMMAND`" DEBUG;
© Super User or respective owner