Shell脚本用户提示/输入 [英] Shell script user prompt/input

查看:87
本文介绍了Shell脚本用户提示/输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是别人写的一个粗糙的korn shell脚本.我对使用shell语法了解不多,甚至不确定是否可行.

This is a crude korn shell script that someone else wrote. I don't know much about using shell syntax and I'm not even sure if this is possible.

我是否可以通过任何方式运行此文件并提示输入日期,从而不必手动进入脚本并每次更改它?

Is there any way for me to run this file and be prompted for the date so that I don't have to manually go into the script and change it each time?

例如,我想将"1/12/09"替换为从用户提示中获取的变量.

For example, I want to replace the "1/12/09" with a variable that is taken from a user prompt.

#!/bin/ksh
./clear_old
./rooms_xls.pl 1/12/09
cd doors
./doors_xls.pl 1/12/09

推荐答案

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

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手册页了解更多信息.

这篇关于Shell脚本用户提示/输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆