RSSAmplifier

Bash Prompt · Mar 18, 2024

Debugging Bash Scripts - Part 5 - Jump forward in a script

0
Sign in to vote or save

Elliot Cooper · Bash Prompt

Here is the list of all the Bash debugging posts:

The exit command will allow you to halt a script without executing everything after the exit command.

If you want to do the opposite and skip some of the script there is no builtin command to do this. This is useful for debugging so that you can work only on the problem section without re-executing a prior portion of the scrip.

However, the following does give you a workaround to jump over and ignore some of the scripts.

Take the following script:

#!/bin/bash
echo "One"
echo "Two"
echo "Three"

This will print the following output:

$ ./counter.sh
One
Two
Three

If you want to jump forward and skip some code use the following structure:

# Code to run
if false; then
# Code to ignore
fi
# Start running code again

Putting this into our script to jump over the first two echo commands will give us the following:

#!/bin/bash
if false; then
echo "One"
echo "Two"
fi
echo "Three"

This now outputs the following:

$ ./counter.sh
Three

The only problem with using this is that it may be hard to find the start and stop of this loop in a long script. Use a comment to make it easier to find e.g.:

#!/bin/bash
# GOTO start
if false; then
echo "One"
echo "Two"
# GOTO end
fi
echo "Three"

Read the original on bash-prompt.net

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.