shell 脚本 没有goto语句,硬是想用怎么办(What if the shell script has no goto statement and just wants to use it)

不要和我讨论goto语句的危害

不要和我讨论goto语句的危害

不要和我讨论goto语句的危害

硬是想用,谁也拦不住

聊一个替代方案

一个简单的例子,话说这里什么时候能有markdown

一个简单的例子

提示用户输入一个名字,如果用户直接回车,那么我们就再提示一次输入名字

按理说,应该是一个完美的goto语句

现在只能用while true 循环

作为一个倔强的老大爷,我想除了一个绝妙的办法

s1(){

 echo -n “name:”

read name

if [ ! $name ]

then

  s1

  exit 0

fi

s2

}

s2(){

   # 其他业务逻辑

}

也就是说,用一个函数,失败了就调用自身,就可以goto了

————————

Don’t talk to me about the dangers of goto statements

Don’t talk to me about the dangers of goto statements

Don’t talk to me about the dangers of goto statements

If you just want to use it, no one can stop it

Talk about an alternative

As a simple example, when can there be a markdown here

A simple example

Prompt the user to enter a name. If the user enters directly, we will prompt the user to enter a name again

Logically, it should be a perfect goto statement

Now you can only loop with while true

As a stubborn old man, I think there is only one wonderful way

s1(){

 echo -n “name:”

read name

if [ ! $name ]

then

s1

exit 0

fi

s2

}

s2(){

# other business logic

}

In other words, if a function fails, call itself, and you can goto