-1

Is there an equivelant to goto in bash? Below is a small part of a script I'm working on and depending on the user input I want to skip to the next piece of code.

#!/bin/bash
WAN1_prompt
read -p "WAN1 exists?: " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
Skip to WAN1
else
Skip to WAN2_prompt
fi


WAN1
read -p "Enter WAN1 IP: " wan1
  if [[ $wan1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  sed -i~ -e "s/bizip/$wan1/g" test.txt
else
  echo Error
fi


WAN2_prompt
read -p "WAN2 exists?: " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
Skip to WAN2
else
exit
fi

read -p "Enter WAN2 IP: " wan2
  if [[ $wan2 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
  sed -i~ -e "s/bizip/$wan2/g" test.txt
else
  echo Error
fi
sjaak
  • 644
  • 1
  • 7
  • 18
  • note that if it existed (as in windows batch) WAN2_prompt would be executed after WAN1, which would lead to more difficult maintenace compared with function style – Nahuel Fouilleul May 15 '18 at 07:33

1 Answers1

2

No, there isn't.

But this example doesn't need it:

read -p "WAN1 exists?: " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
  # WAN1
  read -p "Enter WAN1 IP: " wan1
  if [[ $wan1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
    sed -i~ -e "s/bizip/$wan1/g" test.txt
  else
    echo Error
  fi
fi
# WAN2_prompt
read -p "WAN2 exists?: " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
  read -p "Enter WAN2 IP: " wan2
  if [[ $wan2 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
    sed -i~ -e "s/bizip/$wan2/g" test.txt
  else
    echo Error
  fi
fi
# ...
rici
  • 234,347
  • 28
  • 237
  • 341