linux - 在 bash 中跳过(转到)一段代码?

标签 linux bash

bash 中有 goto 的等效项吗?下面是我正在编写的脚本的一小部分,根据用户输入,我想跳到下一段代码。

#!/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

最佳答案

不,没有。

但是这个例子不需要它:

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
# ...

关于linux - 在 bash 中跳过(转到)一段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344215/

相关文章:

c++ - 进程管理代码在 Linux 和 Windows 上的行为不同 - 为什么?

c - 使用 ld 手动链接 SDL/openGL 应用程序

linux - 将 .txt 文件的内容分类为 .bash_profile

bash - 如何在 bash 提示符中插入环境变量

linux - 使用 echo 和 cat 进行不需要的换行

c - RPC - 类型冲突错误

linux - linux下如何将行转换为列

linux - 在 Linux 中,如何编写脚本来抓取所有 .js 文件?

linux - 启动 bash 脚本

bash - cpio VS tar 和 cp