What could be the substitute to handle the flow control of go-to in SHELL script [duplicate]

↘锁芯ラ 提交于 2019-12-08 14:15:54

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/38872348/what-could-be-the-substitute-to-handle-the-flow-control-of-go-to-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!