-1

Is there a way to do this, or a better approach?

I have a logical error in my script and want to execute it line by line, pausing after each line so I can see what is happening (checking variables, viewing files, etc) before executing the next line. I have placed echo var = $var and cat file statements after a lot of the lines, but that has been confusing and I am thinking there must be a better way. (btw, I am not looking for a program to run, but want to do it with commands)

Insideup
  • 115
  • also see https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html – Rinzwind Nov 27 '20 at 20:48
  • 2
    Does this answer your question? How to debug bash script? –  Nov 27 '20 at 21:24
  • 1
    "pausing after each line so I can see what is happening (checking variables, viewing files, etc)" < This is too vague. Please state it more precisely. Giving a concrete example is the ideal. Do you want a sort of gdb thing, but for Bash? I'm afraid there is none. – Quasímodo Nov 27 '20 at 22:33
  • I want the execution to pause until I tell it to start up again, after each line. Some of the lines make edits to files and I need to watch how files are changing to understand where my errors are. I am not looking for a program, was hoping for a few commands instead. – Insideup Nov 27 '20 at 23:11
  • You can insert a read after each line with sed 'a\read' originalScript > debugScript and then execute debugScript. – Quasímodo Nov 27 '20 at 23:21
  • Great. That's what I was looking for. – Insideup Nov 27 '20 at 23:32

2 Answers2

2

From Stack overflow:

set -x or set -o xtrace expands variables and prints a little + sign before the line.

set -v or set -o verbose does not expand the variables before printing.

Use set +x and set +v to turn off the above settings.

On the first line of the script, one can put #!/bin/sh -x (or -v) to have the same effect as set -x (or -v) later in the script.

The above also works with /bin/sh.

See the bash-hackers' wiki on set attributes, and on debugging.

$ cat shl
#!/bin/bash

DIR=/tmp/so ls $DIR

$ bash -x shl

  • DIR=/tmp/so
  • ls /tmp/so

$

  • That's pretty cool. Although not what I was hopping for, to be more clear, I want the execution to pause until I tell it to start up again, after each line. I need to watch how files are changing to understand where my errors are. – Insideup Nov 27 '20 at 21:02
0

If I understood your problem I'm thinking about two very simple and alternatives solutions:

  1. type on a terminal the name of you script-shell (for example csh.. I dont't know).. It will appear the prompt letting you insert your instructions line by line (so simply copy them from your script and paste it on terminale). In this way you can see what is happening each step at time.

or

  1. modify your script and insert the command "sleep" (followed by a time interval in seconds) before your "critical" line that produces unexpected errors. It works both with csh and sh. For example:

     #this is a script
     #!/bin/sh        
     line 1        
     line 2        
     sleep 200  #it means 200 seconds of pausing before next istruction        
     line 3         
     etc..
    

In this example you have the time to see in your workdir what are the results of your computation after executing line 2 but before line 3