Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Questions about shell script return method

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading