linux 网站监控脚本 有goto 吗,如何在shell脚本中使用goto语句

正如其他人所指出的那样,有一个在bash没有goto(或其他POSIX样弹) - 其它更为灵活的流量控制结构取而代之。

在man bash中查找标题Compound Commands。

对您而言,select命令是正确的选择。 由于如何使用它可能不是很明显,这里的东西让你开始:

#!/usr/bin/env bash

echo "Main Menu"

# Define the choices to present to the user, which will be

# presented line by line, prefixed by a sequential number

# (E.g., '1) copy', ...)

choices=('copy' 'exit')

# Present the choices.

# The user chooses by entering the *number* before the desired choice.

select choice in "${choices[@]}"; do

# If an invalid number was chosen, $choice will be empty.

# Report an error and prompt again.

[[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }

# Examine the choice.

# Note that it is the choice string itself, not its number

# that is reported in $choice.

case $choice in

copy)

echo "Copying..."

# Set flag here, or call function, ...

;;

exit)

echo "Exiting. "

exit 0

esac

# Getting here means that a valid choice was made,

# so break out of the select statement and continue below,

# if desired.

# Note that without an explicit break (or exit) statement,

# bash will continue to prompt.

break

done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值