Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging shell scripts with line numbers

Tags:

linux

shell

unix

I inherited a over 700 line shell script and noticed that when I ran the script it spits out error at some point of execution.

E.g error that I see on the console is something like

cat: /Wreck/wreck_module.rb: No such file or directory 

I have tried to use set -x and most of the tips from this link, however I noticed that all the output that I was getting were pretty noisy.

Is there a way to get the exact line number of where a shell command returned a non-zero status?

like image 911
tawheed Avatar asked Jul 06 '13 03:07

tawheed


1 Answers

Put this at the top of the script you want to debug:

#!/bin/bash
function trace_line(){
  caller
}
trap trace_line debug

and perhaps redirect the output to a file for easy analysis.

like image 153
perreal Avatar answered Sep 30 '22 02:09

perreal