Outils pour utilisateurs

Outils du site


tuto:linux:traceback_shell

Traceback shell

Exemple :

root:~# ./traceback.sh gigix.sh
ok
id : option invalide -- 'x'
Saisissez « id --help » pour plus d'informations.

>>> Traceback (last called is first) :
==> id -x retun 1
source() in 2.sh:3
source() in gigix.sh:5
main() in ./traceback.sh:40

tracebask.sh
#!/bin/bash
 
#set -o pipefail  # trace ERR through pipes
#set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
set -o errexit   ## set -e : exit the script if any statement returns a non-true return value
set -o errtrace  # trace ERR through 'time command' and other functions
 
traceback() {
  local -i start=1
  local -i end=${#BASH_SOURCE[@]}
  local -i i=0
  local -i j=0
  local cmd="$1"
  local -i code="$2"
 
  echo -e "\n >>> Traceback (last called is first) :" 1>&2
  echo -e " ==> \e[93m${cmd}\033[0m \e[1mretun \e[91m${code}\033[0m"
  for ((i=${start}; i < ${end}; i++)); do
    j=$(( $i - 1 ))
    local function="${FUNCNAME[$i]}"
    local file="${BASH_SOURCE[$i]}"
    local line="${BASH_LINENO[$j]}"
    echo "     ${function}() in ${file}:${line}" 1>&2
  done
}
 
usage() {
  echo "Usage :"
  echo "  `basename $0` <shell script>"
  exit 1
}
 
#trap 'exit_handler "$?" "$LINENO" "${BASH_LINENO[*]}" "${BASH_SOURCE[*]}" "$BASH_COMMAND" "${FUNCNAME[*]:-empty}"' ERR
trap 'traceback "$BASH_COMMAND" "$?"' ERR
 
[ -f "$1" ] || usage
. $1
tuto/linux/traceback_shell.txt · Dernière modification: 2016/09/03 15:34 par root