tawheed
tawheed

Reputation: 5821

Debugging shell scripts with line numbers

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?

Upvotes: 8

Views: 1823

Answers (1)

perreal
perreal

Reputation: 98118

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.

Upvotes: 5

Related Questions