-2

We do not have any goto statement in bash, hence I used the below function to implement goto for abrupt jump in the code.

Can we handle this condition without using jumpto function?

Based on the solution in this earlier answer to a related question I have the following code:

 #!/bin/bash
    function jumpto
    {
        label=$1
        cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
        eval "$cmd"
        exit
    }
    start="start"
    jumpto $start
start:
while true
do
    for <condition>
     do
        #do something
        for <condition>
        do
        if <condition>
        then
        break 99
        fi
        #do something
        done
    done
 done
   #do something
   #do something 
   #do manythings
    if <condition>
    then
    #do something
    jumpto start
    else
    #do something
    fi

   for <condition>
    do
    #do something
    done
Community
  • 1
  • 1
frp farhan
  • 445
  • 5
  • 19
  • there is no goto statement in bash. – P.... Aug 10 '16 at 11:39
  • yes I knw, that's the reason i implemented it using jumpto function, can we do it without jumpto function by using any flow control methods. – frp farhan Aug 10 '16 at 11:39
  • Instead of " -----" use comment, e.g: `# do something...` Also properly indent your code. – user3439894 Aug 10 '16 at 11:50
  • 1
    @ghoti The code appears to be copy/pasted from one of the less popular answers there. I don't think the OP understands how to use it, or why it isn't very popular. – tripleee Aug 10 '16 at 12:04
  • guys I would be greatful if u can provide the understanding and the help, rather than discussing the copy and duplication. – frp farhan Aug 10 '16 at 12:06
  • Your question needs to make sense; that's why we are discussing here. If you can clarify what you already understand, we can write answers which actually help you. Currently, the question seems extremely cloudy to me, and has already attracted 4 close votes and 1 downvote. – tripleee Aug 10 '16 at 12:07
  • Is there a particular reason why you don't use one of the other answers there? – tripleee Aug 10 '16 at 12:10
  • yes, the flow of the scripts as per the new edit doesn't allows us to use any of those answers, hence i had used this option of creating a function. – frp farhan Aug 10 '16 at 12:11
  • 1
    That makes this an [XY Problem](http://mywiki.wooledge.org/XyProblem). You are asking us for help on the wrong topic. What you need to fix is the problematic program flow, rather than a way to implement something which does not exist by design. How about closing this question and [asking a new one](http://stackoverflow.com/questions/ask) that addresses your program flow challenges? (Don't forget to include an [MCVE](http://stackoverflow.com/help/mcve), so that we can test our answers before posting them.) – ghoti Aug 10 '16 at 12:48
  • sure, @gothi will do as per ur comments.thnx – frp farhan Aug 10 '16 at 12:56

2 Answers2

3

Rather than trying to extend the syntax of the shell to do

start:

for ...; do
    for ...; do
        ...
    done
done

if CONDITION; then
    goto start
else
    SOMETHING
fi

you may do

while true; do
    for ...; do
        for ...; do
            ...
        done
    done

    if ! CONDITION; then
        SOMETHING
        break
    fi
done

or

while true; do
    for ...; do
        for ...; do
            ...
        done
    done

    if ! CONDITION; then
        break
    fi
done

SOMETHING

UPDATE: New code in question:

start:
while true; do
    for ...; do
        for ...; do
            if ...; then
                break 99
            fi
            ...
        done
    done
done

...

if CONDITION; then
    SOMETHING
    goto start
else
    SOMETHING_ELSE
fi

for ...; do
    ...
done

To avoid a goto, this may be transformed into

while true; do
    while true; do
        for ...; do
            for ...; do
                if ...; then
                    break 99   # change to one less than
                               # the total depth, to not exit
                               # outermost loop
                fi
                ...
            done
        done
    done

    ...

    if ! CONDITION; then
        break
    fi

    SOMETHING
done

SOMETHING_ELSE

for ...; do
    ...
done
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
0

If your goal is simply to restart your script from the beginning, you can use exec.

#!/bin/bash

lots
of
code
:
if condition; then
  exec "$0" "$@"
fi

You could implement a crude option to skip to the label, if there is only one.

#!/bin/bash

if [ $1 = '--restart' ]; then
    shift
    # Skip everything in else clause
else
    lots
    more
    code
    :
fi
# :start
lots
of
code
still
:
if condition; then
  exec "$0" --restart "$@"
fi
tripleee
  • 175,061
  • 34
  • 275
  • 318