8

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?

3
  • What shell are you using exactly? Commented Jul 6, 2013 at 3:22
  • I am using the bash shell Commented Jul 6, 2013 at 3:24
  • Use bash -vx to execute your shell script Commented Jul 6, 2013 at 6:16

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What does this do? Is "caller" a built-in shell feature? Edit: yes, it is.
When I put it in the first few lines of the script, it seems that it cannot print the line number in the function, how can I modify it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.