'Questions about shell script return method [closed]

I made a script to configure the server through the PXE. When boot via pxe, The shell.sh appears on the server. But if an incorrect value is entered in the script input paragraph(read), the power is turned off immediately. Is there a way to return it to the input paragraph?

Added the script below:

#!/bin/bash
(omit)
echo "*************************"
echo "Select script"
echo "      1) firmware"
echo "      2) capture"
echo "      3) get"
echo ""
echo "      4) SMBSET_FW+BIOSSET"
echo ""
echo "      b) bash shell"
echo "      n) systeminfo"
echo "      e) poweroff"
echo "      r) reboot"
echo "*************************${normal}"
read num

case $num in
    1) bash ./firmware.sh;;
    2) bash ./capture.sh;;
    3) bash ./get.sh;;
    4) bash ./smb.sh;;
    b) bash ./bash.sh;; 
    n) bash ./systeminfo.sh;; 
    e) poweroff -f;;
    r) reboot -f;;
esac


Solution 1:[1]

Put the code in a loop. Use continue to repeat the loop if they enter an unmatched response, otherwise break out of the loop.

while :; do
    echo "*************************"
    echo "Select script"
    echo "      1) firmware"
    echo "      2) capture"
    echo "      3) get"
    echo ""
    echo "      4) SMBSET_FW+BIOSSET"
    echo ""
    echo "      b) bash shell"
    echo "      n) systeminfo"
    echo "      e) poweroff"
    echo "      r) reboot"
    echo "*************************${normal}"
    read num
    
    case $num in
        1) bash ./firmware.sh;;
        2) bash ./capture.sh;;
        3) bash ./get.sh;;
        4) bash ./smb.sh;;
        b) bash ./bash.sh;; 
        n) bash ./systeminfo.sh;; 
        e) poweroff -f;;
        r) reboot -f;;
        *) echo "Invalid response, try again"; continue;;
    esac
    break
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar