Trap-Funktion durch Übergeben von Argumenten?
Frage
Ich habe überall gesucht, und ich bin gekommen, um zu glauben, dass es keinen Weg gibt, das andere als globale Variablen zu tun, aber ich glaube, dass der Guru in Stackoverflow.com mir möglicherweise helfen kann:
Gibt es in Bash irgendwie, um eine Funktion zu fangen, indem Sie dazu bringt?
Zum Beispiel trap <function_name> <arg_1> <arg_2> SIGINT
?
Lösung
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
Andere Tipps
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.