-1

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
4
  • You need to use the select command: askubuntu.com/questions/1705/…. Commented Mar 17, 2022 at 8:24
  • Does this answer your question? Menu Driven Shell Script bash Commented Mar 17, 2022 at 8:25
  • The code you posted does not behave as you describe. If you input e.g. q it will simply not execute any of the commands in the case statement. Voting to close as unreproducible. Commented Mar 17, 2022 at 8:34
  • Also, please use meaningful identifiers. Why num? You accept non-numbers as well. What about choice? Commented Mar 17, 2022 at 9:04

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.