参考:如何在shell脚本中使用goto语句 - VoidCC
效果:
注意修改以下几处:
show you my code:
#!/usr/bin/env bash
# Define the choices to present to the user, which will be
# presented line by line, prefixed by a sequential number
# (E.g., '1) copy', ...)
target_eth='USB 10/100/1G/2.5G LAN'
choices=('ip for 192.168/24' 'ip for 10.71/24' 'show eth list' "get $target_eth info" 'exit')
# Present the choices.
# The user chooses by entering the *number* before the desired choice.
while [ "$menu" != 1 ]; do
printf "\nMain Menu:\n\n"
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
ip\ for\ 192.168/24)
echo "Swithing..."
networksetup -setmanual "$target_eth" 192.168.0.155 255.255.255.0 192.168.0.1
# Set flag here, or call function, ...
echo "already changed. the new ip info is: "
networksetup getinfo "$target_eth"
break
;;
show\ eth\ list)
networksetup -listnetworkserviceorder
# Set flag here, or call function, ...
break
;;
get\ $target_eth\ info)
networksetup getinfo "$target_eth"
break
;;
ip\ for\ 10.71/24)
echo "Swithing..."
networksetup -setmanual "$target_eth" 10.71.2.155 255.255.255.0 10.71.2.1
echo "already changed. the new ip info is: "
networksetup getinfo "$target_eth"
break
;;
exit)
echo "Exiting now.."
menu=1
break
;;
*)
echo "operation not avaliable!"
break
;;
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
done
exit 0
Over.