循环代码,直到用户输入成功

时间:2017-12-13 20:04:16

标签: bash shell scripting

我是bash脚本新手。我写了一个代码,它将根据用户输入的操作系统和软件版本下载软件。我的代码需要再次循环,直到用户提供正确的os-bit number(64 or 32) and correct software version。如果满足条件,则应执行并继续下一行。

echo 'Enter the OS-Bit'
read osbit
echo 'Enter the Software Version To Install'
read software_version

这是我的完整代码

if [ $osbit == "64" ];then
    os_version="x86_64"
    cd $directoy_path; curl -L -O https://myblog/downloads/software-$software_version-$os_version.rpm

elif [ $osbit == "32" ];then
    os_version="i686"
    cd $directoy_path; curl -L -O https://myblog/downloads/software-$software_version-$os_version.rpm

else
    echo 'Please enter valid os bit #'
fi
rpm -vi $software_version-$os_version.rpm

echo 'Download Completed'
echo Installing $software_version
rpm --install $directoy_path/dfg-$software_version-$os_version.rpm

1 个答案:

答案 0 :(得分:2)

您可以将条件放在无限循环中, 并在输入正确时突破它:

echo 'Enter the Software Version To Install'
read software_version

while true; do
    echo 'Enter the OS-Bit'
    read osbit

    if [ "$osbit" == "64" ];then
        os_version="x86_64"
        break
    elif [ "$osbit" == "32" ];then
        os_version="i686"
        break
    else
        echo 'Please enter valid os bit #'
    fi
done

cd "$directoy_path" && curl -L -O "https://myblog/downloads/software-$software_version-$os_version.rpm"