-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

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

$

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