1

I don't know the exact word, that's why I'll use "decorator", like in POO, but I'm talking about the kind of functions you can use before other functions to add any operations to it, like the command time which adds the execution time at the end of the command.

$ time echo "test"
test

real    0m0.065s
user    0m0.000s
sys     0m0.000s

I'd like to write a command that can be used as a decorator for other commands, but I don't know how to do that. More exactly, if the second script fails, I don't want a reference to the decorator like. For example, when the second script doesn't exist, what I have for now is:

$ mydecorator ./ascript
mydecorator: line X: ./ascript: permission denied

when I want to have:

$ mydecorator ./ascript
bash: line X: ./ascript: permission denied

I just wrote my script and called eval on all arguments afterwards, but I think there's more to know. Even tried eval $SHELL $*, but it does not work like I'd like. Is it possible at all to write a decorator (in bash 2.05b, not my choice)? Is it possible to have multiple decorators for one launched command?

Oh and if someone could give me the real name of those commands that I can use instead of "decorator".

Thanks.

EDIT: I tried using Bash Function Decorator method, but it simply doesn't work.

decorator.sh:

decorate() {
  eval "
    _inner_$(typeset -f "$1")
    $1"'() {
      echo >&2 "Calling function '"$1"' with $# arguments"
      _inner_'"$1"' "$@"
      local ret=$?
      echo >&2 "Function '"$1"' returned with exit status $ret"
      return "$ret"
    }'
}

decorate $1
$*

Result:

$ ./decorator.sh touch test
./decorator.sh: line 2: _inner_: command not found
Calling function touch with 1 arguments
./decorator.sh: line 5: _inner_touch: command not found
Function touch returned with exit status 127

And, as hinted by exit status 127, the touch command is not successful.

Sylordis
  • 142

0 Answers0