debug en bash

  • en el script, modificando el SheBang:
    #!/bin/bash -x
  • desde linea de comando: $ bash -x <script>
  • activar modos:
    • set (+/-)x : muestra comandos y argumentos a medida que son ejecutados
    • set (+/-)v : «shell inputs lines as they are read» ?

podemos usar una función para hacer las llamadas a un modo DEBUG controlado todo desde una variable

#!/bin/bash
_DEBUG="on"
function DEBUG()
{
 [ "$_DEBUG" == "on" ] &&  $@
}
 
DEBUG echo 'Reading files'
for i in *
do
  grep 'something' $i > /dev/null
  [ $? -eq 0 ] && echo "Found in $i file"
done
DEBUG set -x
a=2
b=3
c=$(( $a + $b ))
DEBUG set +x
echo "$a + $b = $c"
  • linux/bash/debug.txt
  • Darrera modificació: 22/01/2018 01:12
  • per mate