-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)

6
  • 2
    Does this answer your question? How to debug bash script?
    – user986805
    Nov 27, 2020 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, 2020 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, 2020 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, 2020 at 23:21

2 Answers 2

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
$
1
  • 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, 2020 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

1
  • That's interesting, too. Instead of "sleep", is there a "pause until key stroke such-and-such"?
    – Insideup
    Nov 27, 2020 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.