Pergunta

Eu tenho procurado em todos os lugares e eu venho acreditar que não há como fazer isso além de ter variáveis globais, mas eu acredito que o guru's em stackoverflow.com pode ser capaz de me ajudar:

Existe alguma maneira de bash para prender uma função passando argumentos para ele? Por exemplo, trap <function_name> <arg_1> <arg_2> SIGINT?

Foi útil?

Solução

trap lets you specify an arbitrary command (or sequence of commands), but you have to pass that command as a single argument. For example, this:

trap 'foo bar baz | bip && fred barney ; wilma' SIGINT

will run this:

foo bar baz | bip && fred barney ; wilma

whenever the shell receives SIGINT. In your case, it sounds like you want:

trap '<function> <arg_1> <arg_2>' SIGINT

Outras dicas

Maybe I'm misunderstanding you, but ... this is legal:

trap "cp /etc/passwd $HOME/p" SIGINT
trap 'cp /etc/passwd /tmp/p; echo wooo hoo' SIGINT

I'm not sure I understand correctly what you mean, but if you want to make a signal handler call a function and pass it parameters, trap "function arg1 arg2" SIGNAL should work. For example trap "ls -lh /" INT will cause Ctrl+C in your shell to result in ls -lh / (program with 2 args) being called.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top