I have several questions I want to understand in regards to error trap.
- why does the code below execute only 1 trap and not 2 and specifically only the code with "mydir2" and not "mydir1"?
- why at all is it executing the trap?
- why the command, if written as the marked code, will not generate a trap at all.
- what is the best / proper way to write this code.
why and how do I get the correct line number for the error command and not the line of the function FuncA?
#!/bin/bash set -o errtrace trap 'printerr' ERR function printerr(){ local lc="$BASH_COMMAND" rc=$? ln=${BASH_LINENO[$i]} echo "$(date +%s) : Command [ $lc ] exited with code [ $rc ] in line [ $ln ]" } function FuncA(){ [[ -d mydir1 ]] && echo mydir1OK [[ -d mydir2 ]] && echo mydir2OK #if [ -d /mtdir/ ] ;then # echo OKMYDIR #fi } FuncA
Result:
1457453672 : Command [ [[ -d mydir2 ]] ] exited with code [ 1 ] in line [ 13 ]