且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Shell脚本用户提示/输入

更新时间:2023-07-28 19:07:52

如果要提示您(而不是将日期作为参数传递),请使用以下逻辑(或类似方法):

If you want to be prompted (as opposed to passing the date in as a parameter), use the following logic (or something similar):

date=
while [ -z $date ]
do
    echo -n 'Date? '
    read date
done

该循环将继续提示输入日期,直到用户输入除简单的RETURN之外的其他任何内容.

That loop will continue to prompt for the date until the user enters something (anything) other than a simple RETURN.

如果您想添加一些简单的验证,并且使用的是 KSH等于或高于KSH93,请执行以下操作:

If you want to add some simple validation, and you're using a version of KSH that's KSH93 or better, do something like this:

date=
while [ -z $date ]
do
    echo -n 'Date? '
    read date
    if [[ $date =~ ^[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,4}$ ]]
    then
        break
    fi
    date=
done

请参见 ksh93手册页了解更多信息.