Test shell script step by step?

Hi to all,

I don't know if someone has done sometime a MS Excel Macro, that allows us to press F8 over the code
to see step by step, to mention something, how is running the code, which values take the variables,
if some loop is executing correct or where a error occurs, and some other debugging actions.

Is there some way/application to run a shell script (bash, awk, sed etc) similarly, step by step?

*I'm using Ubuntu 11, Cygwin

Thanks in advance

For shell,you can use set -x as first line or magic cookie (#!/bin/bash -x), then execute the script to see it's flow.
Awk you can debug by printing inside the code and observing awk's warnings and errors during execution.
Sed well, i have no idea :slight_smile:

You can also set -e , which will cause the first line which returns a nonzero exit status to quit the script.

Thanks both, I've tested using the code below and error messages are the same if I use set -x or not.
*This code contains a syntax error in if statement

a=7
b=9

if a = b ) ;then
    echo "equals"
else
    echo "differents"
fi

Besides that, is not possible to execute the script line by line to see how the code works, even when the code has no error?

Because it could be only a bad algorithm and not syntax errors.

Thanks for help so far.

Of course the error is the same; the code hasn't changed after all...

Unlike an IDE though, a shell does not have total control of its environment, meaning, debugging could cause radically different behavior (input meant for the script ending up in the 'wait till enter for next line' function, etc.) You're trying to build an IDE out of a shell.

Hmm. Thinking on it.

Hi Corona,

I understand what you're explaining me.

From time to time I do Excel macros and shell scripts, in both scenarios, by far I'm not an expert, but in Excel using that feature,
for instance, I can find the errors faster and/or improve a bad loop.

I only was wondering if in shell that feature exists because I know even less than Excel Macros and I need more
time to find where the error is, or how to know how it goes the code even it has no errors.

Thanks for help.

Grettings

Shells just don't work that way for a whole bunch of reasons. Not being exactly the same as another language you've used doesn't make it broken or incomplete.

Like learning any other language, you'll have to learn new habits, new methods, and new tools.

You can put a read statement most anywhere you want to make the shell wait for you to hit ENTER for instance.

1 Like

Of course, I agree with that, I don't pretend to say shell it has less than any other tool at all. Only I'm asking if there is a similar way to test the code.

This I didn't know, I'll try to put in practice, I think it will help me a lot.

Many thanks for share your knowledge.

Grettings