Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否可以像我在标题中所说的那样使以下 bash 脚本工作?
#!/bin/bash echo_report() { echo "echo on line $1" } trap 'echo_report $LINENO' [sigspec] #same code here echo "hi" #more code here
我不知道我应该用什么[sigspec]...
[sigspec]
如果trap无法使用,我还有哪些其他选择?
trap
包装echo在一个函数中,然后用于caller显示行号:
echo
caller
#!/bin/bash echo() { caller command echo "$@" } echo "hi"
结果:
$ bash foo.bash 8 foo.bash hi