Linux Bash goto命令?

Linux Bash goto命令?,linux,bash,goto,Linux,Bash,Goto,我对我的Bash脚本有一个问题,脚本本身工作得很好,但是我试图整理它,但是我找不到一个方法作为“goto”命令,是的,我对Linux Bash非常陌生 我的代码是: echo "What is the street road?" read road echo "What is the address ?" read address echo "User is set as: $road" echo "Address has been set as: $address" while true;

我对我的Bash脚本有一个问题,脚本本身工作得很好,但是我试图整理它,但是我找不到一个方法作为“goto”命令,是的,我对Linux Bash非常陌生

我的代码是:

echo "What is the street road?"
read road
echo "What is the address ?"
read address
echo "User is set as: $road"
echo "Address has been set as: $address"


while true; do
    read -p "Is this correct? " yn
    case $yn in
        [Yy]* )  break;;
        [Nn]* )  exit;;
        * ) break;;
    esac
done
当用户输入“n”时,脚本将自动退出,但我正在尝试整理它,以便它在这里重新循环。因此,如果用户输入“n”,它将再次询问他们的道路和地址

我知道你能做什么 :A 后藤:A (或类似的事情!)但在Bash中,我不确定如何做到这一点


谢谢大家

我会像这样重写你的脚本:

ask () {
    echo "$1"
    read answer

    while true; do
        read -p "Is this correct? " yn
        case $yn in
            [Yy]* )  break;;
            [Nn]* )  exit;;
            * ) break;;
        esac
    done

    eval "$2='$answer'"
}

ask "What is your street?" street
ask "What is the address?" address
echo "Your address has been set to $address $street"
正如我在对您的问题的评论中提到的,在任何语言中使用
goto
,通常都被认为是不好的形式(因为这会导致代码调试困难,除非在非常特殊的情况下),而且在任何情况下都是如此。如果您发现自己编写的代码需要一个
goto
,请花点时间,从键盘上往后靠,重新评估一下您的前提。99.999%的时候,你会发现你实际上并不需要它,而且有一种结构化的编程方法可以更巧妙地完成同样的事情。

你可以采取激进的方式:

ok=no
while   read -p "What is the street road? " road &&
        read -p "What is the address? " address &&
        echo "Road is set to: $road" &&
        echo "Address has been set as: $address" &&
        read -p "Is this correct? " yn
do
    case $yn in
    ([Yy]*) echo "Great!"; ok=yes; break;;
    ([Nn]*) echo "OK - then try again";;
    (*)     echo "Didn't understand that - it did not look like Y or N";;
    esac
done

if [ "$ok" = "yes" ]
then : OK to use answers
else : Do not use answers
fi
这利用了一个事实,即在
while
循环中,可以将任意命令列表作为“条件”。我已经将命令与
&&
链接在一起,因此它们都必须成功,但是您可以有独立的命令,在这种情况下,最后一个命令才是重要的。我还利用了
read-p'prompt'var
符号作为初始值以及'Is this correct'符号

对话范例:

$ bash prompt.sh
What is the street road? California
What is the address? 1189
Road is set to: California
Address has been set as: 1189
Is this correct? n
OK - then try again
What is the street road? California Ave
What is the address? 2291
Road is set to: California Ave
Address has been set as: 2291
Is this correct? y
Great!
$

我建议您将其与GNU bash一起使用:

#!/bin/bash

until [[ $yn =~ ^[Yy]$ ]]; do
   read -p "What is the street road? " road
   read -p "What is the address ? " address

   echo "User is set as: $road"
   echo "Address has been set as: $address"

   read -p "Is this correct? " yn
done

# continue with your code here

有点非典型,但你可以:

#!/bin/sh
while
    read -p 'What is the street road? ' road
    read -p 'What is the address ? ' address
    echo "User is set as: $road"
    echo "Address has been set as: $address"
    read -p "Is this correct? " yn
    case $yn in
        [Yy]* )  false;;
        * )  true;;
    esac
do
    :
done
echo "User is set as: $road"
echo "Address has been set as: $address"

通常,如果您发现自己在脚本中使用了
goto
,那么您在某个地方做了错事<代码>bash具有(有限的)功能支持,这将帮助您实现所需功能。请参阅:另请参阅正则表达式比较的良好使用。您还应该处理
$yn
是否定的情况(在OP中,这会导致完全退出脚本)。++,但是请注意术语“GNU bash”是多余的和令人困惑的,因为GNU bash是唯一的bash,这就是为什么它不需要限定的原因(特别是考虑到问题被标记为
bash
)@mklement0:谢谢。我故意使用“GNUBash”。Busybox的bash实现不同于GNUBash的实现。例如:Busybox的bash不易受到bash错误的攻击。@Cyrus:我从未使用过Busybox,但维基百科告诉我它根本不使用bash:。据我所知,Cygwin bash是为该应用程序编译的常规bash源代码。相比之下,win-bash是一个边缘项目,它来自于bash的一个古老版本:。因此,我建议只使用“Bash”来引用官方的GNU Bash,并仅在实际需要时调用win Bash等边缘项目,以避免混淆。这不会导致在用户回答
n
时重复提示。如果可以避免,请不要使用
eval
。在这里,您可以使用
printf-v“$2”'%s'$answer”