
이것은 학문적 연습에 가깝지만 대답이 궁금합니다. 현재 문제가 있는 코드는 다음과 같습니다.
export original_command_not_found_handler="$(type -f command_not_found_handler)" # original func as string
function command_not_found_handler(){ # my custom override
echo "my custom handler: $@"
echo "
${original_command_not_found_handler}
command_not_found_handler "$@"
" | bash
}
내가 원하는 것은 원래 zsh 함수를 내 사용자 정의 함수로 덮어쓴 다음 오버레이에서 원래 함수를 호출하는 것입니다.
이 코드에는 두 가지 문제가 있습니다.
- 서브셸에서 원래 함수를 호출하려고 하는데(bash로 파이핑) 서브셸보다는 현재 셸에서 호출하는 것이 더 좋습니다.
- 원래 함수가 문자열에서 필요한 대로 해석하지 않기 때문에 작동하지 않습니다.
답변1
에서는 zsh
함수와 해당 정의가 $functions
특수 연관 배열에 노출되므로 함수의 복사본을 만들려면 다음을 수행하면 됩니다.
functions[original_command_not_found_handler]=$functions[command_not_found_handler]
에서는 bash
다음을 수행할 수 있습니다.
eval "original_command_not_found_handle()
$(typeset -f command_not_found_handle | tail -n +2)"
typeset -f
(그러나 bash에는 일부 특수한 경우에 함수 정의가 올바르게 표시되지 못하게 하는 몇 가지 버그가 있다는 점에 유의하십시오 . 이론적으로는 zsh
s에서도 동일한 일이 발생할 수 있습니다 $functions
)
그런 다음 둘 다에서 다음을 수행합니다.
command_not_found_handler() {
echo my custom handler
if was handled by my custom handler; then
return "$some_ret_code"
fi
original_command_not_found_handler "$@"
}
(Bash에서는 handler
로 교체 handle
)
이제 이는 이 작업을 여러 번 수행할 수 없음을 의미합니다. 그렇지 않으면 매번 저장된 원래 핸들러에 대해 다른 이름을 선택해야 합니다. 대신 다음과 같이 원래 함수의 코드를 사용자 정의 함수에 포함할 수 있습니다.후속 조치라고 Q&A에 표시했습니다..